示例#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
        private void HandleEventClientComm(object client)
        {
            Debug.WriteLine("Client connected to event channel");

            var tcpClient = (System.Net.Sockets.TcpClient)client;
            var stream    = OnPrepareStream(tcpClient.GetStream());

            var eventQueue = new TcpMessageQueue();

            lock (_eventQueues)
            {
                _eventQueues.AddLast(eventQueue);
            }

            while (true)
            {
                var messages = eventQueue.WaitAndPopAll();

                var success = true;
                foreach (var message in messages)
                {
                    success = TcpProtocolHelper.Write(stream, message);
                    if (!success)
                    {
                        break;
                    }
                }

                if (!success)
                {
                    break;
                }
            }

            lock (_eventQueues)
            {
                _eventQueues.Remove(eventQueue);
            }

            Debug.WriteLine("Client disconnected from event channel");
        }
示例#3
0
        public string SendAndReceive(string message)
        {
            lock (_dataLockObject)
            {
                if (_dataClient == null)
                {
                    _dataClient = new System.Net.Sockets.TcpClient();

                    _dataClient.Connect(_host, _dataPort);

                    _dataStream = OnPrepareStream(_dataClient.GetStream());
                }

                message = PrepareOutgoingMessage(message);

                var success = TcpProtocolHelper.Write(_dataStream, message);
                if (!success)
                {
                    return(null);
                }

                return(PrepareIncommingMessage(TcpProtocolHelper.Read(_dataStream)));
            }
        }
示例#4
0
        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();
        }