public void UpdateGame() { if (m_Game != null) { Packets.Packet P = m_Game.ReturnPacket(); switch (P.m_PacketType) { case Packets.PacketType.GameUpdate: { Packets.GameUpdate GameUpdate = (Packets.GameUpdate)P; if (!GameUpdate.Equals(m_CachedUpdate)) { m_CachedUpdate = GameUpdate; UdpSendMessage(P); } break; } case Packets.PacketType.GameFinished: { UdpSendMessage(P); break; } default: break; } } else { } }
//Processing server response private void UdpProccessServerResponse() { try { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { byte[] bytes = udpClient.Receive(ref endPoint); MemoryStream memoryStream = new MemoryStream(bytes); Packets.Packet recievedPacket = formatter.Deserialize(memoryStream) as Packets.Packet; switch (recievedPacket.m_PacketType) { case Packets.Packet.PacketType.PictionaryPaint: Packets.PictionaryPaintPacket paintPacket = recievedPacket as Packets.PictionaryPaintPacket; pictionaryForm.UpdatePaintCanvas(paintPacket.XPositions, paintPacket.YPositions, paintPacket.PenColor, paintPacket.SameLine); break; case Packets.Packet.PacketType.PictionaryClearCanvas: pictionaryForm.ClearCanvas(); break; } } } catch (SocketException e) { Console.WriteLine("Client UDP Read Method Exception: " + e.Message); } }
public void UdpSendMessage(Packets.Packet packets) { MemoryStream WMS = new MemoryStream(); m_Formatter.Serialize(WMS, packets); byte[] buffer = WMS.GetBuffer(); m_UDPClient.Send(buffer, buffer.Length); }
//Server Data management public void UdpSendDataToServer(Packets.Packet packet) { MemoryStream memoryStream = new MemoryStream(); formatter.Serialize(memoryStream, packet); byte[] buffer = memoryStream.GetBuffer(); udpClient.Send(buffer, buffer.Length); }
public void TcpSendDataToServer(Packets.Packet packet) { MemoryStream memoryStream = new MemoryStream(); formatter.Serialize(memoryStream, packet); byte[] buffer = memoryStream.GetBuffer(); writer.Write(buffer.Length); writer.Write(buffer); writer.Flush(); }
public void IncommingPacket(Packets.Packet packet) { switch (packet.m_PacketType) { case Packets.PacketType.GameUpdate: { m_NewestUpdate = (Packets.GameUpdate)packet; break; } } }
private void UdpProcessServerResponse() { try { IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { if (!m_Disconnect) { lock (m_UDPLock) { byte[] NewMessage = m_UDPClient.Receive(ref endPoint); MemoryStream RMS = new MemoryStream(NewMessage); Packets.Packet NewPackets = m_Formatter.Deserialize(RMS) as Packets.Packet; switch (NewPackets.m_PacketType) { case Packets.PacketType.GameUpdate: { if (m_Game != null) { m_Game.IncommingPacket(NewPackets); } break; } } } } } } catch (SocketException e) { if (m_Disconnect) { } else { Console.WriteLine("Client UDP Read Method exception: " + e.Message); } } }
public void Send(Packets.Packet NewMessage) { if (!m_Disconnect) { if (NewMessage.m_PacketType == Packets.PacketType.PrivateMessage) { Packets.PrivateMessage PM = (Packets.PrivateMessage)NewMessage; PM.m_Message = Encrpt(PM.m_Message); NewMessage = PM; } if (NewMessage.m_PacketType == Packets.PacketType.UserNameMessage) { Packets.UserNamePacket UP = (Packets.UserNamePacket)NewMessage; m_Nickname = UP.m_Username; } MemoryStream WMS = new MemoryStream(); m_Formatter.Serialize(WMS, NewMessage); byte[] buffer = WMS.GetBuffer(); m_Writer.Write(buffer.Length); m_Writer.Write(buffer); m_Writer.Flush(); } }
public void ProcessServerResponse() { Packets.Packet NewResponse = Read(); switch (NewResponse.m_PacketType) { case Packets.PacketType.ChatMessage: { Packets.ChatMessagePacket CMP = (Packets.ChatMessagePacket)NewResponse; m_ClientForm.UpdateChatWindow(CMP.m_Message); break; } case Packets.PacketType.ErrorMessage: { Packets.ErrorMessagePacket EM = (Packets.ErrorMessagePacket)NewResponse; m_ClientForm.GiveError(EM.m_Message); Console.WriteLine("Error: " + EM.m_Message); Close(); break; } case Packets.PacketType.AnnouncementMessage: { Packets.AnnouncementPacket AP = (Packets.AnnouncementPacket)NewResponse; bool UserName = AP.m_Announcement.Contains(" has joined the chat."); if (UserName) { string UN = AP.m_Announcement.Split()[0]; m_ClientForm.UpdateDropwDown(UN); } m_ClientForm.GiveAnnouncement(AP.m_Announcement); break; } case Packets.PacketType.ClientData: { Packets.ClientData CD = (Packets.ClientData)NewResponse; if (CD.m_ClientNames != null) { for (int i = 0; i < CD.m_ClientNames.Count(); i++) { m_ClientForm.UpdateDropwDown(CD.m_ClientNames[i]); } } break; } case Packets.PacketType.ConversationPacket: { Packets.ConversationPackets CP = (Packets.ConversationPackets)NewResponse; foreach (Packets.Packet message in CP.Conversation) { switch (message.m_PacketType) { case Packets.PacketType.ChatMessage: { Packets.ChatMessagePacket CMP = (Packets.ChatMessagePacket)message; m_ClientForm.UpdateChatWindow(CMP.m_Message); break; } case Packets.PacketType.AnnouncementMessage: { Packets.AnnouncementPacket AP = (Packets.AnnouncementPacket)message; m_ClientForm.GiveAnnouncement(AP.m_Announcement); break; } case Packets.PacketType.PrivateMessage: { Packets.PrivateMessage PM = (Packets.PrivateMessage)message; if (m_View == PM.m_Who || PM.m_Who == m_Nickname) { m_ClientForm.UpdateChatWindow(DecyrptString(PM.m_Message)); } break; } default: { break; } } } break; } case Packets.PacketType.PrivateMessage: { Packets.PrivateMessage PM = (Packets.PrivateMessage)NewResponse; if (m_View == PM.m_Who || PM.m_Who == m_Nickname) { m_ClientForm.UpdateChatWindow(DecyrptString(PM.m_Message)); } break; } case Packets.PacketType.PublicKey: { Packets.PublicKeys PK = (Packets.PublicKeys)NewResponse; m_ServerKey = PK.m_PublicKey; break; } case Packets.PacketType.GameRequest: { Packets.GameRequest GR = (Packets.GameRequest)NewResponse; GameRequestUser = GR.m_From; m_ClientForm.GameInvite(); m_ClientForm.GiveAnnouncement("You have been invited to a game by " + GR.m_From + ". Do you Accept?"); break; } case Packets.PacketType.GameAccepted: { using (m_Game = new Game1()) { m_ManageGameUpdate.Start(); m_Game.Run(); } m_GameUpdate.Dispose(); break; } case Packets.PacketType.RPSSignal: { Packets.RPSSignal S = (Packets.RPSSignal)NewResponse; m_ClientForm.ReadyToRock(S.m_From); break; } default: { break; } } }