예제 #1
0
        private void SendMessageToClient(ConnectorBase sendConnector, ConcurrentQueue <Package> queue, CancellationToken ct)
        {
            RaiseLogEvent(LogType.Info, "SendMessageToClient Thread started.");
            while (true)
            {
                Package package = null;
                bool    result  = queue.TryDequeue(out package);
                if (result)
                {
                    try
                    {
                        // TODO [log4net] LOG 4 SendMessageToClient()
                        //log4.Debug(package.ToString());
                        sendConnector.Send(package);
                    }
                    catch (Exception ex)
                    {
                        lock (connectedConnectors)
                        {
                            connectedConnectors.Remove(sendConnector);
                        }
                        string clientId = "Unknown";
                        if (sendConnector.GetType() == typeof(ConnectorTcp))
                        {
                            clientId = string.Format("{0}", ((ConnectorTcp)sendConnector).Client.Client.RemoteEndPoint);
                            RaiseClientDisconnectedEvent(((ConnectorTcp)sendConnector).Client.Client.RemoteEndPoint);
                        }
                        RaiseLogEvent(LogType.Warning, string.Format("We lost '{0}' him.\r\nException:{1}", clientId, ex));

                        RaiseLogEvent(LogType.Info, "SendMessageToClient Thread stopped.");
                        return;
                    }
                }
                else
                {
                    Thread.Sleep(1);
                }
                if (ct.IsCancellationRequested)
                {
                    break;
                }
            }
        }
예제 #2
0
        public void Process(ConnectorBase connector, Filter filter, string logFile, CancellationToken ct)
        {
            try
            {
                connector.Connect();
                RaiseLogEvent(LogType.Info, string.Format("Connected to Server"));
                Package package;

                if (logFile != null && logFile != string.Empty)
                {
                    RaiseLogEvent(LogType.Info, string.Format("Log file: {0}", logFile));
                    logFileFileStream   = new FileStream(logFile, FileMode.OpenOrCreate);
                    logFileStreamWriter = new StreamWriter(logFileFileStream);
                }
                bool first         = true;
                int  packagesCount = 0;
                var  sw            = new Stopwatch();

                while (true)
                {
                    Thread.Sleep(1);
                    while (connector.Receive(out package))
                    {
                        packagesCount++;
                        if (first)
                        {
                            sw.Start();
                            first = false;
                        }
                        try
                        {
                            bool processMessage = filter.applyFilter(package);
                            if (processMessage)
                            {
                                package.PrintToConsole(packagesCount, ConsoleColor.Green);

                                LogType infoLogType = LogType.Info;
                                switch (package.Method)
                                {
                                case Method.Response:
                                    infoLogType = LogType.InfoRed;
                                    break;

                                case Method.GET:
                                    infoLogType = LogType.InfoBlue;
                                    break;

                                case Method.POST:
                                    infoLogType = LogType.InfoGreen;
                                    break;

                                case Method.PUT:
                                    infoLogType = LogType.InfoYellow;
                                    break;

                                case Method.DELETE:
                                    infoLogType = LogType.InfoMagenta;
                                    break;

                                default:
                                    break;
                                }

                                RaiseLogEvent(infoLogType, string.Format("{0} {1}", packagesCount.ToString().PadRight(7), package.ToStringShort()));

                                switch (package.Method)
                                {
                                case Method.GET:
                                    ProcessGET(connector, package);
                                    Package eofPackage = new Package(senderId, package.Sender, 2, Method.Response, null);
                                    connector.Send(eofPackage);
                                    // TODO updates while sending hole list ?
                                    break;

                                case Method.POST:
                                    ProcessPOST(connector, package);
                                    Package postOKPackage = new Package(senderId, package.Sender, 1, Method.Response, null);
                                    //connector.Send(postOKPackage);

                                    break;

                                case Method.PUT:
                                    ProcessPUT(connector, package);
                                    Package putOKPackage = new Package(senderId, package.Sender, 1, Method.Response, null);
                                    //connector.Send(putOKPackage);
                                    break;

                                case Method.DELETE:
                                    ProcessDELETE(connector, package);
                                    Package deleteOKPackage = new Package(senderId, package.Sender, 1, Method.Response, null);
                                    //connector.Send(deleteOKPackage);
                                    break;

                                default:
                                    break;
                                }


                                if (logFileStreamWriter != null)
                                {
                                    logFileStreamWriter.WriteLine("{0}", package.ToString());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            RaiseLogEvent(LogType.Error, "TornadoNetList.Process() Process Message: " + ex.Message);
                            return;
                        }

                        if (ct.IsCancellationRequested)
                        {
                            break;
                        }
                    }
                    if (ct.IsCancellationRequested)
                    {
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                RaiseLogEvent(LogType.Error, "TornadoNetList.Process(): " + e.Message);
            }

            return;
        }