public void AcceptConnection() { try { AcceptSockets.Add(MainSocket.Accept()); StatusReport = "Client " + AcceptSockets.Count + " Connected !!"; ClientConnected = true; Send = true; StatusReport.SerializePacket(); TotalNumberOfConnectedClients = AcceptSockets.Count; } catch (SocketException SocketEx) { if (SocketEx.SocketErrorCode != SocketError.WouldBlock) { StatusReport = SocketEx.ToString(); } } }
void Connect() { while (!MainSocket.Connected) { Thread.Sleep(100); try { MainSocket.Connect(ServerIPEndPoint); } catch (SocketException SocketEx) { if (SocketEx.SocketErrorCode != SocketError.WouldBlock) { StatusReport = SocketEx.ToString(); } } if (MainSocket.Connected) { StatusReport = "Game Client Connected to " + ServerIPEndPoint.ToString(); } } }
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()); } }
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); }
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(); }
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) { // 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(); }
//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(); } }