public override async Task OnDisconnectAsync(TcpSocketClient <Message, Message> client, CancellationToken cancellationToken) { int id = manager.GetId(client); bool result = await manager.RemoveSocketAsync(id, cancellationToken); if (!result) { IClient socket = client.GetSocket(); logger.Error($"Failed to remove socket: {socket.Endpoint}"); } logger.Information($"Player {id} disconnected"); DisconnectPayload payload = new DisconnectPayload() { AgentID = id }; Message message = new Message() { AgentID = -1, MessageID = MessageID.PlayerDisconnected, Payload = payload }; await container.GMClient.SendAsync(message, cancellationToken); }
public async Task ClientLoopAsync(TcpSocketClient <R, S> client, CancellationToken cancellationToken) { IClient socket = null; try { socket = client.GetSocket(); logger.Information($"Started handling messages for {socket.Endpoint}"); (bool result, R message) = await client.ReceiveAsync(cancellationToken); while (!cancellationToken.IsCancellationRequested && result) { await OnMessageAsync(client, message, cancellationToken); (result, message) = await client.ReceiveAsync(cancellationToken); } } catch (IOException) { logger.Warning("Connection stream closed"); } catch (Exception e) { logger.Error($"Error reading message: {e}"); await OnExceptionAsync(client, e, cancellationToken); } finally { logger.Information($"Finished handling messages for {socket.Endpoint}"); } }
public void GetSocket_Test() { // Arrange var socketClient = new TcpSocketClient <Message, Message>(tcpClient, logger); // Act var socket = socketClient.GetSocket(); // Assert Assert.True(socket == tcpClient, "Returned socket should be the same object which was passed via constructor"); }
public override void OnConnect(TcpSocketClient <Message, Message> client) { int id = manager.AddSocket(client); if (id == -1) { IClient socket = client.GetSocket(); logger.Error($"Failed to add socket: {socket.Endpoint}"); } else { sync.SemaphoreSlim.Wait(); container.ConfirmedAgents.Add(manager.GetId(client), false); sync.SemaphoreSlim.Release(1); } }