Example #1
0
 public bool setKeyWithString(string input)
 {
     byte[] bytes = SharedEncoding.encodeString(input);
     if (bytes.Length == 16)
     {
         this.key = bytes;
         return(true);
     }
     else
     {
         return(false);
     }
 }
Example #2
0
        public void sendMessage(BroadcastRequest request)
        {
            byte[] packet = SharedEncoding.encryptBroadcastRequest(request, config.key);

            if (stream != null)
            {
                try
                {
                    stream.WriteAsync(packet, 0, packet.Length);
                }
                catch (Exception e)
                {
                    MessageBox.Show("ERROR: Cannot send message");
                }
            }
            else
            {
                MessageBox.Show("ERROR: Cannot send message");
            }
        }
Example #3
0
        public void disconnect()
        {
            if (stream != null && client != null)
            {
                DisconnectionRequest request = new DisconnectionRequest()
                {
                    userName = config.userName
                };
                byte[] packet = SharedEncoding.encodeDisconnectionRequest(request);

                try
                {
                    stream.Write(packet, 0, packet.Length);
                }
                finally
                {
                    ClientEvents.messageSent -= sendMessage;
                    client.Close();
                }
            }
        }
Example #4
0
        private bool connect()
        {
            if (config == null || config.serverIP == default(string) || config.userName == default(string))
            {
                MessageBox.Show("Cannot connect to server without proper configuration");

                return(false);
            }
            else
            {
                try
                {
                    client = new TcpClient(config.serverIP, 2056);
                    stream = client.GetStream();

                    ConnectionRequest connReq = new ConnectionRequest {
                        userName = config.userName
                    };
                    byte[] connReqPacket = SharedEncoding.encodeConnectionRequest(connReq);

                    stream.Write(connReqPacket, 0, connReqPacket.Length);

                    ClientEvents.messageSent += sendMessage;

                    Thread connThread = new Thread(Main);
                    connThread.IsBackground = true;

                    connThread.Start();

                    return(true);
                }
                catch (Exception exe)
                {
                    MessageBox.Show(exe.ToString());

                    return(false);
                }
            }
        }
Example #5
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);
            }
        }
Example #6
0
 public string getKeyString()
 {
     return(SharedEncoding.decodeString(key));
 }
Example #7
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;
        }