コード例 #1
0
        /* Takes user input, IP and Port*/

        public async void Connect(string address, ushort port)
        {
            //try to parse user input into a valid IP and Port
            _connection = new TcpClient();
            try
            {
                await _connection.ConnectAsync(address, port);

                MainWindow._gstatus = MainWindow.State.ConnectedOff;
                UI.UpdateUi("Connection Succesfull", true);

                using (_NetworkIo = new Nio <TelemetryR, Command>())
                {
                    _NetworkIo.Initiate(_connection.GetStream(), new TelemetryR(), new Command(false, 0, 0), cts);

                    while (!cts.IsCancellationRequested)
                    {
                        var temp = await _NetworkIo.RecieveData();

                        Telemetry.Add(temp);
                        UI.UpdateUi(temp);
                    }
                }
            }
            catch (Exception exception)
            {
                MainWindow._gstatus = MainWindow.State.Idle;
                UI.UpdateUi(exception.Message);
                cts.Cancel();
            }
        }
コード例 #2
0
        public async void Readnetwork()
        {
            long localSesionId     = _sesionID;
            var  localCancellation = cts;

            if (!AllConnections.Exists(p => p == this)) //if it is not in the list of dysfunctional connections, ad it there
            {
                AllConnections.Add(this);
            }

            try
            {
                _tcpListen = new TcpListener(LocalIp, _port);
                Userinterface.Update_IP_Config(LocalIp, _port);
                _tcpListen.Start();

                Active = true;
                Userinterface.Update_IO_Displays(0, _clientsConnected, _sentframes, _commandsRecived);
                using (_tcpClient = await _tcpListen.AcceptTcpClientAsync())
                {
                    using (NetworkIo = new Nio <Command, TelemetryR>())
                    {
                        NetworkIo.Initiate
                            (_tcpClient.GetStream(), new Command(false, 1, 1), new TelemetryR(), cts);
                        _clientsConnected++;
                        Userinterface.Update_IO_Displays(1, _clientsConnected, _sentframes, _commandsRecived, true);

                        while (!localCancellation.IsCancellationRequested)
                        {
                            try
                            {
                                Command temp = await NetworkIo.RecieveData();

                                _commandsRecived++;
                                Userinterface.Update_IO_Displays(1, _clientsConnected, _sentframes, _commandsRecived);
                                Userinterface.Update_Command(temp);
                            }
                            catch (ArgumentNullException exception)
                            {
                                Logging.Add(exception.ToString() + exception.Message);
                                /* Indicates Disconnection*/
                                if (_sesionID == localSesionId)
                                {
                                    cts.Cancel();
                                }
                                break;
                            }
                            catch (System.IO.IOException exception)
                            {
                                Logging.Add(exception.ToString() + exception.Message);
                                if (_sesionID == localSesionId)
                                {
                                    cts.Cancel();
                                }
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                //_userinterface.Update_feedback("Port is already in use, choosing random port");

                /*Here was a really bad idea with changing ports
                 * var rand = new Random();
                 * _port = (ushort)rand.Next(0, 65000);
                 *
                 *
                 */
                if (exception.Message.Contains("address is not valid "))
                {
                    _connectionErorCount++;
                    if (!DisposedConnections.Exists(p => p == this)) //if it is not in the list of dysfunctional connections, ad it there
                    {
                        DisposedConnections.Add(this);
                    }
                    Active = false;
                    Dispose();
                    Logging.Add("Attempted to start listeninig on invalid IP address " + LocalIp.ToString());
                }
                else if (exception.Message.Contains("Only one usage of each socket address (protocol/network address/port) is normally permitted"))
                {
                    _connectionErorCount++;
                    if (!DisposedConnections.Exists(p => p == this)) //if it is not in the list of dysfunctional connections, ad it there
                    {
                        DisposedConnections.Add(this);
                    }
                    Active = false;
                    Dispose();

                    string listofDisposedconnections = "Disposed COnnections:";

                    string listofAllconnections = "All COnnections:";

                    foreach (Servlet connection in DisposedConnections)
                    {
                        listofDisposedconnections += connection.LocalIp + "\n";
                    }

                    foreach (Servlet connection in AllConnections)
                    {
                        listofAllconnections += connection.LocalIp + "\n";
                    }

                    Logging.Add("Attempted to start listening on " + LocalIp.ToString() + " but socket address is already in use \n" + listofAllconnections + listofDisposedconnections);
                }
                else if (exception.Message.Contains("Cannot access a disposed object"))
                {
                    if (!DisposedConnections.Exists(p => p == this)) //if it is not in the list of dysfunctional connections, ad it there
                    {
                        DisposedConnections.Add(this);
                    }
                    Active = false;
                    Dispose();
                    Logging.Add("Connection " + LocalIp.ToString() + "was closed");
                }
                else
                {
                    Active = false;
                    Logging.Add(exception.ToString() + exception.Message);
                    if (_sesionID == localSesionId)
                    {
                        cts.Cancel();
                    }
                }
            }
        }