private void UDPServerResponse() { try { while (udpClientConnect) { Packet udpReadPacket = UDPClientRead(); switch (udpReadPacket.type) { case PacketType.NICKNAMESCONNECTED: ConnectedNicknames connectedNicknamePack = (ConnectedNicknames)udpReadPacket; Console.WriteLine("Clients connected are " + connectedNicknamePack.nicknamesConnected); messageForm.UpdateClientListBox(connectedNicknamePack.nicknamesConnected); break; case PacketType.GAMEINFORMATION: GameInfoUpdate gameInfoUpdatePacket = (GameInfoUpdate)udpReadPacket; mainGame.UpdateDictionaryInfo(gameInfoUpdatePacket.clientGameTankPacket); mainGame.ForcePaint(); break; case PacketType.GAMESPRITE: GameInfoSpriteUpdate gameInfoSpriteUpdate = (GameInfoSpriteUpdate)udpReadPacket; mainGame.UpdateSpriteInfo(gameInfoSpriteUpdate.cliengGameSpriteInfo); break; case PacketType.GAMEBOMBINFORMATION: GameInfoBombUpdate gameInfoBombUpdatePacket = (GameInfoBombUpdate)udpReadPacket; mainGame.UpdateBombDictionaryInfo(gameInfoBombUpdatePacket.clientGameBombPacket); mainGame.ForcePaint(); break; case PacketType.GAMEBOMBSPRITE: GameInfoBombSpriteUpdate gameInfoBombSpriteUpdate = (GameInfoBombSpriteUpdate)udpReadPacket; mainGame.UpdateBombSpriteInfo(gameInfoBombSpriteUpdate.cliengGameBombSpriteInfo); break; } } } catch (Exception e) { } }
private void HandlePacket(Packet packetFromServer) { switch (packetFromServer.type) { case PacketType.GAME: GamePacket pGamePacket = (GamePacket)packetFromServer; int selection = UpdateButtons(pGamePacket.x, pGamePacket.y); switch (selection) { case 1: noughtsAndCrosses.button1.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button1.Text = pGamePacket.text; noughtsAndCrosses.button1.Enabled = false; })); break; case 2: noughtsAndCrosses.button2.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button2.Text = pGamePacket.text; noughtsAndCrosses.button2.Enabled = false; })); break; case 3: noughtsAndCrosses.button3.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button3.Text = pGamePacket.text; noughtsAndCrosses.button3.Enabled = false; })); break; case 4: noughtsAndCrosses.button4.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button4.Text = pGamePacket.text; noughtsAndCrosses.button4.Enabled = false; })); break; case 6: noughtsAndCrosses.button6.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button6.Text = pGamePacket.text; noughtsAndCrosses.button6.Enabled = false; })); break; case 7: noughtsAndCrosses.button7.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button7.Text = pGamePacket.text; noughtsAndCrosses.button7.Enabled = false; })); break; case 8: noughtsAndCrosses.button8.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button8.Text = pGamePacket.text; noughtsAndCrosses.button8.Enabled = false; })); break; case 9: noughtsAndCrosses.button9.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button9.Text = pGamePacket.text; noughtsAndCrosses.button9.Enabled = false; })); break; case 10: noughtsAndCrosses.button10.Invoke(new MethodInvoker(delegate() { noughtsAndCrosses.button10.Text = pGamePacket.text; noughtsAndCrosses.button10.Enabled = false; })); break; } break; case PacketType.CHATMESSAGE: ChatMessagePacket messagePacket = (ChatMessagePacket)packetFromServer; _form.UpdateChatWindow(messagePacket.message); break; case PacketType.NICKNAME: NickNamePacket nicknamePacket = (NickNamePacket)packetFromServer; _form.UpdateChatWindow(nicknamePacket.nickName); break; case PacketType.ENDPOINT: LoginPacket serverLogInDetails = (LoginPacket)packetFromServer; _udpClient.Connect((IPEndPoint)serverLogInDetails.endPoint); _udpReaderThread = new Thread(UDPRead); _udpReaderThread.Start(); break; case PacketType.USERLIST: UserListPacket userListPacket = (UserListPacket)packetFromServer; nicknameList = userListPacket.userList; _form.UpdateClientListBox(userListPacket.userList); break; case PacketType.GAMEREQ: GameRequestPacket gameReqPacket = (GameRequestPacket)packetFromServer; int packetState = (int)gameReqPacket.requestState; switch (packetState) { case 0: _form.UpdateChatWindow("Game was declined!"); break; case 1: gameRecipient = gameReqPacket.recipient; gameSender = gameReqPacket.sender; _form.UpdateChatWindow("Game accepted!"); RequestGame(gameReqPacket.sender, 1, gameReqPacket.recipient); noughtsAndCrosses.Text = _form.clientNickName; noughtsAndCrosses.ShowDialog(); break; case 2: _form.CreateMessageBox(gameReqPacket.sender, gameReqPacket.recipient); break; } break; } }