Exemple #1
0
        private void OnDataReceived(object sender, TcpDataEventArgs e)
        {
            if (e.TcpData == null || e.TcpData.Data == null || e.TcpData.RemoteEndpoint == null)
            {
                return;
            }

            Log.DebugFormat("TCP data packet '{0}' received from '{1}'", e.TcpData.Id, e.TcpData.RemoteEndpoint);

            lock (SyncLock)
            {
                TcpDataQueue.Enqueue(e.TcpData);
                Log.InfoFormat("TCP data packet '{0}' queued for processing", e.TcpData.Id);
            }
        }
Exemple #2
0
        private void ProcessTcpPacket(object state)
        {
            TcpData tcpData = state as TcpData;

            if (tcpData == null)
            {
                return;
            }

            Log.InfoFormat("Process TCP data packet '{0}' from '{1}' in direction '{2}'", tcpData.Id, tcpData.RemoteEndpoint, tcpData.Direction);

            switch (tcpData.Direction)
            {
            // Process inbound data
            case TcpDataDirection.Inbound:
            {
                try
                {
                    IProtocolAction action = ProtocolActionFactory.Create(tcpData);
                    if (action == null)
                    {
                        return;
                    }

                    // TODO: Consistency check that Action is valid in this context

                    TcpData tcpResponseData = action.Execute(tcpData);
                    if (tcpResponseData != null)
                    {
                        lock (SyncLock)
                            TcpDataQueue.Enqueue(tcpResponseData);
                    }
                }
                catch (Exception e)
                {
                    Log.ErrorFormat("Error occurred while processing TCP data packet; {0}", e.Message);
                }
                finally
                {
                }

                break;
            }

            // Process outbound data
            case TcpDataDirection.Outbound:
            {
                TcpConnection connection = Connections.Find(m => m.RemoteEndpoint == tcpData.RemoteEndpoint);
                if (connection != null)
                {
                    try
                    {
                        connection.Send(tcpData.Data);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorFormat("Error occurred while processing response; {0}", e.Message);
                    }
                }
                else
                {
                    Log.WarnFormat("Connection for client '{0}' not found", tcpData.RemoteEndpoint);
                }

                break;
            }
            }

            Log.InfoFormat("Processing of TCP data packet '{0}' complete", tcpData.Id);
        }