Exemplo n.º 1
0
        public void isBrodcastRequest_notGivenBrodcastRequest_returnsFalse()
        {
            byte[] buffer = new byte[1024];
            buffer[0] = RequestCodes.connect;

            bool result = SharedPacketTranslation.isBrodcastRequest(buffer);

            Assert.IsFalse(result);
        }
Exemplo n.º 2
0
        public void isBrodcastRequest_givenBrodcastRequest_returnsTrue()
        {
            byte[] buffer = new byte[1024];
            buffer[0] = RequestCodes.broadcastMessage;

            bool result = SharedPacketTranslation.isBrodcastRequest(buffer);

            Assert.IsTrue(result);
        }
Exemplo n.º 3
0
        private void Main()
        {
            while (client.Connected)
            {
                if (stream.DataAvailable)
                {
                    byte[] buffer = new byte[1024];
                    stream.Read(buffer, 0, buffer.Length);

                    if (SharedPacketTranslation.isBrodcastRequest(buffer))
                    {
                        BroadcastRequest request = SharedEncoding.decryptBroadcastRequest(buffer, config.key);
                        ClientEvents.invokeMessageRecived(request);
                    }
                    else if (SharedPacketTranslation.isNotificationRequest(buffer))
                    {
                        NotificationRequest notification = SharedEncoding.decodeNotificationRequest(buffer);
                        ClientEvents.invokeNotificationReceived(notification);
                    }
                }

                Thread.Sleep(100);
            }
        }
Exemplo n.º 4
0
        private void handleClient(TcpClient client)
        {
            Client user = null;

            while (client.Connected)
            {
                NetworkStream stream = client.GetStream();
                if (stream.DataAvailable)
                {
                    byte[] buffer = new byte[1024];
                    stream.Read(buffer, 0, buffer.Length);

                    if (SharedPacketTranslation.isConnectionRequest(buffer))
                    {
                        IPAddress clientIP = IPAddress.Parse(client.Client.RemoteEndPoint.ToString().Split(':')[0]);
                        string    username = SharedEncoding.decodeConnectionRequest(buffer).userName;

                        Client thisClient = null;
                        foreach (Client clide in clients)
                        {
                            if (clide.userName == username && clide.publicIP.ToString() == clientIP.ToString())
                            {
                                thisClient = clide;
                                break;
                            }
                        }

                        if (thisClient != null)
                        {
                            user = thisClient;

                            thisClient.tcpClient   = client;
                            thisClient.isConnected = true;

                            log.Add($"{user.userName} reconnected at: {DateTime.Now.ToString()}");
                        }
                        else
                        {
                            Client newClient = new Client()
                            {
                                userName = username, isConnected = true, publicIP = clientIP, tcpClient = client
                            };
                            user = newClient;

                            clients.Add(newClient);

                            log.Add($"{user.userName} connected at: {DateTime.Now.ToString()}");
                        }
                        sendMessageToAllConnectedClients(SharedEncoding.encodeNotificationRequest(new NotificationRequest()
                        {
                            message = $"{user.userName} has connected"
                        }));
                    }
                    else if (SharedPacketTranslation.isDisconnectRequest(buffer))
                    {
                        user.isConnected = false;
                        log.Add($"{user.userName} disconnected at: {DateTime.Now.ToString()}");
                        sendMessageToAllConnectedClients(SharedEncoding.encodeNotificationRequest(new NotificationRequest()
                        {
                            message = $"{user.userName} has disconnected"
                        }));
                    }
                    else if (SharedPacketTranslation.isBrodcastRequest(buffer))
                    {
                        sendMessageToAllConnectedClients(buffer);
                    }
                }

                Thread.Sleep(100);
            }
            user.isConnected = false;
        }