private async Task ReadClientMessages(ClientConnection connection) { while (!cancellation.IsCancellationRequested) { var message = await connection.ReadMessage(); OnIncomingMessage(new IncomingMessage(connection, message)); } }
private void OnClientAccepted(Socket clientSocket) { var clientConnection = new ClientConnection(clientSocket); var clientAcceptedHandlers = ClientAccepted; if (clientAcceptedHandlers != null) clientAcceptedHandlers(this, clientConnection); }
private async void ClientDisconnected(object sender, ClientConnection connection) { try { await BroadcastToAll(new Message { Sender = "<server>", Text = $"{connection} disconnected" }); } catch (Exception ex) { logger.Warn(ex, "{0} disconnected handling", connection); } }
private async void OnClientAccepted(object sender, ClientConnection connection) { ImmutableInterlocked.Update(ref clients, oldClients => oldClients.Add(connection)); logger.Debug("{0} connected", connection); OnClientConnected(connection); try { await ReadClientMessages(connection); } catch (Exception ex) { if (ex is OperationCanceledException || ex is DisconnectedException) { } else { logger.Warn(ex, "{0} disconnected with error", connection); } } finally { ImmutableInterlocked.Update(ref clients, oldClients => oldClients.Remove(connection)); logger.Debug("{0} disconnected", connection); OnClientDisconnected(connection); } }
public IncomingMessage(ClientConnection sender, Message message) { Sender = sender; Message = message; }
private void OnClientDisconnected(ClientConnection connection) { var handlers = ClientDisconnected; if (handlers != null) { handlers(this, connection); } }
public async Task ReplyTo(ClientConnection client, Message message) { await Task.Yield(); await client.SendMessage(message); }