Пример #1
0
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                if (_Server == null)
                {
                    continue;
                }

                if (isServerStarted == false)
                {
                    continue;
                }

                ClientInfo[] clientList = _Server.GetClientList();
                foreach (ClientInfo client in clientList)
                {
                    AbstractTcpSocketClientHandler clientHandler = client.TcpSocketClientHandler;

                    if (clientHandler.Connected == false)
                    {
                        Trace.WriteLine(String.Format("Disconnected: {0}", clientHandler.GetHashCode().ToString()));
                        clientHandler.Close();
                    }
                }

                Thread.Sleep(5000);
            }
        }
Пример #2
0
        private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {
                if (_server == null)
                {
                    return;
                }

                ClientInfo[] clientList = _server.GetClientList();

                if (clientList.Length != 0)
                {
                    string containerListAsString = "";
                    foreach (string cn in _containerNames.Values.ToList())
                    {
                        if (cn.StartsWith("Unknown container")) // ignore container until it registers
                        {
                            continue;
                        }

                        containerListAsString += cn + " ";
                    }

                    foreach (ClientInfo ci in clientList)
                    {
                        if (_containerNames.ContainsKey(ci))
                        {
                            var m = new ContainerMessage("Server", _containerNames[ci], "Inform Containers", containerListAsString);
                            Send(ci, m);
                        }
                    }

                    DisplayContainerList();
                }
            }
            catch (Exception ex)
            {
                RaiseNewTextEvent("Exception in Server.TimerElapsed: " + ex.Message + ".");
            }
        }
Пример #3
0
        protected override void OnStart(string[] args)
        {
            //Assembly.LoadFrom(@"InterceptKeys.exe");
            //var instance = AppDomain.CurrentDomain.CreateInstance("InterceptKeys", "InterceptKeys.InterceptKeys");
            //var instance = Activator.CreateInstance(Type.GetType("InterceptKeys.InterceptKeys,InterceptKeys"));
            Task.Factory.StartNew(() =>
            {
                var pipe   = new Receiver();
                pipe.Data += (data) =>
                {
                    LogManager.Logger.Debug(string.Format("收到数据:{0}", string.Join(",", data)));
                };
                if (pipe.ServiceOn() == false)
                {
                    LogManager.Logger.Error(pipe.error);
                }

                //start socket server
                this.serverGuid = Guid.NewGuid();
                this.server     = new BasicSocketServer();
                this.server.ReceiveMessageEvent += (handler, message) =>
                {
                    BasicMessage receivedMessage = (BasicMessage)message;
                    byte[] buffer = receivedMessage.GetBuffer();
                    if (buffer.Length > 1000)
                    {
                        LogManager.Logger.Debug(string.Format("Received a long message of {0} bytes", receivedMessage.MessageLength), "Socket Server");
                        return;
                    }
                    string s = System.Text.ASCIIEncoding.Unicode.GetString(buffer);
                    LogManager.Logger.Debug(string.Format("Received Message:{0} from {1}", s, receivedMessage.ClientUID));
                    var client = server.GetClientList().FirstOrDefault(c => c.ClientUID == handler.ToString());
                    client.TcpSocketClientHandler.SendAsync(new BasicMessage(this.serverGuid, System.Text.ASCIIEncoding.Unicode.GetBytes("收到")));
                };
                this.server.ConnectionEvent += (handler) =>
                {
                    LogManager.Logger.Debug(string.Format("A client is connected to the server"), "Socket Server");
                };
                this.server.CloseConnectionEvent += (handler) =>
                {
                    LogManager.Logger.Debug(string.Format("A client is disconnected from the server"), "Socket Server");
                };
                this.server.Init(new IPEndPoint(IPAddress.Loopback, 8100));
                this.server.StartUp();

                ClosePreProcess();
                Process.Start(@"InterceptKeys.exe");
            });
        }