private void HandleConnectMessage(byte[] buffer, int senderID) { ConnectMessage message = PacketDecoder.Decode_ConnectMessage(buffer); peerList[senderID].Name = message.Name; //activeControl.AddPlayerToLobby(senderID, message.Name); }
private void HandleHostResponse(byte[] buffer) { HostResponse response = PacketDecoder.Decode_HostResponse(buffer); peerID = response.AssignedID; peerList.First(x => x.Key == 1).Value.Name = response.HostName; foreach (var test in peerList) { Console.WriteLine(test.Key); } //activeControl.AddPlayerToLobby(peerList[1]); }
private void HandleConnectToNewPeer(byte[] buffer) { NewPeer newPeer = PacketDecoder.Decode_NewConnectedPeer(buffer); Console.WriteLine("New player ip: {0}", newPeer.PeerAddress.ToString()); }
private void ReceiveCallback(IAsyncResult ar) { try { Socket peer = (Socket)ar.AsyncState; // Read data from the remote device. int bytesRead = peer.EndReceive(ar); Console.WriteLine("Read {0} bytes from peer: {1}", bytesRead, peerList.First(x => x.Value.Socket == peer).Key); int senderID = peerList.First(x => x.Value.Socket == peer).Key; if (bytesRead > 0) { // There might be more data, so store the data received so far. //response = (Encoding.ASCII.GetString(buffer, 0, bytesRead)); int type = BitConverter.ToInt32(buff, 0); MessageType msgType = (MessageType)type; Console.WriteLine("Read message: {0} from peer: {1}", msgType.ToString(), peerList.First(x => x.Value.Socket == peer).Key); switch (msgType) { case MessageType.ChatMessage: ChatMessage message = PacketDecoder.Decode_ChatMessage(buff); message.Player = senderID; message.Name = peerList[senderID].Name; Console.WriteLine("Message: {0}, from player {1}", message.Message, message.Player); activeControl.HandleCommand(message); //activeControl.PostChatMessage(message.Message, peerList[senderID].Name); break; case MessageType.HostResponse: HandleHostResponse(buff); break; case MessageType.ConnectMessage: HandleConnectMessage(buff, senderID); if (isHost) { var data = PacketBuilder.Build_HostResponse(peerList.First(x => x.Value.Socket == peer).Key, "Host"); peer.BeginSend(data, 0, data.Length, 0, SendCallback, peer); } break; case MessageType.NewPeer: HandleConnectToNewPeer(buff); break; case MessageType.PlayerMovement: activeControl.HandleCommand(PacketDecoder.Decode_PlayerMovement(buff)); break; case MessageType.Ready: activeControl.ReadySignal(PacketDecoder.Decode_Ready(buff)); break; case MessageType.DeployBomb: activeControl.HandleCommand(PacketDecoder.Decode_DeployBomb(buff)); break; } Array.Clear(buff, 0, buff.Length); // Get the rest of the data. peer.BeginReceive(buff, 0, buff.Length, 0, ReceiveCallback, peer); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }