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"); }
// Remove connection from all collections public static void Remove(Connection connection) { var userID = connection.User.ID; var connectionID = connection.ConnectionID; Connection tempConnection; ConcurrentDictionary <Guid, Connection> tempUser; // Remove the connection from the list of all connections CONNECTIONS.TryRemove(connectionID, out tempConnection); // If there is no user associated with the connection it should not be in any other collections if (userID != null) { // Remove the connection from the list of connections for this user in USER_CONNECTIONS USER_CONNECTIONS[userID].TryRemove(connectionID, out tempConnection); // Check to see if this was the last connection for this user if (USER_CONNECTIONS[userID].IsEmpty) { // If this was the last connection for this user remove the user from USER_CONNECTIONS USER_CONNECTIONS.TryRemove(connection.User.ID, out tempUser); // Send diconnect message ServerTransmissionHandler.Send(connection.User.ID, $"------------------User: {connection.User.Name} disconnected ------------------"); // Log disconnect in console Console.WriteLine($"{connection.User.Name} (User) Disconnected"); } } }
private static void Rebroadcast(Tuple <Transmission, Connection> transmission) { Console.WriteLine($"{transmission.Item2.ConnectionID} (Connection): Processing Rebroadcast to {transmission.Item1.Message.ChatID} (Chat)"); ServerTransmissionHandler.Send(transmission.Item1.Message.ChatID, transmission.Item1); }