Beispiel #1
0
        private void TcpClientMethod(Client client)
        {
            try
            {
                Packet packet;
                while ((packet = client.TcpRead()) != null)
                {
                    if (packet != null)
                    {
                        switch (packet.packetType)
                        {
                        case PacketType.LOGIN:
                            Console.WriteLine("Server TCP 'Login' Packet Received");
                            LoginPacket loginPacket = (LoginPacket)packet;
                            clients[index - 1].endPoint  = loginPacket.EndPoint;
                            clients[index - 1].ClientKey = loginPacket.PublicKey;
                            client.TcpSend(new LoginPacket(null, client.PublicKey));
                            client.TcpSend(new PenPacket(startColor));
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                if (c.Value != client)
                                {
                                    c.Value.TcpSend(new PenPacket(Color.Black));
                                }

                                for (int i = 0; i < clientNames.Count; i++)
                                {
                                    if (i == 0)
                                    {
                                        c.Value.TcpSend(new EncryptedClientListPacket(c.Value.EncryptString(clientNames[i]),
                                                                                      BitConverter.GetBytes(true)));
                                    }
                                    else
                                    {
                                        c.Value.TcpSend(new EncryptedClientListPacket(c.Value.EncryptString(clientNames[i]),
                                                                                      BitConverter.GetBytes(false)));
                                    }
                                }

                                c.Value.TcpSend(new EncryptedAdminPacket(BitConverter.GetBytes(adminIsConnected)));
                            }
                            break;

                        case PacketType.ENCRYPTED_ADMIN:
                            Console.WriteLine("Server TCP 'Admin' Packet Received");
                            EncryptedAdminPacket adminPacket = (EncryptedAdminPacket)packet;
                            adminIsConnected = BitConverter.ToBoolean(adminPacket.adminConnected, 0);
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                c.Value.TcpSend(adminPacket);
                            }
                            break;

                        case PacketType.ENCRYPTED_CLIENT_LIST:
                            Console.WriteLine("Server TCP 'Client List' Packet Received");
                            EncryptedClientListPacket clientListPacket = (EncryptedClientListPacket)packet;
                            string clientListName = client.DecryptString(clientListPacket.name);
                            bool   clientListBool = BitConverter.ToBoolean(clientListPacket.removeText, 0);
                            if (!clientListBool)
                            {
                                clientNames.Add(clientListName);
                            }
                            else
                            {
                                clientNames.Remove(clientListName);
                            }
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                for (int i = 0; i < clientNames.Count; i++)
                                {
                                    if (i == 0)
                                    {
                                        c.Value.TcpSend(new EncryptedClientListPacket(c.Value.EncryptString(clientNames[i]),
                                                                                      BitConverter.GetBytes(true)));
                                    }
                                    else
                                    {
                                        c.Value.TcpSend(new EncryptedClientListPacket(c.Value.EncryptString(clientNames[i]),
                                                                                      BitConverter.GetBytes(false)));
                                    }
                                }
                            }
                            break;

                        case PacketType.PEN:
                            Console.WriteLine("Server TCP 'Pen' Packet Received");
                            PenPacket penPacket = (PenPacket)packet;
                            startColor = penPacket.penColor;
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                c.Value.TcpSend(penPacket);
                            }
                            break;

                        case PacketType.ENCRYPTED_ENABLE_PAINTING:
                            Console.WriteLine("Server TCP 'Enable Painting' Packet Received");
                            EncryptedEnablePaintingPacket enablePaintingPacket = (EncryptedEnablePaintingPacket)packet;
                            string enablePaintingString = client.DecryptString(enablePaintingPacket.playerName);
                            bool   enablePaintingBool   = BitConverter.ToBoolean(enablePaintingPacket.enablePainting, 0);
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                if (c.Value != client)
                                {
                                    c.Value.TcpSend(new EncryptedEnablePaintingPacket(c.Value.EncryptString(enablePaintingString),
                                                                                      BitConverter.GetBytes(enablePaintingBool)));
                                }
                            }
                            break;

                        case PacketType.ENCRYPTED_CLEAR_SINGLE:
                            Console.WriteLine("Server TCP 'Clear Single' Packet Received");
                            EncryptedClearSinglePacket clearSinglePacket = (EncryptedClearSinglePacket)packet;
                            string clearSingleString = client.DecryptString(clearSinglePacket.playerName);
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                if (c.Value != client)
                                {
                                    c.Value.TcpSend(new EncryptedClearSinglePacket(c.Value.EncryptString(clearSingleString)));
                                }
                            }
                            break;

                        case PacketType.CLEAR_GLOBAL:
                            Console.WriteLine("Server TCP 'Clear Global' Packet Received");
                            ClearGlobalPacket clearGlobalPacket = (ClearGlobalPacket)packet;
                            foreach (KeyValuePair <int, Client> c in clients)
                            {
                                c.Value.TcpSend(clearGlobalPacket);
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Server TCP Read Method Exception: " + exception.Message);
            }
            finally
            {
                client.Close();
                clients.TryRemove(index, out client);
            }
        }
Beispiel #2
0
        private void TcpProcessServerResponse()
        {
            try
            {
                int numberOfBytes = 0;
                while ((numberOfBytes = reader.ReadInt32()) != -1)
                {
                    byte[]       buffer       = reader.ReadBytes(numberOfBytes);
                    MemoryStream memoryStream = new MemoryStream(buffer);
                    Packet       packet       = formatter.Deserialize(memoryStream) as Packet;
                    switch (packet.packetType)
                    {
                    case PacketType.LOGIN:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Login' Packet Received");
                        LoginPacket loginPacket = (LoginPacket)packet;
                        ServerKey = loginPacket.PublicKey;
                        break;

                    case PacketType.ENCRYPTED_ADMIN:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Admin' Packet Received");
                        EncryptedAdminPacket adminPacket = (EncryptedAdminPacket)packet;
                        paintForm.adminConnected = BitConverter.ToBoolean(adminPacket.adminConnected, 0);
                        break;

                    case PacketType.ENCRYPTED_CLIENT_LIST:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Client List' Packet Received");
                        EncryptedClientListPacket clientListPacket = (EncryptedClientListPacket)packet;
                        paintForm.UpdatePlayerList(DecryptString(clientListPacket.name), BitConverter.ToBoolean(clientListPacket.removeText, 0));
                        break;

                    case PacketType.PEN:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Pen' Packet Received");
                        PenPacket penPacket = (PenPacket)packet;
                        paintForm.UpdatePen(penPacket.penColor);
                        break;

                    case PacketType.ENCRYPTED_ENABLE_PAINTING:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Enable Painting' Packet Received");
                        EncryptedEnablePaintingPacket enablePaintingPacket = (EncryptedEnablePaintingPacket)packet;
                        string enablePaintingString = DecryptString(enablePaintingPacket.playerName);
                        bool   enablePaintingBool   = BitConverter.ToBoolean(enablePaintingPacket.enablePainting, 0);
                        if (enablePaintingString == clientName)
                        {
                            paintForm.penEnabled = enablePaintingBool;
                            if (enablePaintingBool == true)
                            {
                                paintForm.UpdateServerWindow("Admin has enabled painting!", Color.Black, Color.SkyBlue);
                            }
                            else
                            {
                                paintForm.UpdateServerWindow("Admin has disabled painting!", Color.Black, Color.IndianRed);
                            }
                        }
                        break;

                    case PacketType.ENCRYPTED_CLEAR_SINGLE:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Clear Single' Packet Received");
                        EncryptedClearSinglePacket clearSinglePacket = (EncryptedClearSinglePacket)packet;
                        string clearSingleString = DecryptString(clearSinglePacket.playerName);
                        if (clearSingleString == clientName)
                        {
                            paintForm.UpdateServerWindow("Admin cleared the canvas!", Color.Black, Color.IndianRed);
                            paintForm.ClearCanvas();
                        }
                        break;

                    case PacketType.CLEAR_GLOBAL:
                        Console.WriteLine("Client [" + clientName + "] TCP 'Clear Global' Packet Received");
                        paintForm.UpdateServerWindow("Admin cleared the canvas!", Color.Black, Color.IndianRed);
                        paintForm.ClearCanvas();
                        break;
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Client TCP Read Method Exception: " + exception.Message);
            }
        }