public async Task DisbandGame([GameCode] string gameCode) { int.TryParse(Context.Items[KeyConstants.GameCode] as string, out int gameId); await _gameService.DisbandGame(gameId); await Clients.Group(gameCode).DisbandGame("Game has been canceled."); ConnectionObserver.CleanConnectionGroup(gameCode); }
public override async Task OnDisconnectedAsync(Exception exception) { string connectionId = Context.ConnectionId; string groupName = ConnectionObserver.GetCurrentGroupName(connectionId); ConnectionObserver.ConnectionStates.Remove(Context.ConnectionId); if (groupName != null) { var playersList = ConnectionObserver.GetPlayersList(groupName); await Clients.Group(groupName).RefreshPlayersList(playersList); } await base.OnDisconnectedAsync(exception); }
public void It_should_open_connection() { var connectionInfo = CreateValidConnectionInfo(); var connection = new PLinkConnection(connectionInfo); var observer = new ConnectionObserver(); connection.Observer = observer; connection.Open(); WaitForExit(connection); observer.AppliedStates.Should().Contain(ConnectionState.Opening); observer.AppliedStates.Should().Contain(ConnectionState.Open); observer.AppliedStates.Should().Contain(ConnectionState.Closing); observer.AppliedStates.Should().Contain(ConnectionState.Closed); }
public void It_should_handle_invalid_hostname() { var connectionInfo = CreateValidConnectionInfo(); connectionInfo.HostName = "foo"; var connection = new PLinkConnection(connectionInfo); var observer = new ConnectionObserver(); connection.Observer = observer; connection.Open(); WaitForExit(connection); observer.FatalError.Should().Be.EqualTo("Unable to open connection: Host does not exist"); observer.AppliedStates.Should().Contain(ConnectionState.Opening); observer.AppliedStates.Should().Contain(ConnectionState.Closed); }
public void It_should_handle_invalid_password() { var connectionInfo = CreateValidConnectionInfo(); connectionInfo.Password = "******"; var connection = new PLinkConnection(connectionInfo); var observer = new ConnectionObserver(); connection.Observer = observer; connection.Open(); WaitForExit(connection); observer.FatalError.Should().Be.EqualTo("Access Denied"); observer.AppliedStates.Should().Contain(ConnectionState.Opening); observer.AppliedStates.Should().Contain(ConnectionState.Closing); observer.AppliedStates.Should().Contain(ConnectionState.Closed); }
GetConnectionObserver(Ice.ConnectionInfo c, Ice.Endpoint e, Ice.Instrumentation.ConnectionState s, Ice.Instrumentation.IConnectionObserver? old) { lock (this) { TestHelper.Assert(old == null || old is ConnectionObserver); if (connectionObserver == null) { connectionObserver = new ConnectionObserver(); connectionObserver.reset(); } return(connectionObserver); } }
GetConnectionObserver(ConnectionInfo c, Endpoint e, ConnectionState s, IConnectionObserver?old) { lock (this) { TestHelper.Assert(old == null || old is ConnectionObserver); if (connectionObserver == null) { connectionObserver = new ConnectionObserver(); connectionObserver.reset(); } return(connectionObserver); } }
public async Task ConnectToGame([GameCode] string gameCode, string nickname) { _logger.LogInformation($"Client with connection id: {Context.ConnectionId} is attempting connection using code: {gameCode} under nickname: {nickname}"); int.TryParse(Context.Items[KeyConstants.GameCode] as string, out int gameId); int?userId = Context.UserIdentifier != null ? (int?)int.Parse(Context.UserIdentifier) : null; var playerProfile = await _profileService.CreateOrUpdatePlayerProfile(new PlayerProfileCreateRequest { GameId = gameId, Nickname = nickname, UserId = userId }); await Clients.Caller.SendPlayerProfileId(playerProfile.Id); await Groups.AddToGroupAsync(Context.ConnectionId, gameCode); ConnectionObserver.ConnectionStates[Context.ConnectionId] = new PlayerEntry { Group = gameCode, PlayerProfileId = playerProfile.Id, Nickname = playerProfile.Nickname, }; await Clients.GroupExcept(gameCode, Context.ConnectionId).SendMessage($"Player {playerProfile.Nickname} has joined!", MessageType.Success); var playersList = ConnectionObserver.GetPlayersList(gameCode); await Clients.Group(gameCode).RefreshPlayersList(playersList); var items = await _itemService.GetItemsByGameId(gameId); await Clients.Caller.RefreshItemList(items); var categories = await _categoryService.GetCategoriesByGameId(gameId); await Clients.Caller.RefreshCategories(categories); if (_gameService.IsUserGameOwner(userId, gameId)) { await Clients.Caller.AllowGameControl((int)userId); await Groups.AddToGroupAsync(Context.ConnectionId, $"{gameCode}-master"); } await Clients.Group($"{gameCode}-master").RequestCurrentItemId(gameCode); await Clients.Caller.SendMessage("Game joined successfully", MessageType.Success); }
public async Task DisconnectFromGame([GameCode] string gameCode) { int.TryParse(Context.Items[KeyConstants.GameCode] as string, out int gameId); _logger.LogInformation($"Connection:{Context.ConnectionId} is leaving group {gameCode}"); await Groups.RemoveFromGroupAsync(Context.ConnectionId, gameCode); await Clients.GroupExcept(gameCode, Context.ConnectionId).SendMessage($"Player {Context.User.Identity.Name} has left.", MessageType.Warning); string connectionId = Context.ConnectionId; string groupName = ConnectionObserver.GetCurrentGroupName(connectionId); ConnectionObserver.ConnectionStates.Remove(Context.ConnectionId); if (groupName != null) { var playersList = ConnectionObserver.GetPlayersList(groupName); await Clients.Group(groupName).RefreshPlayersList(playersList); } }
private void setState(int state) { // // We don't want to send close connection messages if the endpoint // only supports oneway transmission from client to server. // if(_endpoint.datagram() && state == StateClosing) { state = StateClosed; } // // Skip graceful shutdown if we are destroyed before validation. // if(_state <= StateNotValidated && state == StateClosing) { state = StateClosed; } if(_state == state) // Don't switch twice. { return; } try { switch(state) { case StateNotInitialized: { Debug.Assert(false); break; } case StateNotValidated: { if(_state != StateNotInitialized) { Debug.Assert(_state == StateClosed); return; } break; } case StateActive: { // // Can only switch from holding or not validated to // active. // if(_state != StateHolding && _state != StateNotValidated) { return; } _threadPool.register(this, IceInternal.SocketOperation.Read); break; } case StateHolding: { // // Can only switch from active or not validated to // holding. // if(_state != StateActive && _state != StateNotValidated) { return; } if(_state == StateActive) { _threadPool.unregister(this, IceInternal.SocketOperation.Read); } break; } case StateClosing: case StateClosingPending: { // // Can't change back from closing pending. // if(_state >= StateClosingPending) { return; } break; } case StateClosed: { if(_state == StateFinished) { return; } _batchRequestQueue.destroy(_exception); _threadPool.finish(this); _transceiver.close(); break; } case StateFinished: { Debug.Assert(_state == StateClosed); _transceiver.destroy(); _communicator = null; break; } } } catch(Ice.LocalException ex) { _logger.error("unexpected connection exception:\n" + ex + "\n" + _transceiver.ToString()); } // // We only register with the connection monitor if our new state // is StateActive. Otherwise we unregister with the connection // monitor, but only if we were registered before, i.e., if our // old state was StateActive. // if(_monitor != null) { if(state == StateActive) { if(_acmLastActivity > -1) { _acmLastActivity = IceInternal.Time.currentMonotonicTimeMillis(); } _monitor.add(this); } else if(_state == StateActive) { _monitor.remove(this); } } if(_instance.initializationData().observer != null) { ConnectionState oldState = toConnectionState(_state); ConnectionState newState = toConnectionState(state); if(oldState != newState) { _observer = _instance.initializationData().observer.getConnectionObserver(initConnectionInfo(), _endpoint, newState, _observer); if(_observer != null) { _observer.attach(); } else { _writeStreamPos = -1; _readStreamPos = -1; } } if(_observer != null && state == StateClosed && _exception != null) { if(!(_exception is CloseConnectionException || _exception is ForcedCloseConnectionException || _exception is ConnectionTimeoutException || _exception is CommunicatorDestroyedException || _exception is ObjectAdapterDeactivatedException || (_exception is ConnectionLostException && _state >= StateClosing))) { _observer.failed(_exception.ice_id()); } } } _state = state; System.Threading.Monitor.PulseAll(this); if(_state == StateClosing && _dispatchCount == 0) { try { initiateShutdown(); } catch(LocalException ex) { setState(StateClosed, ex); } } }
public void updateObserver() { lock(this) { if(_state < StateNotValidated || _state > StateClosed) { return; } Debug.Assert(_instance.initializationData().observer != null); _observer = _instance.initializationData().observer.getConnectionObserver(initConnectionInfo(), _endpoint, toConnectionState(_state), _observer); if(_observer != null) { _observer.attach(); } else { _writeStreamPos = -1; _readStreamPos = -1; } } }