public bool Start(string BindAddr, int nPort, E_ServerType ServType, int toPort = 0)
        {
            bool res = false;

            if (m_ListenerSock != null)
            {
                throw new Exception("AsyncServer::Trying to start server on socket which is already in use");
            }

            m_ServerType = ServType;

            m_ListenerSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                m_ListenerSock.Bind(new IPEndPoint(IPAddress.Parse(BindAddr), nPort));
                m_ListenerSock.Listen(5);

                m_AcceptInitThread = new Thread(AcceptInitThread);
                m_AcceptInitThread.Start();

                Program.echo(String.Format("Listen:[" + BindAddr + ":" + nPort + "] [{0}] => [" + FilterMain.cRemoteIP + ":" + toPort + "]", ServType.ToString()), "+");
            }
            catch (SocketException SocketEx)
            {
                Program.echo(String.Format("AsyncServer::Could not bind/listen/BeginAccept socket. Exception: {0}", SocketEx.ToString()), "-");
            }

            return(res);
        }
Ejemplo n.º 2
0
        public bool Start(string BindAddr, int nPort, E_ServerType ServType)
        {
            bool res = false;

            if (m_ListenerSock != null)
            {
                throw new Exception(FilterMain.FILTER + "Trying to start server on socket which is already in use");
            }

            m_ServerType = ServType;

            m_ListenerSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                m_ListenerSock.Bind(new IPEndPoint(IPAddress.Parse(BindAddr), nPort));
                m_ListenerSock.Listen(rnd.Next(1, 65535));

                m_AcceptInitThread = new Thread(AcceptInitThread);
                m_AcceptInitThread.Start();

                //Console.ForegroundColor = ConsoleColor.DarkCyan;
                //Console.WriteLine(FilterMain.FILTER + "FORWARDING COMPLETE " + IPAddress.Parse(BindAddr) + ":" + nPort);
                //Console.ResetColor();
            }
            catch (SocketException SocketEx)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "Could not bind/listen/BeginAccept socket. Exception: {0}", SocketEx.ToString());
                Console.ResetColor();
            }

            return(res);
        }
        void OnClientDisconnect(ref Socket ClientSock, E_ServerType HandlerType)
        {
            if (ClientSock == null)
            {
                return;
            }

            switch (HandlerType)
            {
            case E_ServerType.GatewayServer:
            {
                m_ClientCount_Gateway--;
                //Console.Title = string.Format("Client count [GatewayServer: {0}] [AgentServer: {1}]", GatewayClientCount, AgentClientCount);
            }
            break;

            case E_ServerType.AgentServer:
            {
                m_ClientCount_Agent--;
                //Console.Title = string.Format("Client count [GatewayServer: {0}] [AgentServer: {1}]", GatewayClientCount, AgentClientCount);
            }
            break;
            }

            try
            {
                ClientSock.Close();
            }
            catch (SocketException SocketEx)
            {
                Console.WriteLine("AsyncServer::OnClientDisconnect()::Error closing socket. Exception: {0}", SocketEx.ToString());
            }
            catch (ObjectDisposedException ObjDispEx)
            {
                Console.WriteLine("AsyncServer::OnClientDisconnect()::Error closing socket (socket already disposed?). Exception: {0}", ObjDispEx.ToString());
            }
            catch (Exception e)
            {
                Program.echo(String.Format("Exception: {0}", e.ToString()), "-");
            }


            ClientSock = null;
            GC.Collect();
        }
Ejemplo n.º 4
0
        void OnClientDisconnect(ref Socket ClientSock, E_ServerType HandlerType)
        {
            // Check
            if (ClientSock == null)
            {
                return;
            }

            switch (HandlerType)
            {
            case E_ServerType.GatewayServer:
            {
                //m_ClientCount_Gateway--;
                Console.Title = string.Format("Client count [GatewayServer: {0}] [AgentServer1: {1}] [AgentServer2: {2}]", FilterMain.gateway, FilterMain.agent1, FilterMain.agent2);
            }
            break;

            case E_ServerType.AgentServer:
            {
                //m_ClientCount_Agent--;
                //FilterMain.cur_players--;
                Console.Title = string.Format("Client count [GatewayServer: {0}] [AgentServer1: {1}] [AgentServer2: {2}]", FilterMain.gateway, FilterMain.agent1, FilterMain.agent2);
            }
            break;

            case E_ServerType.AgentServer2:
            {
                //m_ClientCount_Agent2--;
                //FilterMain.cur_players--;
                Console.Title = string.Format("Client count [GatewayServer: {0}] [AgentServer1: {1}] [AgentServer2: {2}]", FilterMain.gateway, FilterMain.agent1, FilterMain.agent2);
            }
            break;
            }

            try
            {
                ClientSock.Close();
            }
            catch (SocketException SocketEx)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "OnClientDisconnect()::Error closing socket. Exception: {0}", SocketEx.ToString());
                Console.ResetColor();
            }
            catch (ObjectDisposedException ObjDispEx)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "OnClientDisconnect()::Error closing socket (socket already disposed?). Exception: {0}", ObjDispEx.ToString());
                Console.ResetColor();
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "Something went wrong with Async systems.");
                Console.ResetColor();
            }


            ClientSock = null;
            GC.Collect();
        }