private void CallEvent_ServerStopped() { // Tell the upper layer that we are all finished with shutdown now. GameServerEventInfo info = new GameServerEventInfo(GameServerEventInfoCode.ServerStopped, "NetworkServer stopped."); this.gameStateUpdateEventHandler(info); }
private void CallEvent_LogMessage(string msg) { GameServerEventInfo info = new GameServerEventInfo(GameServerEventInfoCode.LogMessage, msg); this.gameStateUpdateEventHandler(info); }
// Call the GUI handler so it can redraw the graphics, score, etc. private void CallEvent_Update(GameState state) { GameServerEventInfo info = new GameServerEventInfo(GameServerEventInfoCode.StateUpdated, state.Copy()); this.gameStateUpdateEventHandler(info); }
// NetworkServer callback handler. private void ServerEventHandler(NetworkServerEventInfo info) { if (this.shutDown) // We have been told to shut down, don't start any new // connections now. return; if (info.Code == NetworkServerEventInfoCode.ClientConnected) { if (this.client1Id == -1) this.client1Id = info.ClientId; else this.client2Id = info.ClientId; // Send a Version message to the client. string managerVersion = this.GetType().Assembly.GetName().Version.ToString(); string gameName = GameChoice.CurrentGame.GetGameName(); string gameVersion = this.logic.GetType().Assembly.GetName().Version.ToString(); string gameConfig = ""; if (GameChoice.GameSupportsConfiguration(GameChoice.CurrentGame)) { // Get the configuration string for this game. gameConfig = ((IGameConfig)GameChoice.CurrentGame).GetConfigString(); } GameMessage.Version versionMsg = new GameMessage.Version(managerVersion, gameName, gameVersion, gameConfig); this.server.SendMessage(info.ClientId, versionMsg); // Call the upper layer's event handler to give it the good // news! GameServerEventInfo tinfo = new GameServerEventInfo(GameServerEventInfoCode.ClientConnected, "Remote Client ( " + this.server.GetClientRemoteEndPoint(info.ClientId).ToString() + " ) has connected."); this.gameStateUpdateEventHandler(tinfo); if (++this.connectionCount == 2) // This is connection 2! That means the game can begin! StartGame(); } else if (info.Code == NetworkServerEventInfoCode.ClientDisconnected) { // A client has disconnected. this.connectionCount--; if (info.ClientId == this.client1Id) this.client1Id = -1; else this.client2Id = -1; if (this.gameInProgress) { this.logic.GetGameState().SetGameOver(GetPlayerFromClientId(GetOtherClientId(info.ClientId)), GameOverCondition.OpponentDisconnected); EndGame(); } } else if (info.Code == NetworkServerEventInfoCode.LogMessage) { CallEvent_LogMessage(info.Message); } else if (info.Code == NetworkServerEventInfoCode.NewData) { // A new message has been received. // Split multiple messages up on carriage return and linefeed. string[] msgs = info.Message.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); // Add each new message to the read queue. foreach (string m in msgs) ProcessNewMessageFromClient(info.ClientId, m); } else if (info.Code == NetworkServerEventInfoCode.ServerStopped) { CallEvent_LogMessage("NetworkServer stopped."); } else { // Should never be here! new Exception("NetworkServer generated an event with code " + info.Code); } }
// Gets called by the server when we need to refresh the graphics. private void UpdateHandler(GameServerEventInfo info) { if (info.Code == GameServerEventInfoCode.StateUpdated) { this.gameStateQueue.Enqueue(info.State); if (info.State.GameIsOver) StopServer(); pictureBox1.Invalidate(); } else if (info.Code == GameServerEventInfoCode.ServerStopped) { // NetworkServer stopped, clients are disconnected. StopServer(); } if (info.Message != null) this.Display("> " + info.Message + "\n"); }