public void TriggerEvent_ServerStopped()
 {
     NetworkServerEventInfo info = new NetworkServerEventInfo(NetworkServerEventInfoCode.ServerStopped, "NetworkServer stopped.");
     QueueEvent(info);
 }
 public void TriggerEvent_LogMessage(string msg)
 {
     NetworkServerEventInfo info = new NetworkServerEventInfo(NetworkServerEventInfoCode.LogMessage, msg);
     QueueEvent(info);
 }
 public void TriggerEvent_NewData(NetworkServerClientConnection connection, string msg)
 {
     NetworkServerEventInfo info = new NetworkServerEventInfo(NetworkServerEventInfoCode.NewData, connection.Id, msg);
     QueueEvent(info);
 }
 public void TriggerEvent_LogMessage(NetworkServerClientConnection connection, string msg)
 {
     NetworkServerEventInfo info = new NetworkServerEventInfo(NetworkServerEventInfoCode.LogMessage, connection.Id, msg);
     QueueEvent(info);
 }
 public void TriggerEvent_ClientDisconnected(NetworkServerClientConnection connection)
 {
     NetworkServerEventInfo info = new NetworkServerEventInfo(NetworkServerEventInfoCode.ClientDisconnected, "Client " + connection.Id + " has disconnected.");
     QueueEvent(info);
 }
 public void TriggerEvent_ClientConnected(NetworkServerClientConnection connection)
 {
     NetworkServerEventInfo info = new NetworkServerEventInfo(NetworkServerEventInfoCode.ClientConnected, connection.Id, "Client " + connection.Id + " (" + connection.RemoteEndPoint.ToString() + ") has connected.");
     QueueEvent(info);
 }
 private void QueueEvent(NetworkServerEventInfo info)
 {
     lock (this.eventLock)
     {
         this.eventQ.Enqueue(info);
         Monitor.Pulse(this.eventLock);
     }
 }
        // 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);
            }
        }