public GsnllLobby(TcpClient tcpClient, string clientId, IGsnllGameManager <TState, TClientInfo> gameManager) { this._tcpClient = tcpClient; this._tcpStream = tcpClient.GetStream(); this.ClientId = clientId; this.GameManager = gameManager; this._stoppedTokenSource = new CancellationTokenSource(); }
public GameServerIncomingMessageConverter(IGsnllGameManager <TState, TClientInfo> gameManager) { this._gameManager = gameManager; }
public async Task <GsnllLobby <TState, TClientInfo> > EnterMatchAsync <TState, TClientInfo>(IGsnllGameManager <TState, TClientInfo> gameManager, string clientId, CancellationToken cancellation = default) { TcpClient tcpClient = null; bool successful = false; try { tcpClient = new TcpClient(); // Connect to the matchmaking server await tcpClient.ConnectAsync(this.RemoteAddress, this.RemotePort); // Send connection details NetworkStream stream = tcpClient.GetStream(); await stream.WriteJsonAsync(new MatchmakingConnectOutgoingMessage(new MatchmakingConnectOutgoingMessage.ConnectionData("default", new MatchmakingConnectOutgoingMessage.ClientConfiguration(clientId))), cancellation : cancellation); // Wait for matchmaking server response IMatchmakingIncomingMessage response = await stream.ReadJsonAsync <IMatchmakingIncomingMessage>(cancellation : cancellation); // Process response switch (response) { case MatchmakingIncomingResponseMessage responseMessage: { Console.WriteLine("Ready to connect!"); break; } case MatchmakingIncomingErrorMessage errorMessage: { throw new Exception($"An error was received by the matchmaking server while connecting: {errorMessage.Data.ErrorType} - {errorMessage.Data.Message}"); } default: { throw new Exception($"Unexpected message: {response.MessageType}"); } } // Wait until connected to a lobby while (!(response is MatchmakingIncomingConnectMessage)) { response = await stream.ReadJsonAsync <IMatchmakingIncomingMessage>(cancellation : cancellation); } Console.WriteLine("Connected to a lobby!"); successful = true; return(new GsnllLobby <TState, TClientInfo>(tcpClient, clientId, gameManager)); } finally { if (!successful) { tcpClient?.Dispose(); } } }