コード例 #1
0
        private void ListenForEvents()
        {
            _eventClient = new System.Net.Sockets.TcpClient();
            _eventClient.Connect(_host, _eventPort);

            _eventStream = OnPrepareStream(_eventClient.GetStream());

            while (_listenForEvents)
            {
                var rawMessage = TcpProtocolHelper.Read(_eventStream);
                if (rawMessage == null)
                {
                    break;
                }

                var message = TcpMessage.FromRaw(rawMessage);

                foreach (var handler in _eventHandlers)
                {
                    if (handler(message))
                    {
                        break;
                    }
                }
            }
        }
コード例 #2
0
ファイル: TcpServer.cs プロジェクト: TimotejRybar/sdb
        private void HandleDataClientComm(object client)
        {
            var tcpClient = (System.Net.Sockets.TcpClient)client;

            var addressSplits = tcpClient.Client.RemoteEndPoint.ToString().Split(':');

            var host = new TcpConnectedHost
            {
                TcpClient = tcpClient,
                IPAddress = addressSplits[0]
            };

            if (!IsAllowed(host.IPAddress))
            {
                Debug.WriteLine("Client refused access to data channel. IP: " + host.IPAddress);
                tcpClient.Close();
                return;
            }

            Debug.WriteLine("Client connected to data channel. IP: " + host.IPAddress);

            var stream = OnPrepareStream(host.TcpClient.GetStream());

            _connectedDataClients.AddLast(host);

            while (true)
            {
                var rawRequest = TcpProtocolHelper.Read(stream);

                if (rawRequest == null)
                {
                    break;
                }

                rawRequest = PrepareIncommingMessage(host, rawRequest);

                Debug.WriteLine(DateTime.Now.ToLongTimeString() + " [R] " + rawRequest);

                TcpMessage response = null;

                var request = TcpMessage.FromRaw(rawRequest);

                foreach (var handler in _dataRequestHandlers)
                {
                    try
                    {
                        response = handler.Invoke(host, request);
                        if (response != null)
                        {
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        response = TcpMessage.Error(e.ToString());
                        break;
                    }
                }

                if (response == null)
                {
                    response = TcpMessage.Error("Could not handle request");
                }

                var responseMessage = response.ToString();

                Debug.WriteLine(DateTime.Now.ToLongTimeString() + " [S] " + responseMessage);

                responseMessage = PrepareOutgoingMessage(host, responseMessage);

                var success = TcpProtocolHelper.Write(stream, responseMessage);
                if (!success)
                {
                    break;
                }
            }

            Debug.WriteLine("Client disconnected from data channel. IP: " + host.IPAddress);
            tcpClient.Close();
        }
コード例 #3
0
 public TcpMessage SendAndReceive(TcpMessage request)
 {
     return(TcpMessage.FromRaw(SendAndReceive(request.ToString())));
 }