Пример #1
0
        protected virtual void Dispose(bool disposing)
        {
            if (State == ServerState.Started)
            {
                Stop();
            }

            // Free managed resources
            if (disposing)
            {
                Threads?.Clear();
                Threads = null;

                Connections?.Clear();
                Connections = null;

                TcpDataQueue?.Clear();
                TcpDataQueue = null;

                ThreadCancellationToken?.Dispose();
                ThreadCancellationToken = null;

                TcpServer = null;
                SyncLock  = null;
            }
            // Free native resources here if there are any
        }
Пример #2
0
        private void ProcessThread(object state)
        {
            CancellationToken tct = (CancellationToken)state;

            Log.InfoFormat("Thread '{0}' started", Thread.CurrentThread.Name);

            while (!tct.IsCancellationRequested)
            {
                int queueLength = 0;
                while (queueLength == 0)
                {
                    Thread.Sleep(THREAD_INTERVAL_PROC);
                    lock (SyncLock)
                        queueLength = TcpDataQueue.Count;
                }

                Log.DebugFormat("Process message in queue [length: {0}]", queueLength);
                if (queueLength >= HTTP_TRAFFIC_WARNING_THRESHOLD)
                {
                    Log.WarnFormat("Traffic on this server is high [queue length: {0}]", queueLength);
                }

                TcpData tcpData = null;
                lock (SyncLock)
                    tcpData = TcpDataQueue.Dequeue();

                Log.DebugFormat("TCP data packet '{0}' dequeued and ready to be processed.", tcpData.Id);
                Task.Factory.StartNew(ProcessTcpPacket, tcpData);
            }

            Log.InfoFormat("Thread '{0}' stopped", Thread.CurrentThread.Name);
        }
Пример #3
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);
            }
        }
Пример #4
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);
        }