private static void ProcessServerJoinRequest(Tuple <Transmission, Connection> transmission)
        {
            Console.WriteLine($"{transmission.Item2.ConnectionID} (Connection): Processing Join Request");

            var connection = transmission.Item2;
            var userID     = transmission.Item1.SenderID.ToByteArray();

            // Update connection User data
            // TODO: Set name if already exists in DB
            connection.User = new User(userID, (new Guid(userID.Skip(16).Take(16).ToArray())).ToString());

            // Check to see if the User is currently connected on any other Connections
            var firstConnection = !ServerConnections.USER_CONNECTIONS.ContainsKey(userID);

            // Add Connection to relevant collections
            ServerConnections.Add(connection);

            // Send join message if this is the first Connection for this User
            if (firstConnection)
            {
                ServerTransmissionHandler.Send(userID, $"-------------------User: {connection.User.Name} has joined--------------------");
            }

            // Update Connection to show succesful join
            connection.Joined = true;
            // Send success Response
            ServerTransmissionHandler.SendResponse(connection, Errors.Error.NoError);
            // Send success ServerMessage
            ServerTransmissionHandler.Send(connection, "SERVER: Join Successful");
            // Send user count ServerMessage
            ServerTransmissionHandler.Send(connection, $"SERVER: {ServerConnections.USER_CONNECTIONS.Keys.Count} users currently online");
        }
Example #2
0
        private static void LookForConnections()
        {
            var tcpListener = new TcpListener(LOCAL_ADDRESS, PORT);

            try
            {
                tcpListener.Start(1000);
                //UICONTROLLER.Display(new Message(SERVER_USER, "Starting up..."));
                while (true)
                {
                    try
                    {
                        var connection = new Connection(tcpListener.AcceptTcpClient());
                        Console.WriteLine($"{connection.ConnectionID} (Connection): Connection Established");
                        connection.SessionKey = Encryption.DiffieHellman.GetSharedKey(connection.TCPClient);
                        Console.WriteLine($"{connection.ConnectionID} (Connection): Session Key Established");
                        ServerConnections.Add(connection);
                        new Task(() => { ServerTransmissionHandler.RecieveFrom(connection); }).Start();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Connection failed, Error: {0}", e);
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                tcpListener.Stop();
                //UICONTROLLER.Display(new Message(SERVER_USER, "Shutting down..."));
            }
        }
Example #3
0
 // Main loop to receive from a connection
 public static void RecieveFrom(Connection connection)
 {
     while (true)
     {
         try
         {
             // Attempt to add new Transmission to queue
             TransmissionsRecieved.Enqueue(new Tuple <Transmission, Connection>(Recieve(connection), connection));
             // Signal Transmission has been received
             transmissionsRecievedSignal.Set();
         }
         catch (System.IO.IOException)
         {
             // Remove connection when error occurs
             Console.WriteLine($"{connection.ConnectionID} (Connection): Disconnected (IOException)");
             ServerConnections.Remove(connection);
             return;
         }
         catch (System.Security.Cryptography.CryptographicException)
         {
             // Remove connection when error occurs
             Console.WriteLine($"{connection.ConnectionID} (Connection): Disconnected (CryptographicException)");
             ServerConnections.Remove(connection);
             return;
         }
     }
 }
        private static void ProcessChatJoinRequest(Tuple <Transmission, Connection> transmission)
        {
            var userID = transmission.Item1.SenderID.ToByteArray();
            var chatID = new Guid(transmission.Item1.Request.ChatJoinRequest.ChatID.ToByteArray());

            // TODO: Permissions
            if (!(ServerConnections.CHAT_USERS.ContainsKey(chatID) && ServerConnections.CHAT_USERS[chatID].ContainsKey(userID)))
            {
                ServerConnections.AddToChat(userID, chatID);
                ServerTransmissionHandler.SendResponse(transmission.Item2, Errors.Error.NoError);
                Console.WriteLine($"{transmission.Item2.User.Name} (User): Joined {chatID} (Chat)");
            }
            //TODO: Error processing
        }
Example #5
0
        public static void StartServer()
        {
            //UICONTROLLER.Display += (x) => { System.Console.WriteLine($"<{x.Author.Name}>: {x._message}"); };
            //System.Console.WriteLine(Dns.GetHostName());

            Task processTransmisisons = new Task(() => { ServerTransmissionHandler.OnTransmissionRecieved(); });

            processTransmisisons.Start();

            Task lookingForConenctions = new Task(LookForConnections);

            lookingForConenctions.Start();

            lookingForConenctions.Wait();
            processTransmisisons.Wait();

            foreach (var connection in ServerConnections.CONNECTIONS)
            {
                ServerConnections.Remove(connection.Value);
            }
        }