Ejemplo n.º 1
0
 private void TcpBroadcastDataToAllClients(Packets.Packet packet)
 {
     foreach (ConnectedClient c in connectedClients)
     {
         c.TcpSend(packet);
     }
 }
Ejemplo n.º 2
0
        public void TcpSend(Packets.Packet packet)
        {
            lock (writeLock)
            {
                MemoryStream memoryStream = new MemoryStream();

                formatter.Serialize(memoryStream, packet);

                byte[] buffer = memoryStream.GetBuffer();

                writer.Write(buffer.Length);
                writer.Write(buffer);
                writer.Flush();
            }
        }
Ejemplo n.º 3
0
        private void UdpListen()
        {
            try
            {
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);
                while (true)
                {
                    BinaryFormatter formatter      = new BinaryFormatter();
                    byte[]          bytes          = udpListener.Receive(ref endPoint);
                    MemoryStream    memoryStream   = new MemoryStream(bytes);
                    Packets.Packet  recievedPacket = formatter.Deserialize(memoryStream) as Packets.Packet;

                    foreach (ConnectedClient client in connectedClients)
                    {
                        udpListener.Send(bytes, bytes.Length, client.endPoint);
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Server UDP Read Method Exception: " + e.Message);
            }
        }
Ejemplo n.º 4
0
 private void TcpSendDataToSpecificClient(ConnectedClient client, Packets.Packet packet)
 {
     client.TcpSend(packet);
 }
Ejemplo n.º 5
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;
            }
        }