private static void ClientIsConnected(IMultiplayerSessionConnectionContext sessionConnectionContext) { if (!sessionConnectionContext.Client.IsConnected) { throw new InvalidOperationException("Клиент не подключен."); } }
public void JoinSessionShouldTransitionToSessionJoinedState() { // Arrange MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation( TestConstants.TEST_CORRELATION_ID, TestConstants.TEST_PLAYER_ID, TestConstants.TEST_RESERVATION_KEY); IClient serverClient = Substitute.For <IClient>(); serverClient.IsConnected.Returns(true); IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); connectionContext.Reservation.Returns(successfulReservation); connectionContext.Client.Returns(serverClient); SessionReserved connection = new SessionReserved(); // Act connection.JoinSession(connectionContext); // Assert connectionContext.Received().UpdateConnectionState(Arg.Any <SessionJoined>()); }
private static void ClientIsConnected(IMultiplayerSessionConnectionContext sessionConnectionContext) { if (!sessionConnectionContext.Client.IsConnected) { throw new InvalidOperationException("The client is not connected."); } }
private void ReservationPacketIsCorrelated(IMultiplayerSessionConnectionContext sessionConnectionContext) { if (!reservationCorrelationId.Equals(sessionConnectionContext.Reservation.CorrelationId)) { throw new UncorrelatedPacketException(sessionConnectionContext.Reservation, reservationCorrelationId); } }
public void JoinSessionShouldSendPlayerJoiningMultiplayerSessionPacket() { // Arrange MultiplayerSessionReservation successfulReservation = new MultiplayerSessionReservation( TestConstants.TEST_CORRELATION_ID, TestConstants.TEST_PLAYER_ID, TestConstants.TEST_RESERVATION_KEY); IClient client = Substitute.For <IClient>(); client.IsConnected.Returns(true); IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); connectionContext.Reservation.Returns(successfulReservation); connectionContext.Client.Returns(client); SessionReserved connectionState = new SessionReserved(); // Act connectionState.JoinSession(connectionContext); // Assert client.Received().Send(Arg.Any <PlayerJoiningMultiplayerSession>()); }
private void SessionPolicyPacketCorrelation(IMultiplayerSessionConnectionContext sessionConnectionContext) { if (!policyRequestCorrelationId.Equals(sessionConnectionContext.SessionPolicy.CorrelationId)) { throw new UncorrelatedPacketException(sessionConnectionContext.SessionPolicy, policyRequestCorrelationId); } }
private static void HandleReservation(IMultiplayerSessionConnectionContext sessionConnectionContext) { IMultiplayerSessionConnectionState nextState = sessionConnectionContext.Reservation.ReservationState == MultiplayerSessionReservationState.Reserved ? new SessionReserved() : new SessionReservationRejected() as IMultiplayerSessionConnectionState; sessionConnectionContext.UpdateConnectionState(nextState); }
public virtual void Disconnect(IMultiplayerSessionConnectionContext sessionConnectionContext) { sessionConnectionContext.ClearSessionState(); sessionConnectionContext.Client.Stop(); Disconnected newConnectionState = new Disconnected(); sessionConnectionContext.UpdateConnectionState(newConnectionState); }
private void RequestSessionReservation(IMultiplayerSessionConnectionContext sessionConnectionContext, string reservationCorrelationId) { IClient client = sessionConnectionContext.Client; PlayerSettings playerSettings = sessionConnectionContext.PlayerSettings; AuthenticationContext authenticationContext = sessionConnectionContext.AuthenticationContext; MultiplayerSessionReservationRequest requestPacket = new MultiplayerSessionReservationRequest(reservationCorrelationId, playerSettings, authenticationContext); client.Send(requestPacket); }
private static void HandleReservation(IMultiplayerSessionConnectionContext sessionConnectionContext) { IMultiplayerSessionConnectionState nextState = sessionConnectionContext.Reservation.ReservationState switch { MultiplayerSessionReservationState.RESERVED => new SessionReserved(), _ => new SessionReservationRejected(), }; sessionConnectionContext.UpdateConnectionState(nextState); }
private static void ReservationIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.Reservation); } catch (ArgumentNullException ex) { throw new InvalidOperationException("Контекст не имеет резервации.", ex); } }
private static void AuthenticationContextIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.AuthenticationContext); } catch (ArgumentNullException ex) { throw new InvalidOperationException("Контекст не содержит контекст аутентификации.", ex); } }
private static void PlayerSettingsIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.PlayerSettings); } catch (ArgumentNullException ex) { throw new InvalidOperationException("Контекст не содержит настроек игрока.", ex); } }
private static void SessionPolicyIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.SessionPolicy); } catch (ArgumentNullException ex) { throw new InvalidOperationException("The context is missing a session policy.", ex); } }
private static void ReservationIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.Reservation); } catch (ArgumentNullException ex) { throw new InvalidOperationException("The context does not have a reservation.", ex); } }
private static void AuthenticationContextIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.AuthenticationContext); } catch (ArgumentNullException ex) { throw new InvalidOperationException("The context does not contain an authentication context.", ex); } }
private static void ValidateClient(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.Client); } catch (ArgumentNullException ex) { throw new InvalidOperationException("The client must be set on the connection context before trying to negotiate a session reservation.", ex); } }
private static void PlayerSettingsIsNotNull(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { Validate.NotNull(sessionConnectionContext.PlayerSettings); } catch (ArgumentNullException ex) { throw new InvalidOperationException("The context does not contain player settings.", ex); } }
private void EnterMultiplayerSession(IMultiplayerSessionConnectionContext sessionConnectionContext) { IClient client = sessionConnectionContext.Client; MultiplayerSessionReservation reservation = sessionConnectionContext.Reservation; string correlationId = reservation.CorrelationId; string reservationKey = reservation.ReservationKey; PlayerJoiningMultiplayerSession packet = new PlayerJoiningMultiplayerSession(correlationId, reservationKey); client.Send(packet); }
public void DisconnectShouldThrowInvalidOperationException() { // Arrange IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); Disconnected connectionState = new Disconnected(); // Act Action action = () => connectionState.Disconnect(connectionContext); // Assert action.ShouldThrow <InvalidOperationException>(); }
public void JoinSessionShouldThrowInvalidOperationExcepion() { // Arrange IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); SessionReservationRejected connectionState = new SessionReservationRejected(); // Act Action action = () => connectionState.JoinSession(connectionContext); // Assert action.ShouldThrow <InvalidOperationException>(); }
private static void EstablishSessionPolicy(IMultiplayerSessionConnectionContext sessionConnectionContext, IClient client) { string policyRequestCorrelationId = Guid.NewGuid().ToString(); MultiplayerSessionPolicyRequest requestPacket = new MultiplayerSessionPolicyRequest(policyRequestCorrelationId); client.Send(requestPacket); EstablishingSessionPolicy nextState = new EstablishingSessionPolicy(policyRequestCorrelationId); sessionConnectionContext.UpdateConnectionState(nextState); }
public void JoinSessionShouldThrowInvalidOperationException() { // Arrange IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); AwaitingReservationCredentials connectionState = new AwaitingReservationCredentials(); // Act Action action = () => connectionState.JoinSession(connectionContext); // Assert action.Should().Throw <InvalidOperationException>(); }
public void JoinSessionShouldThrowInvalidOperationException() { // Arrange IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID); // Act Action action = () => connectionState.JoinSession(connectionContext); // Assert action.Should().Throw <InvalidOperationException>(); }
public override void NegotiateReservation(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { ValidateState(sessionConnectionContext); HandleReservation(sessionConnectionContext); } catch (Exception) { Disconnect(sessionConnectionContext); throw; } }
private void ValidateState(IMultiplayerSessionConnectionContext sessionConnectionContext) { ValidateClient(sessionConnectionContext); try { Validate.NotNull(sessionConnectionContext.IpAddress); } catch (ArgumentNullException ex) { throw new InvalidOperationException("The context is missing an IP address.", ex); } }
public void NegotiateReservation(IMultiplayerSessionConnectionContext sessionConnectionContext) { lock (stateLock) { ValidateState(sessionConnectionContext); IClient client = sessionConnectionContext.Client; string ipAddress = sessionConnectionContext.IpAddress; int port = sessionConnectionContext.ServerPort; StartClient(ipAddress, client, port); EstablishSessionPolicy(sessionConnectionContext, client); } }
public override void JoinSession(IMultiplayerSessionConnectionContext sessionConnectionContext) { try { ValidateState(sessionConnectionContext); EnterMultiplayerSession(sessionConnectionContext); ChangeState(sessionConnectionContext); } catch (Exception) { Disconnect(sessionConnectionContext); throw; } }
public void UpdateStateShouldRaiseEvent() { // Arrange IClient client = Substitute.For <IClient>(); IMultiplayerSession multiplayerSession = new MultiplayerSessionManager(client); IMultiplayerSessionConnectionContext connectionContext = (IMultiplayerSessionConnectionContext)multiplayerSession; IMonitor <IMultiplayerSession> monitor = multiplayerSession.Monitor(); // Act connectionContext.UpdateConnectionState(TestConstants.TEST_CONNECTION_STATE); // Assert monitor.Should().Raise("ConnectionStateChanged"); }
public void NegotiateShouldThrowInvalidOperationExceptionWhenTheReservationIsNull() { // Arrange IMultiplayerSessionConnectionContext connectionContext = Substitute.For <IMultiplayerSessionConnectionContext>(); connectionContext.Reservation.Returns((MultiplayerSessionReservation)null); AwaitingSessionReservation connectionState = new AwaitingSessionReservation(TestConstants.TEST_CORRELATION_ID); // Act Action action = () => connectionState.NegotiateReservation(connectionContext); // Assert action.Should().Throw <InvalidOperationException>(); }