void OnDataAvailable(TcpServerConnection connection) { NetworkStream stream = connection.Socket.GetStream(); ClientRequest req = ClientRequest.ReceiveFrom(stream); ServerResponse resp = ProcessRequest(req); resp.SendTo(stream); }
void OnConnect(TcpServerConnection connection) { NetworkStream stream = connection.Socket.GetStream(); ClientRequest req = null; try { req = ClientRequest.ReceiveFrom(stream); } catch { return; } if (req.Purpose == RequestPurpose.Login) { if (FreeSlots > 0 && CurrentGame.Phase == GamePhase.Lobby) { ServersideClientInfo client = new ServersideClientInfo(); client.Connection = connection; client.PlayerName = (string)req.Data[0]; client.CharacterToString = (string)req.Data[1]; client.ClientID = Clients.Count; Clients.Add(client); ServerResponse toSend = new ServerResponse(); toSend.Purpose = ResponseType.LoginAccepted; toSend.Data = new object[] { client.ClientID, ClientsideServerInfo.FromServer(this) }; toSend.SendTo(stream); } else { ServerResponse toSend = new ServerResponse(); toSend.Purpose = ResponseType.ActionDenied; toSend.Data = new object[] { "" }; if (FreeSlots == 0) { toSend.Data[0] = "Server full"; } else { toSend.Data[0] = "Game in progress"; } toSend.SendTo(stream); server.Connections.Remove(connection); } } }