public void Inject(PlayerStreams playerStreams, CharacterHudCard.Factory cardFactory) { this.playerStreams = playerStreams; this.cardFactory = cardFactory; }
public override async Task LockStepStream(IAsyncStreamReader <MStepReq> requestStream, IServerStreamWriter <MStepRes> responseStream, ServerCallContext context) { var authToken = context.RequestHeaders.Single(h => h.Key == "authentication").Value; Console.WriteLine($"authToken:{authToken}"); long playerID = 0L; if (authToken != "") { playerID = 24L; } // TODO: check if authToken is validate if (playerID == 0L) { throw new AuthenticationException(); } else if (!PlayerStreams.ContainsKey(playerID)) { PlayerStreams.Add(playerID, responseStream); } CancellationTokenSource tokenSource = new CancellationTokenSource(); CancellationToken token = tokenSource.Token; // Read incoming messages in a background task MStepReq?lastMessageReceived = null; var readTask = Task.Run(async() => { while (!token.IsCancellationRequested && await requestStream.MoveNext(token)) { // Next the message to observable this.requestO.Notify(new StepMessageModel(this) { PlayerId = playerID, Message = requestStream.Current }); lastMessageReceived = requestStream.Current; Console.WriteLine($"lastMessageReceived: {lastMessageReceived}"); // if (requestStream.Current.Name == "stop") // { // break; // } } PlayerStreams.Remove(playerID); this.requestO.Notify(new StepMessageModel(this) { PlayerId = playerID, Message = new MStepReq() { MsgType = EMessageType.Leave } }); Console.WriteLine($"close client connection: {playerID}"); context.CancellationToken.ThrowIfCancellationRequested(); return(""); }, token); context.CancellationToken.Register(() => { Console.WriteLine("request closed, close the read task"); tokenSource.Cancel(); readTask.Dispose(); }); await readTask; }
public void Inject(PlayerState playerState, PlayerStreams playerStreams, Guid playerId) { this.playerState = playerState; this.playerStreams = playerStreams; this.playerId = playerId; }