Пример #1
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);
        }