Esempio n. 1
0
        private void TcpProcessServerResponse()
        {
            Packets.Packet serverResponse;
            while ((serverResponse = TcpReadDataFromserver()) != null)
            {
                switch (serverResponse.m_PacketType)
                {
                case Packets.Packet.PacketType.ChatMessage:
                    Packets.ChatMessagePacket chatPacket = serverResponse as Packets.ChatMessagePacket;
                    clientForm.UpdateChatWindow(DecryptString(chatPacket.Message), Colors.Black);
                    break;

                case Packets.Packet.PacketType.PrivateMessage:
                    Packets.PrivateMessagePacket privateMessagePacket = serverResponse as Packets.PrivateMessagePacket;
                    clientForm.UpdateChatWindow(DecryptString(privateMessagePacket.Message), Colors.DeepPink);
                    break;

                case Packets.Packet.PacketType.Disconnect:
                    clientForm.ServerFullLogic();
                    Close();
                    break;

                case Packets.Packet.PacketType.KeyPacket:
                    Packets.KeyPacket keyPacket = serverResponse as Packets.KeyPacket;
                    serverKey = keyPacket.Key;
                    break;

                case Packets.Packet.PacketType.ClientList:
                    Packets.ClientsPacket clientsPacket = serverResponse as Packets.ClientsPacket;
                    clientForm.UpdateClientWindow(clientsPacket.Clients);
                    break;

                case Packets.Packet.PacketType.PictionaryChatMessage:
                    Packets.PictionaryChatMessagePacket pictionaryChatMessagePacket = serverResponse as Packets.PictionaryChatMessagePacket;
                    pictionaryForm.UpdateChatWindow(DecryptString(pictionaryChatMessagePacket.message), Colors.Black);
                    break;

                case Packets.Packet.PacketType.PictionarySetupClient:
                    Packets.PictionarySetupClientPacket pictionarySetupClientPacket = serverResponse as Packets.PictionarySetupClientPacket;
                    SetUpPictionary(pictionarySetupClientPacket.IsDrawer);
                    break;

                case Packets.Packet.PacketType.PictionaryWordToDraw:
                    Packets.PictionaryWordToDrawPacket pictionaryWordToDrawPacket = serverResponse as Packets.PictionaryWordToDrawPacket;
                    pictionaryForm.RecievedWordToDraw(DecryptString(pictionaryWordToDrawPacket.WordToDraw));
                    break;

                case Packets.Packet.PacketType.PictionaryClearCanvas:
                    pictionaryForm.ClearCanvas();
                    break;
                }
            }
        }
Esempio n. 2
0
        private void TcpProcessDataSentFromClient(Packets.Packet data, ConnectedClient client)
        {
            switch (data.m_PacketType)
            {
            case Packets.Packet.PacketType.Login:
                Packets.LoginPacket loginPacket = data as Packets.LoginPacket;
                client.Login(loginPacket.Nickname, loginPacket.Endpoint, loginPacket.PublicKey);
                PrintToConsoleAsLogMessage("[TCP] " + loginPacket.Nickname + " Joined the Server [" + loginPacket.Endpoint + "]");
                UpdateClientsOnlineBox();
                SendEncryptedChatPacket("[Server] " + connectedClients.Last().GetNickname() + " has joined the chat!");
                break;

            case Packets.Packet.PacketType.Nickname:
                Packets.NicknamePacket nicknamePacket = data as Packets.NicknamePacket;
                if (nicknamePacket.Name == client.GetNickname())
                {
                    TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Error] You Can't Change Your Name to the Same Name")));
                    return;
                }

                PrintToConsoleAsLogMessage("[TCP] [" + nicknamePacket.m_PacketType + "] from: " + client.GetNickname() + " data: " + nicknamePacket.Name);
                PrintToConsoleAsLogMessage("[TCP] [" + client.GetNickname() + "] Changed Name to " + nicknamePacket.Name);
                UpdateClientsOnlineBox();
                SendEncryptedChatPacket("[Server] " + client.GetNickname() + " Changed Name to " + nicknamePacket.Name);
                client.SetNickname(nicknamePacket.Name);
                break;

            case Packets.Packet.PacketType.ChatMessage:
                Packets.ChatMessagePacket messagePacket = data as Packets.ChatMessagePacket;
                string chatMessage = client.DecryptString(messagePacket.Message);
                SendEncryptedChatPacket("[" + client.GetNickname() + "] " + chatMessage);
                PrintToConsoleAsLogMessage("[TCP] [" + messagePacket.m_PacketType + "] from [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Message: " + chatMessage);
                break;

            case Packets.Packet.PacketType.PrivateMessage:
                Packets.PrivateMessagePacket privateMessagePacket = data as Packets.PrivateMessagePacket;

                foreach (ConnectedClient target in connectedClients)
                {
                    if (target.GetNickname().ToLower() == privateMessagePacket.TargetUser.ToLower())
                    {
                        if (target == client)
                        {
                            PrintToConsoleAsLogMessage("[TCP] [Error] [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Tried to send a Private Message to Themself");
                            TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Error] You Can't Message Yourself")));
                            return;
                        }

                        string privateMessage = client.DecryptString(privateMessagePacket.Message);
                        PrintToConsoleAsLogMessage("[TCP] [" + privateMessagePacket.m_PacketType + "] from [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] " +
                                                   "To [" + target.GetNickname() + " " + target.GetSocket().RemoteEndPoint + "] Message: " + privateMessage);

                        privateMessagePacket.Message = client.EncryptString("[To " + target.GetNickname() + "] " + privateMessage);
                        TcpSendDataToSpecificClient(client, privateMessagePacket);
                        privateMessagePacket.Message = target.EncryptString("[From " + client.GetNickname() + "] " + privateMessage);
                        TcpSendDataToSpecificClient(target, privateMessagePacket);
                        return;
                    }
                }

                PrintToConsoleAsLogMessage("[TCP] [Error] [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Tried to send a Private Message to a client that does not exist");
                TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Error] User not Found")));
                break;

            case Packets.Packet.PacketType.GameConnectionPacket:
                Packets.GameConnectionPacket gameConnectionPacket = data as Packets.GameConnectionPacket;

                if (gameConnectionPacket.GameToPlay == Packets.Packet.GameType.Pictionary)
                {
                    if (pictionaryLobby.Contains(client))
                    {
                        pictionaryLobby.Remove(client);
                        SendEncryptedChatPacket("[Server] " + client.GetNickname() + " has left the Pictionary Lobby [" + pictionaryLobby.Count + "|" + pictionaryLobbyMaxSize + "]");
                        if (playingPictionary)
                        {
                            foreach (ConnectedClient c in pictionaryLobby)
                            {
                                TcpSendDataToSpecificClient(c, new Packets.PictionaryChatMessagePacket(c.EncryptString("[Server] " + c.GetNickname() + " Has Left The Game!")));
                            }
                        }
                        if (pictionaryLobby.Count == 0)
                        {
                            playingPictionary = false;
                        }
                    }
                    else
                    {
                        if (pictionaryLobby.Count > pictionaryLobbyMaxSize - 1 || playingPictionary)
                        {
                            TcpSendDataToSpecificClient(client, new Packets.ChatMessagePacket(client.EncryptString("[Server] Server is Full")));
                        }
                        else
                        {
                            pictionaryLobby.Add(client);
                            SendEncryptedChatPacket("[Server] " + client.GetNickname() + " have joined the Pictionary Lobby [" + pictionaryLobby.Count + "|" + pictionaryLobbyMaxSize + "]");
                        }
                    }

                    if (pictionaryLobby.Count > pictionaryLobbyMaxSize - 1)
                    {
                        StartPictionaryRound();
                    }
                }
                break;

            case Packets.Packet.PacketType.Disconnect:
                PrintToConsoleAsLogMessage("[TCP] " + client.GetNickname() + " Left The Server. [" + client.GetSocket().RemoteEndPoint + "]");
                SendEncryptedChatPacket("[Server] " + client.GetNickname() + " has left the chat!");
                if (pictionaryLobby.Contains(client))
                {
                    SendEncryptedPictionartChatPacket("[Server] " + client.GetNickname() + " has Left the Game!");
                    SendEncryptedChatPacket("[Server] " + client.GetNickname() + " has left the Pictionary Lobby [" + pictionaryLobby.Count + "|" + pictionaryLobbyMaxSize + "]");
                    pictionaryLobby.Remove(client);
                }
                threads.RemoveAt(connectedClients.IndexOf(client));
                connectedClients.Remove(client);

                client.CloseConnection();
                UpdateClientsOnlineBox();
                break;

            case Packets.Packet.PacketType.PictionaryChatMessage:
                Packets.PictionaryChatMessagePacket pictionaryChatMessagePacket = data as Packets.PictionaryChatMessagePacket;
                string pictionaryChatMessage = client.DecryptString(pictionaryChatMessagePacket.Message);
                SendEncryptedPictionartChatPacket("[" + client.GetNickname() + "] " + pictionaryChatMessage);
                PrintToConsoleAsLogMessage("[TCP] [" + pictionaryChatMessagePacket.m_PacketType + "] from [" + client.GetNickname() + " " + client.GetSocket().RemoteEndPoint + "] Message: " + pictionaryChatMessage);

                if (pictionaryChatMessage.ToLower() == pictionaryCurrentWordBeingDrawn)
                {
                    pictionaryScores[pictionaryLobby.IndexOf(client)]++;
                    SendEncryptedPictionartChatPacket("[Server] " + client.GetNickname() + " Guessed The Word!");
                    SendEncryptedPictionartChatPacket("[Server] New Round!");

                    foreach (ConnectedClient c in pictionaryLobby)
                    {
                        TcpSendDataToSpecificClient(c, new Packets.PictionaryClearCanvasPacket());
                    }
                    StartPictionaryRound();
                }
                break;
            }
        }