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"); }
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...")); } }