void netMessageService_ClientConnected(object sender, ServiceNetClient client) { using(var connectionMessagesHandler = new ServerConnectionMessagesHandler(client)) { if(!WaitHandshake(connectionMessagesHandler)) { if(Console != null) Console.WriteLine(MessageCategory.Warning, "Opponent handshake not received"); } else { ConnectionResult result; string refuseReason; ValidatePlayer(connectionMessagesHandler, out result, out refuseReason); HandshakeResponse handshakeResponse = new HandshakeResponse() { Deck = new DeckDataContract(deck), PlayerInfo = Player, ConnectionResult = result, }; Thread.Sleep(100); client.SendMessage(handshakeResponse); if(result != ConnectionResult.Accepted) { if(Console != null) Console.WriteLine(MessageCategory.Warning, string.Concat("Player ", connectionMessagesHandler.Handshake.PlayerInfo.NickName, " refused: ", refuseReason)); } else { if(!WaitPlayerConfirm(connectionMessagesHandler)) { if(Console != null) Console.WriteLine(MessageCategory.Warning, string.Concat("Player ", connectionMessagesHandler.Handshake.PlayerInfo.NickName, " hasn't confirmed the connection.")); } else { client.Disconnected += new EventHandler(client_Disconnected); lock(syncObject) connections.Add(client, connectionMessagesHandler); PlayerAccountData connectedPlayer = new PlayerAccountData() { Info = connectionMessagesHandler.Handshake.PlayerInfo, Deck = new DeckItem(connectionMessagesHandler.Handshake.Deck), Password = connectionMessagesHandler.Handshake.Password }; OnPlayerConnected(client, connectedPlayer); } } } } }
void client_MessageReceived(object sender, EventArgs e) { INetMessage msg = client.DequeueMessage(); if(msg is HandshakeResponse) { lock(syncObject) handshake = (HandshakeResponse)msg; } }