Exemple #1
0
        private async void ConnectionReceived(SocketAsyncEventArgs acceptArgs)
        {
            if (_connection != null)
            {
                // send sensor busy?
                acceptArgs.AcceptSocket?.Dispose();
            }
            else
            {
                try
                {
                    _connection = acceptArgs.AcceptSocket;
                    var helloData = await _connection.ReceiveAsync(HelloPacket.Size);

                    // todo check version header
                    var helloPacket = HelloPacket.Parse(helloData, 0);

                    var helloResponse = new HelloPacket(1);
                    await _connection.SendAsync(helloResponse.GetData());

                    Init((IPEndPoint)_connection.RemoteEndPoint);
                    StartReceivingData();
                    StartSendingData();
                }
                catch (Exception)
                {
                    // todo log?
                    _connection = null;
                }
            }
            ResetAccept(acceptArgs);
        }
Exemple #2
0
        public async Task <bool> OpenNetworkAsync()
        {
            if (_connection != null)
            {
                return(true);
            }

            // TODO winsock/Socket vs. RT sockets
            try
            {
                var con = new Socket(SocketType.Stream, ProtocolType.Tcp);

                // write header/ protocol version/ connection config
                var helloPacket = new HelloPacket(1);
                _connection = await con.ConnectAsync(_sensorAddress, helloPacket.GetData());

                // read the response from the server

                var helloResponse = await _connection.ReceiveAsync(HelloPacket.Size);

                helloPacket = HelloPacket.Parse(helloResponse, 0);

                // TODO: compare protocol versions

                // Start receive loop - TODO: cancellation token
                Init((IPEndPoint)_connection.RemoteEndPoint);
                StartReceivingData();
                StartSendingData();
            }
            catch (Exception)
            {
                _connection?.Dispose();
                _connection = null;

                IsConnected = false;
                return(false);
            }

            IsConnected = true;
            return(true);
        }