void AcceptConnectionCallback(IAsyncResult iar)
        {
            Program.echo("Conexion recibida...");
            Socket ClientSocket = null;

            m_Waiter.Set();

            try
            {
                ClientSocket = m_ListenerSock.EndAccept(iar);
            }
            catch (SocketException SocketEx)
            {
                Console.WriteLine("AsyncServer::AcceptConnectionCallback()::SocketException while EndAccept. Exception: {0}", SocketEx.ToString());
            }
            catch (ObjectDisposedException ObjDispEx)
            {
                Console.WriteLine("AsyncServer::AcceptConnectionCallback()::ObjectDisposedException while EndAccept. Is server shutting down ? Exception: {0}", ObjDispEx.ToString());
            }

            try
            {
                switch (m_ServerType)
                {
                case E_ServerType.GatewayServer:
                {
                    new GatewayContext(ClientSocket, OnClientDisconnect);


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

                default:
                {
                    Console.WriteLine("AsyncServer::AcceptConnectionCallback()::Unknown server type");
                }
                break;
                }
            }
            catch (SocketException SocketEx)
            {
                Console.WriteLine("AsyncServer::AcceptConnectionCallback()::Error while starting context. Exception: {0}", SocketEx.ToString());
            }
        }
Ejemplo n.º 2
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();
        }
        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
        //asynchronous callback on connection accepted
        void AcceptConnectionCallback(IAsyncResult iar)
        {
            Socket ClientSocket = null;

            //AcceptInitThread sleeps...
            m_Waiter.Set();

            try
            {
                ClientSocket = m_ListenerSock.EndAccept(iar);
            }

            catch (SocketException SocketEx)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "AcceptConnectionCallback()::SocketException while EndAccept. Exception: {0}", SocketEx.ToString());
                Console.ResetColor();
            }
            catch (ObjectDisposedException ObjDispEx)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "AcceptConnectionCallback()::ObjectDisposedException while EndAccept. Is server shutting down ? Exception: {0}", ObjDispEx.ToString());
                Console.ResetColor();
            }

            try
            {
                switch (m_ServerType)
                {
                case E_ServerType.GatewayServer:
                {
                    //pass socket to gateway context handler
                    new GatewayContext(ClientSocket, OnClientDisconnect);
                    //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:
                {
                    //pass socket to agent context handler
                    new AgentContext(ClientSocket, OnClientDisconnect);
                    //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:
                {
                    //pass socket to agent context handler
                    new AgentContext2(ClientSocket, OnClientDisconnect);
                    //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;

                default:
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(FilterMain.FILTER + "AcceptConnectionCallback()::Unknown server type");
                    Console.ResetColor();
                    //Environment.Exit(0);
                }
                break;
                }
            }
            catch (SocketException SocketEx)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(FilterMain.FILTER + "AcceptConnectionCallback()::Error while starting context. Exception: {0}", SocketEx.ToString());
                Console.ResetColor();
            }
        }