示例#1
0
        protected override void Execute()
        {
            while (!_Stop)
            {
                // Accept an incoming control client connection
                if (_Listener.CanAccept(1000)) // 1 second
                {
                    try
                    {
                        RaiseMessageEvent("IPCSocketServer about to accept a client connection");
                        _ClientConnection = _Listener.AcceptTCP();
                        if (_ClientConnection != null)
                        {
                            string Request = _ClientConnection.ReadLn("\r\n", false, '\0', 1000);
                            if (Request == "OK?")
                            {
                                _ClientConnection.WriteLn("OK!");

                                RaiseMessageEvent("Control server accepted a client connection from " + _ClientConnection.GetRemoteIP() + ":" + _ClientConnection.GetRemotePort());

                                _Buffer.Length = 0;
                                while ((!_Stop) && (_ClientConnection.Connected))
                                {
                                    if (_ClientConnection.CanRead(1000))
                                    {
                                        ParseClientCommands(_ClientConnection.ReadString());
                                    }
                                }
                            }

                            if (_ClientConnection.Connected)
                            {
                                _ClientConnection.Close();
                            }
                        }
                        else
                        {
                            RaiseErrorMessageEvent("IPCSocketServer failed to accept a client connection");
                        }
                    }
                    catch (Exception ex)
                    {
                        RaiseExceptionEvent("Error in IPCSocketServerThread::Execute()", ex);
                    }
                }
            }
        }
示例#2
0
        protected override void Execute()
        {
            _Connection = new TcpConnection();
            if (_Connection.Listen(_Address, _Port))
            {
                RaiseBoundEvent();

                while (!_Stop)
                {
                    // Accept a new connection
                    if (_Connection.CanAccept(500)) // 1/2 of a second
                    {
                        TcpConnection NewConnection = _Connection.AcceptTCP();
                        if (NewConnection != null)
                        {
                            // Wait up to 5 seconds for the request string
                            string Request = NewConnection.ReadLn("\0", 5000);
                            if (Request.ToLower().Replace(" ", "") == "<policy-file-request/>")
                            {
                                ConnectionAcceptedEventArgs.Raise(this, ConnectionAcceptedEvent, _Address, _Port, NewConnection.GetRemoteIP(), NewConnection.GetRemotePort());
                                RaiseMessageEvent("Answered policy file request from " + NewConnection.GetRemoteIP() + ":" + NewConnection.GetRemotePort().ToString());

                                NewConnection.WriteLn("<?xml version=\"1.0\"?>");
                                NewConnection.WriteLn("<cross-domain-policy>");
                                NewConnection.WriteLn("   <allow-access-from domain=\"*\" to-ports=\"" + _AllowedPorts + "\"/>");
                                NewConnection.WriteLn("   <site-control permitted-cross-domain-policies=\"all\"/>"); // TODO Maybe add property to determine whether this should be all or master-only
                                NewConnection.WriteLn("</cross-domain-policy>");
                                NewConnection.Write("\0");
                            }
                            else
                            {
                                RaiseErrorMessageEvent("Invalid policy file request from " + NewConnection.GetRemoteIP() + ":" + NewConnection.GetRemotePort().ToString());
                            }
                            NewConnection.Close();
                        }
                    }
                }
                _Connection.Close();
            }
            else
            {
                RaiseErrorMessageEvent("Flash Socket Policy Thread: Unable to listen on " + _Address + ":" + _Port);
                RaiseBindFailedEvent();
            }
        }
        public static IPCSocketClientThread GetNewClient(string remoteAddress, int remotePort)
        {
            TcpConnection ClientConnection = new TcpConnection();

            if (ClientConnection.Connect(remoteAddress, remotePort))
            {
                ClientConnection.WriteLn("OK?");
                string Response = ClientConnection.ReadLn("\r\n", false, '\0', 1000);
                if (Response == "OK!")
                {
                    IPCSocketClientThread Result = new IPCSocketClientThread(ClientConnection);
                    Result.Start();
                    return(Result);
                }
            }

            return(null);
        }
        protected override void Execute()
        {
            while (!_Stop)
            {
Reconnected:

                try
                {
                    while ((!_Stop) && (_ClientConnection.Connected))
                    {
                        // Check for server message
                        if (_ClientConnection.CanRead(1000))
                        {
                            ParseServerMessages(_ClientConnection.ReadString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    RaiseExceptionEvent("Error in IPCSocketClientThread::Execute() while communicating", ex);
                }

                if (_ClientConnection.Connected)
                {
                    _ClientConnection.Close();
                }

                while (!_Stop)
                {
                    try
                    {
                        RaiseMessageEvent("IPSocketClient trying to reconnect...");
                        if (_ClientConnection.Connect(_RemoteIP, _RemotePort))
                        {
                            _ClientConnection.WriteLn("OK?");
                            string Response = _ClientConnection.ReadLn("\r\n", false, '\0', 1000);
                            if (Response == "OK!")
                            {
                                RaiseMessageEvent("IPSocketClient reconnected");
                                goto Reconnected;
                            }
                        }
                        else
                        {
                            RaiseMessageEvent("IPSocketClient failed to reconnect, trying again in 10 seconds...");
                        }
                    }
                    catch (Exception ex)
                    {
                        RaiseExceptionEvent("Error in IPCSocketClientThread::Execute() while connecting", ex);
                    }

                    for (int i = 0; i < 10; i++)
                    {
                        if (!_Stop)
                        {
                            break;
                        }
                        Thread.Sleep(1000);
                    }
                }
            }
        }