/// <summary> /// Handle TCP connections initiated by servants and receive data from the TCP pipe. /// </summary> /// <param name="AResult"></param> protected void RunServer(IAsyncResult AResult) { // Server connection TcpClient TcpConnection = null; // Accept connection between servant and proxy if (serverListener != null) { try { TcpConnection = serverListener.EndAcceptTcpClient(AResult); } catch { Log.Error("[PROXY->SERVANT]: Connection with servant failed. "); } } // Fill each SContext with information related to a particular servant and keep reading from the TCP pipe. ConnectReady.Release(); //decrease semaphore by 1 if (TcpConnection != null) { using (ServerContext SContext = new ServerContext(this, TcpConnection)) //each servant has its own context { onlineServers.Add(SContext); try { // When something has arrived in the TCP pipe, process it while (SContext.serverConnection.Connected) { if (SContext.ReceiveReady.Wait(TimeOut)) //wait until timeout { SContext.serverConnection.Client.BeginReceive(SContext.buffer, 0, SContext.buffer.Length, SContext.sflag, new AsyncCallback(DoReceive), SContext); } else { Log.Warn("[PROXY->SERVANT]: Game Server timed out. Disconnecting."); break; } } } catch { Log.Error("[PROXY->SERVANT]: Game Server Forcefully Disconnected 1."); onlineServers.Remove(SContext); } } //at this point, the connection with the servant has been lost, therefore ServerContext.Dispose() is called (because end of the using(){} block). } }
/// <summary> /// Handles the server request. If position, message.data is a ClientBroadcast object. /// </summary> /// <param name="server"></param> protected void HandleRequest(ServerContext server, List<string> commands) { int i = 0; foreach (string s in commands) { try { if (s != "") { ClientMessage message = Newtonsoft.Json.JsonConvert.DeserializeObject<ClientMessage>(s); if (message.Type == ResponseType.Position) { Proxy.broadcastToClients(message); } else if (message.Type == ResponseType.AllUsers) { Proxy.sendToClient(message); } else if (message.Type == ResponseType.Disconnect) { Proxy.broadcastToClients(message); } i++; } } catch (Exception e) { Log.Warn("[PROXY->SERVANT]: Error in GameServer.HandleRequest. JSON: " + commands[i] + ". Error is " + e.Message + "\n" + e.StackTrace); //Log.Error("Error parsing JSON in GameServer.HandleRequest",e); } } }