private void OnConnected(IAsyncAction action, AsyncStatus target) { if (target == AsyncStatus.Completed) { this.connection.Connected(); this.writer = new DataWriter(this.socket.OutputStream); this.reader = new DataReader(this.socket.InputStream); NetworkMessage m = new NetworkMessage(); m.MessageCategory = SystemMessages.SystemPrefix; m.MessageType = SystemMessages.Connect; AuthenticationInfo auth = new AuthenticationInfo(); auth.Username = this.id; m.MessageContent = NetworkSerializer.Serialize(auth); this.SendMessage(m); this.reader.InputStreamOptions = InputStreamOptions.Partial; DataReaderLoadOperation operation = this.reader.LoadAsync(256); operation.Completed = new AsyncOperationCompletedHandler<uint>(this.ReaderHasData); } }
void OnConnect(SocketAsyncEventArgs e) { if (e.SocketError != SocketError.Success) { ViewModelDispatcher.BeginInvoke(new Action(() => { MessageBox.Show("Socket error:" + e.SocketError.ToString()); })); return; } e.Completed -= new EventHandler<SocketAsyncEventArgs>(e_Completed); NetworkMessage message = new NetworkMessage(); message.MessageCategory = SystemMessages.SystemPrefix; message.MessageType = SystemMessages.Connect; AuthenticationInfo auth = new AuthenticationInfo(); auth.Username = this.id; message.MessageContent = NetworkSerializer.Serialize(auth); this.connection.Connected(); SendMessage(message); ReceiveMessage(); }
public void HandleMessage(NetworkMessage message) { lock (this.statusLock) { if (Server.EnableLogging) { string log = "Received"; if (this.user != null && this.user.Name != null) { log += " from " + this.user.Name; } log += ": "; log += message.ToString(); Console.WriteLine(log); } if (message.MessageCategory == SystemMessages.SystemPrefix) { switch (message.MessageType) { case SystemMessages.Connect: { ConnectInfo connectInfo = NetworkSerializer.Deserialize<ConnectInfo>(message.MessageContent); if (Server.EnableLogging) { Console.WriteLine(connectInfo.Username); } this.user = this.server.AddUser(connectInfo.Username, this); AuthenticationInfo auth = new AuthenticationInfo(); auth.Username = user.Name; auth.Authentication = user.Authentication; NetworkMessage authMessage = new NetworkMessage(); authMessage.MessageCategory = SystemMessages.SystemPrefix; authMessage.MessageType = SystemMessages.Authenticate; authMessage.MessageContent = NetworkSerializer.Serialize(auth); this.serverSocket.SendMessage(authMessage); this.Status = ConnectionStatus.Ready; } break; case SystemMessages.SendChat: { SendChatInfo chatInfo = NetworkSerializer.Deserialize<SendChatInfo>(message.MessageContent); this.server.AddChat(this.user, chatInfo.Chat); if (Server.EnableLogging) { Console.WriteLine("Receiving chat"); } } break; case SystemMessages.EnterLobby: { EnterLobbyInfo enterLobbyInfo = NetworkSerializer.Deserialize<EnterLobbyInfo>(message.MessageContent); Lobby lobby = null; if (this.server.Lobbies.TryGetValue(enterLobbyInfo.Lobby, out lobby)) { this.server.AddUserToLobby(this.user, lobby); } } break; case SystemMessages.Disconnect: { this.server.RemoveUserFromLobby(this.user, this.user.Lobby); this.server.RemoveUser(this.user); this.Status = ConnectionStatus.Exiting; if (Server.EnableLogging) { Console.WriteLine("Exiting"); } } break; case SystemMessages.RequestGame: { GameSpecificationInfo gameSpecificationInfo = NetworkSerializer.Deserialize<GameSpecificationInfo>(message.MessageContent); this.server.ProposeGameWithUsers(this, gameSpecificationInfo.Players, gameSpecificationInfo, this.user.Lobby); if (Server.EnableLogging) { Console.WriteLine("Proposing game with " + message.MessageContent); } } break; case SystemMessages.AcceptGame: { if (this.RespondedToGame != null) { this.RespondedToGame(new RespondToGameEventArgs(this, true)); } } break; case SystemMessages.DeclineGame: { if (this.RespondedToGame != null) { this.RespondedToGame(new RespondToGameEventArgs(this, false)); } } break; } } else if (message.MessageCategory == GameMessages.GamePrefix) { if (this.GameMessage != null) { this.GameMessage(new GameMessageEventArgs() { Message = message }); } } } }