public HubConnection CreateSignalRHubConnection(UserConnectionInfo userConnectionInfo) { return(new HubConnectionBuilder().WithUrl(userConnectionInfo.Url, option => { option.AccessTokenProvider = () => Task.FromResult(userConnectionInfo.AccessToken); }).Build()); }
private void AcceptCallback(IAsyncResult result) { UserConnectionInfo connection = new UserConnectionInfo(); try { // Завершение операции Accept Socket s = (Socket)result.AsyncState; connection.Socket = s.EndAccept(result); connection.Buffer = new byte[255]; lock (_connections) _connections.Add(connection); // Начало операции Receive и новой операции Accept connection.Socket.BeginReceive(connection.Buffer, 0, connection.Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), connection); _serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), result.AsyncState); } catch (SocketException exc) { CloseConnection(connection); txtmgr.ShowSystemMessage(ServerBox, "Socket exception: " + exc.SocketErrorCode); } catch (Exception exc) { CloseConnection(connection); txtmgr.ShowSystemMessage(ServerBox, "Exception: " + exc); } }
/// <summary> /// Removes the connection privately. /// </summary> /// <param name="userObjectIdentifier">The user object identifier.</param> /// <param name="connectionId">The connection identifier.</param> private void RemoveConnectionInternal(string userObjectIdentifier, string connectionId) { Debug.Assert(!string.IsNullOrWhiteSpace(userObjectIdentifier), "userObjectIdentifier is invalid."); Debug.Assert(!string.IsNullOrWhiteSpace(connectionId), "connectionId is invalid."); UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId); HashSet <UserConnectionInfo> userConnectionsSet; lock (this.lockObject) { // lock keyword cannot have await in its block. userConnectionsSet = Task.Run(async() => await this.GetCacheEntryAsync(userObjectIdentifier).ConfigureAwait(false)).Result; if (userConnectionsSet?.Any() ?? false) { if (userConnectionsSet.Count > 1) { _ = userConnectionsSet.Remove(userConnectionInfo); Task.Run(async() => await this.SetCacheEntryAsync(userObjectIdentifier, userConnectionsSet).ConfigureAwait(false)).Wait(); } else { Task.Run(async() => await this.distributedCache.RemoveAsync(userObjectIdentifier).ConfigureAwait(false)).Wait(); } } } }
public async Task SetUserConnectionInfoAsync(UserConnectionInfo userConnectionInfo) { _logger.LogInformation($"Setting current user to: {userConnectionInfo?.User?.DisplayName}."); await _localStorage.SetItem(KEY, userConnectionInfo); OnCurrentUserChanged(userConnectionInfo); }
public void SetConnectionInfo_WithValidInputs() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId); // Act this.userConnectionTracker.SetConnectionInfo(userOid, userConnectionInfo); this.userConnectionTracker.SetConnectionInfo(Guid.NewGuid().ToString(), new UserConnectionInfo("some")); var set = this.userConnectionTracker.RetrieveConnectionInfo(userOid); Assert.IsTrue(set.First().Equals(userConnectionInfo)); }
public void SetConnectionInfo_WithValidInputs() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId); this.distributedCacheMock.Setup(dc => dc.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult <byte[]>(null)); this.distributedCacheMock.Setup(dc => dc.SetAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>())).Returns(Task.CompletedTask); // Act this.userConnectionCacheTracker.SetConnectionInfo(userOid, userConnectionInfo); this.distributedCacheMock.Verify(dc => dc.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once); this.distributedCacheMock.Verify(dc => dc.SetAsync(It.IsAny <string>(), It.IsAny <byte[]>(), It.IsAny <DistributedCacheEntryOptions>(), It.IsAny <CancellationToken>()), Times.Once); }
public void GetUserConnectionIds_WithValidInputs() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; string applicationName = "Some Application"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId, applicationName); this.userConnectionTracker.SetConnectionInfo(userOid, userConnectionInfo); this.userConnectionTracker.SetConnectionInfo(userOid, new UserConnectionInfo("some", "Browser")); var connectionIds = this.userConnectionTracker.GetUserConnectionIds(userOid, applicationName); Assert.IsTrue(connectionIds.First().Equals(userConnectionInfo.ConnectionId)); Assert.IsTrue(connectionIds.Count() == 1); }
public void RemoveConnectionInfo_WithValidInputs() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId); HashSet <UserConnectionInfo> connectionSet = new HashSet <UserConnectionInfo>(); connectionSet.Add(userConnectionInfo); this.distributedCacheMock.Setup(dc => dc.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult <byte[]>(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(connectionSet)))); this.distributedCacheMock.Setup(dc => dc.RemoveAsync(userOid, It.IsAny <CancellationToken>())).Returns(Task.CompletedTask); // Act this.userConnectionCacheTracker.RemoveConnectionInfo(userOid, connectionId); this.distributedCacheMock.Verify(dc => dc.RemoveAsync(userOid, It.IsAny <CancellationToken>()), Times.Once); }
/// <inheritdoc /> public void SetConnectionApplicationName(string userObjectIdentifier, UserConnectionInfo userConnectionInfo) { this.logger.LogInformation($"Started {nameof(this.SetConnectionApplicationName)} method of {nameof(UserConnectionTracker)}."); if (string.IsNullOrWhiteSpace(userObjectIdentifier)) { throw new ArgumentException("The user object identifier is not specified.", nameof(userObjectIdentifier)); } if (userConnectionInfo == null) { throw new ArgumentNullException(nameof(userConnectionInfo)); } this.SetApplicationNameInternal(userObjectIdentifier, userConnectionInfo); }
public void SetConnectionApplicationName_WithValidInputs() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; string applicationName = "Some Application"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId); this.userConnectionTracker.SetConnectionInfo(userOid, userConnectionInfo); this.userConnectionTracker.SetConnectionInfo(Guid.NewGuid().ToString(), new UserConnectionInfo("some")); // Act this.userConnectionTracker.SetConnectionApplicationName(userOid, new UserConnectionInfo(connectionId, applicationName)); var set = this.userConnectionTracker.RetrieveConnectionInfo(userOid); Assert.IsTrue(set.First().ApplicationName.Equals(applicationName, StringComparison.OrdinalIgnoreCase)); }
public void GetUserConnectionIds_WithValidInputs() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; string applicationName = "Some Application"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId, applicationName); HashSet <UserConnectionInfo> userConnectionsSet = new HashSet <UserConnectionInfo>(); userConnectionsSet.Add(userConnectionInfo); userConnectionsSet.Add(new UserConnectionInfo("some", "Browser")); this.distributedCacheMock.Setup(dc => dc.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult <byte[]>(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(userConnectionsSet)))); var connectionIds = this.userConnectionCacheTracker.GetUserConnectionIds(userOid, applicationName); Assert.IsTrue(connectionIds.First().Equals(userConnectionInfo.ConnectionId)); Assert.IsTrue(connectionIds.Count() == 1); }
public void RectrieveConnection_WithValidUserObjectIndefier() { string connectionId = Guid.NewGuid().ToString(); string userOid = "Test Oid"; UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId, "Some App"); HashSet <UserConnectionInfo> connectionSet = new HashSet <UserConnectionInfo>(); connectionSet.Add(userConnectionInfo); this.distributedCacheMock.Setup(dc => dc.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())).Returns(Task.FromResult <byte[]>(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(connectionSet)))); // Act var set = this.userConnectionCacheTracker.RetrieveConnectionInfo(userOid); this.distributedCacheMock.Verify(dc => dc.GetAsync(It.IsAny <string>(), It.IsAny <CancellationToken>()), Times.Once); Assert.AreEqual(set.First().ConnectionId, connectionId); }
/// <summary> /// Adds the connection to the dictionary. /// </summary> /// <param name="userObjectIdentifier">The user object identifier.</param> /// <param name="userConnectionInfo">The instance of <see cref="UserConnectionInfo"/>..</param> private void AddConnection(string userObjectIdentifier, UserConnectionInfo userConnectionInfo) { Debug.Assert(!string.IsNullOrWhiteSpace(userObjectIdentifier), "UserObjectIdentifier is null."); Debug.Assert(userConnectionInfo != null, "The userConnectionInfo is null."); HashSet <UserConnectionInfo> userConnectionsSet; lock (this.lockObject) { if (!this.connections.TryGetValue(userObjectIdentifier, out userConnectionsSet)) { userConnectionsSet = new HashSet <UserConnectionInfo>(); this.connections[userObjectIdentifier] = userConnectionsSet; } _ = userConnectionsSet.Add(userConnectionInfo); } }
/// <summary> /// Sets the application name internal. /// </summary> /// <param name="userObjectIdentifier">The user object identifier.</param> /// <param name="userConnectionInfo">The instance of <see cref="UserConnectionInfo"/>..</param> private void SetApplicationNameInternal(string userObjectIdentifier, UserConnectionInfo userConnectionInfo) { HashSet <UserConnectionInfo> userConnectionsSet; lock (this.lockObject) { if (this.connections.TryGetValue(userObjectIdentifier, out userConnectionsSet)) { _ = userConnectionsSet.Remove(userConnectionInfo); } else { userConnectionsSet = new HashSet <UserConnectionInfo>(); _ = this.connections.TryAdd(userObjectIdentifier, userConnectionsSet); } _ = userConnectionsSet.Add(userConnectionInfo); } }
/// <summary> /// Adds the connection to the dictionary. /// </summary> /// <param name="userObjectIdentifier">The user object identifier.</param> /// <param name="userConnectionInfo">The instance of <see cref="UserConnectionInfo"/>.</param> private void AddConnection(string userObjectIdentifier, UserConnectionInfo userConnectionInfo) { Debug.Assert(!string.IsNullOrWhiteSpace(userObjectIdentifier), "UserObjectIdentifier is null."); Debug.Assert(userConnectionInfo != null, "The userConnectionInfo is null."); HashSet <UserConnectionInfo> userConnectionsSet; lock (this.lockObject) { // lock keyword cannot have await in its block. userConnectionsSet = Task.Run(async() => await this.GetCacheEntryAsync(userObjectIdentifier).ConfigureAwait(false)).Result; if (userConnectionsSet == null) { userConnectionsSet = new HashSet <UserConnectionInfo>(); } _ = userConnectionsSet.Add(userConnectionInfo); Task.Run(async() => await this.SetCacheEntryAsync(userObjectIdentifier, userConnectionsSet).ConfigureAwait(false)).Wait(); } }
/// <summary> /// Sets the application name internal. /// </summary> /// <param name="userObjectIdentifier">The user object identifier.</param> /// <param name="userConnectionInfo">The instance of <see cref="UserConnectionInfo"/>..</param> private void SetApplicationNameInternal(string userObjectIdentifier, UserConnectionInfo userConnectionInfo) { HashSet <UserConnectionInfo> userConnectionsSet; lock (this.lockObject) { // lock keyword cannot have await in its block. userConnectionsSet = Task.Run(async() => await this.GetCacheEntryAsync(userObjectIdentifier).ConfigureAwait(false)).Result; if (userConnectionsSet != null) { _ = userConnectionsSet.Remove(userConnectionInfo); } else { userConnectionsSet = new HashSet <UserConnectionInfo>(); } _ = userConnectionsSet.Add(userConnectionInfo); Task.Run(async() => await this.SetCacheEntryAsync(userObjectIdentifier, userConnectionsSet).ConfigureAwait(false)).Wait(); } }
/// <summary> /// Removes the connection privately. /// </summary> /// <param name="userObjectIdentifier">The user object identifier.</param> /// <param name="connectionId">The connection identifier.</param> private void RemoveConnectionInternal(string userObjectIdentifier, string connectionId) { Debug.Assert(!string.IsNullOrWhiteSpace(userObjectIdentifier), "userObjectIdentifier is invalid."); Debug.Assert(!string.IsNullOrWhiteSpace(connectionId), "connectionId is invalid."); UserConnectionInfo userConnectionInfo = new UserConnectionInfo(connectionId); HashSet <UserConnectionInfo> userConnectionsSet; lock (this.lockObject) { if (this.connections.TryGetValue(userObjectIdentifier, out userConnectionsSet) && userConnectionsSet.Any()) { if (userConnectionsSet.Count > 1) { _ = userConnectionsSet.Remove(userConnectionInfo); } else { _ = this.connections.TryRemove(userObjectIdentifier, out _); } } } }
private void CloseConnection(UserConnectionInfo user) { user.Socket.Close(); lock (_connections) _connections.Remove(user); }
protected virtual void OnCurrentUserChanged(UserConnectionInfo userConnectionInfo) { UserConnectionInfoChanged?.Invoke(userConnectionInfo); }