AsterixDisplayAnalyser.FrmReplayForm.btnStartStop_Click C# (CSharp) Метод

btnStartStop_Click() приватный Метод

private btnStartStop_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
Результат void
        private void btnStartStop_Click(object sender, EventArgs e)
        {
            AsterixReplay.ReplayStatus ReplayStatus = AsterixReplay.LANReplay.GetCurrentStatus();

            switch (ReplayStatus)
            {
                case AsterixReplay.ReplayStatus.Disconnected:

                    bool Input_Validated = true;
                    IPAddress IP = IPAddress.Any;
                    IPAddress Multicast = IPAddress.Any;
                    int PortNumber = 2222;

                    if (labelSourceFile.Text == "N/A")
                    {
                        MessageBox.Show("Please set the input file");
                        Input_Validated = false;
                    }
                    else
                    {

                        // First make sure that all boxes are filled out
                        if ((!string.IsNullOrEmpty(this.txtboxIPAddress.Text)) &&
                             (!string.IsNullOrEmpty(this.comboBoxNetworkInterface.Text)) &&
                            (!string.IsNullOrEmpty(this.textboxPort.Text)))
                        {

                            // Validate that a valid IP address is entered
                            if ((IPAddress.TryParse(this.txtboxIPAddress.Text, out Multicast) != true) || (IPAddress.TryParse(this.comboBoxNetworkInterface.Text, out IP) != true))
                            {
                                MessageBox.Show("Not a valid IP address");
                                Input_Validated = false;
                            }
                            else // Add a check that this is a valid multicast address
                            {
                                UdpClient TempSock;
                                TempSock = new UdpClient(2222);// Port does not matter
                                // Open up a new socket with the net IP address and port number
                                try
                                {
                                    TempSock.JoinMulticastGroup(Multicast, 50); // 50 is TTL value
                                }
                                catch
                                {
                                    MessageBox.Show("Not valid Multicast address (has to be in range 224.0.0.0 to 239.255.255.255");
                                    Input_Validated = false;
                                }
                                if (TempSock != null)
                                    TempSock.Close();
                            }

                            if (int.TryParse(this.textboxPort.Text, out PortNumber) && (PortNumber >= 1 && PortNumber <= 65535))
                            {
                            }
                            else
                            {
                                MessageBox.Show("Invalid Port number");
                                Input_Validated = false;
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please fill out all data fileds");
                            Input_Validated = false;
                        }
                    }

                    // Input has been validated, so lets connect to the provided multicast address and interface
                    if (Input_Validated == true)
                    {
                        // Syntatically all the provided data is valid, so save it off so it persists over the sessions
                        Properties.Settings.Default.ReplayMulticast = this.txtboxIPAddress.Text;
                        Properties.Settings.Default.ReplayPort = this.textboxPort.Text;
                        Properties.Settings.Default.Save();

                        if (AsterixReplay.LANReplay.Connect(labelSourceFile.Text, IP, Multicast, PortNumber) == true)
                        {
                            this.btnStartPause.Enabled = true;
                            this.btnStartPause.Text = "Start";
                            this.btnConnectDisconnect.Text = "Disconnect";
                            lblBytesSent.Text = "0 Bytes";
                        }
                        else
                        {
                            MessageBox.Show("No connection is possible, please check multicast address and port and interface address");
                        }
                    }

                    break;

                // Anything else just disconnect
                case AsterixReplay.ReplayStatus.Connected:
                case AsterixReplay.ReplayStatus.Paused:
                case AsterixReplay.ReplayStatus.Replaying:
                    AsterixReplay.LANReplay.Disconnect();
                    this.btnStartPause.Text = "Start";
                    this.btnConnectDisconnect.Text = "Connect";
                    this.btnStartPause.Enabled = false;
                    this.progressBar1.Visible = false;
                    ReplayHasCompleted = false;
                    break;
            }
        }