Beispiel #1
0
        private void TCPServerResponse() //TCP Read is also here
        {
            while (tcpClientConnect)
            {
                int numberOfIncomingBytes;
                while ((numberOfIncomingBytes = reader.ReadInt32()) != 0)
                {
                    MemoryStream ms       = new MemoryStream();
                    byte[]       byteData = reader.ReadBytes(numberOfIncomingBytes);

                    ms.Write(byteData, 0, byteData.Length);
                    ms.Position = 0;

                    Packet packet = binaryFormatter.Deserialize(ms) as Packet;
                    Console.WriteLine("Packet Deserialized");
                    switch (packet.type)
                    {
                    case PacketType.CHATMESSAGE:
                        ChatMessagePacket chatPacket = (ChatMessagePacket)packet;
                        Console.WriteLine(chatPacket.message);
                        messageForm.UpdateChatWindow(chatPacket.message);
                        break;

                    case PacketType.LOGIN:
                        Console.WriteLine("Login packet Created");
                        LoginPacket tcpLoginPacket = (LoginPacket)packet;
                        udpClient.Client.Connect(tcpLoginPacket.endPoint);
                        tUdpClientMethod = new Thread(UDPServerResponse);
                        tUdpClientMethod.Start();
                        udpClientConnect = true;
                        break;
                        // Private Messaging
                        //case PacketType.PRIVATEMESSAGEREQUEST:
                        //    Console.WriteLine("Pm Recieved");
                        //    PrivateMessageRequest privateMessagePack = (PrivateMessageRequest)packet;
                        //    PrivateMessageMain();
                        //    break;
                        //case PacketType.PRIVATEMESSAGETOSEND:
                        //    PrivateMessageToSend privateMessageToSend = (PrivateMessageToSend)packet;
                        //    pmForm.UpdatePrivateMessageChatWindow(privateMessageToSend.privateMessageToSend);
                        //    break;
                    }
                }
            }
        }
Beispiel #2
0
        public void ProcessServerResponse()
        {
            int numOfIncomingBytes = 0;

            while ((numOfIncomingBytes = reader.ReadInt32()) != 0)
            {
                byte[] buffer = reader.ReadBytes(numOfIncomingBytes);
                memoryStream.Write(buffer, 0, numOfIncomingBytes);
                memoryStream.Position  = 0;
                binaryFormatter.Binder = new MyBinder.MyBinder();
                PacketData.Packet rawPacket = (PacketData.Packet)binaryFormatter.Deserialize(memoryStream);
                memoryStream.SetLength(0);

                switch (rawPacket.type)
                {
                case PacketData.PacketType.CHATMESSAGE:
                    PacketData.ChatMessagePacket packet = (PacketData.ChatMessagePacket)rawPacket;
                    messageForm.UpdateChatWindow(packet.message);
                    break;

                case PacketData.PacketType.GAME:
                    startSnakeGame();
                    break;

                case PacketData.PacketType.SNAKEGAME:
                    Socket udpSocket = udpClient.Client;
                    PacketData.LoginPacket loginpacket = new PacketData.LoginPacket((IPEndPoint)udpSocket.LocalEndPoint);
                    TCPSend(loginpacket);
                    PacketData.GameMovement gamemovement = (PacketData.GameMovement)rawPacket;
                    snakeGame.UpdateSnake(gamemovement.input);
                    break;

                case PacketData.PacketType.SCORE:
                    PacketData.Score score = (PacketData.Score)rawPacket;
                    snakeGame.UpdateScore(score.input);
                    break;

                case PacketData.PacketType.APPLEPOS:
                    PacketData.ApplePos pos = (PacketData.ApplePos)rawPacket;
                    snakeGame.SetApplePos(pos.pos);
                    break;

                case PacketData.PacketType.PLAYER:
                    PacketData.Player player = (PacketData.Player)rawPacket;
                    snakeGame.startingText(player.player);
                    break;

                case PacketData.PacketType.STOPGAME:
                    snakeGame.Close();
                    break;

                case PacketData.PacketType.QUIT:

                    TCPStop();
                    //PacketData.QuitPacket quitPacket = (PacketData.QuitPacket)rawPacket;
                    //messageForm.UpdateChatWindow(quitPacket.message);
                    break;

                default:

                    break;
                }
            }
        }
Beispiel #3
0
        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;
            }
        }