Ejemplo n.º 1
0
        private void TCPProcessServerResponse()
        {
            Packet receivedPacket;

            while ((receivedPacket = TCPRead()) != null)
            {
                try
                {
                    switch (receivedPacket.EPacketType)
                    {
                    case PacketType.EMPTY:
                        EmptyPacket emptyPacket = (EmptyPacket)receivedPacket;
                        break;

                    case PacketType.CHATMESSAGE:
                        ChatMessagePacket chatMessagePacket = (ChatMessagePacket)receivedPacket;

                        //Write message to client console
                        Console.WriteLine(chatMessagePacket.Time + " " + UTF8.GetString(chatMessagePacket.OriginClient) + ": " + UTF8.GetString(chatMessagePacket.Message));

                        //Write message to client form
                        _clientForm.UpdateChatWindow(chatMessagePacket.Time + " " + UTF8.GetString(chatMessagePacket.OriginClient) + ": " + UTF8.GetString(chatMessagePacket.Message));
                        break;

                    case PacketType.PRIVATEMESSAGE:
                        PrivateMessagePacket privateMessagePacket = (PrivateMessagePacket)receivedPacket;

                        //Write pm to client console
                        Console.WriteLine(privateMessagePacket.Time + " [PM] " + UTF8.GetString(privateMessagePacket.OriginClient) + " -> " + UTF8.GetString(privateMessagePacket.PrivateMessage));

                        //Write message to client form
                        _clientForm.UpdateChatWindow(privateMessagePacket.Time + " [PM] " + UTF8.GetString(privateMessagePacket.OriginClient) + " -> " + UTF8.GetString(privateMessagePacket.PrivateMessage));
                        break;

                    case PacketType.SERVERMESSAGE:
                        ServerMessagePacket serverMessagePacket = (ServerMessagePacket)receivedPacket;

                        //Write server response to client console
                        Console.WriteLine(serverMessagePacket.Time + " [Server] -> " + UTF8.GetString(serverMessagePacket.Message));

                        //Write server response to client form
                        _clientForm.UpdateChatWindow(serverMessagePacket.Time + " [Server] -> " + UTF8.GetString(serverMessagePacket.Message));
                        break;

                    case PacketType.ANNOUNCEMESSAGE:
                        AnnouncementMessagePacket announcementMessagePacket = (AnnouncementMessagePacket)receivedPacket;

                        //Write announcement to client console
                        Console.WriteLine(announcementMessagePacket.Time + " " + "[Announcement] -> " + UTF8.GetString(announcementMessagePacket.Message));

                        //Write announcement to client form
                        _clientForm.UpdateChatWindow(announcementMessagePacket.Time + " " + "[Announcement] -> " + UTF8.GetString(announcementMessagePacket.Message));
                        break;

                    case PacketType.SERVERKEY:
                        ServerKeyPacket serverKeyPacket = (ServerKeyPacket)receivedPacket;
                        _serverKey      = serverKeyPacket.ServerKey;
                        successfulLogin = serverKeyPacket.Successful;
                        Console.WriteLine("Server Key Packet Received!");
                        break;

                    case PacketType.CLIENTLIST:
                        ClientListPacket clientListPacket = (ClientListPacket)receivedPacket;
                        _clientForm.RefreshClientList(clientListPacket.ClientList, clientListPacket.ClientIPS);
                        break;
                    }
                }
                catch (SocketException e)
                {
                    Console.WriteLine("[Error] " + e.Message + e.StackTrace);
                    break;
                }
            }
        }