public async Task ProcessAsync(IMessageProtocol messageProtocol) { if (Equals(messageProtocol.Header, MessageType.Message)) { var message = messageProtocol.GetDataAs <Message>(); try { var meseClient = _clientsPool.FindClient(message.To.Id); if (meseClient != null) { await MessageCommunicator.WriteAsync(meseClient.TcpClient, messageProtocol); } else { Logger.Error($"Client {message.To.Name} not found. "); } } catch (Exception ex) { Logger.Error($"Exception: {ex}."); } return; } await _innerProcessor.ProcessAsync(messageProtocol); }
public override async Task ProcessAsync(IMessageProtocol messageProtocol) { if (messageProtocol != null && Equals(messageProtocol.Header, MessageType.ClientConnectedSelf)) { var connectionEstablished = messageProtocol.GetDataAs <ConnectionEstablished>(); Notifier.OnConnectedToServer(this, new NotifierEventArgs <ConnectionEstablished>(connectionEstablished)); return; } await _innerProcessor.ProcessAsync(messageProtocol); }
public override async Task ProcessAsync(IMessageProtocol messageProtocol) { if (messageProtocol.Header.Equals(MessageType.ClientDisconnected)) { var user = messageProtocol.GetDataAs <User>(); Notifier.OnUserDisconnected(this, new NotifierEventArgs <User>(user)); return; } await _innerProcessor.ProcessAsync(messageProtocol); }
public override async Task ProcessAsync(IMessageProtocol messageProtocol) { if (messageProtocol != null && Equals(messageProtocol.Header, MessageType.Message)) { var message = messageProtocol.GetDataAs <Message>(); Notifier.OnMessageReceived(this, new NotifierEventArgs <Message>(message)); return; } await _innerProcessor.ProcessAsync(messageProtocol); }
public async Task ProcessAsync(IMessageProtocol messageProtocol) { if (messageProtocol != null && Equals(messageProtocol.Header, MessageType.ClientDisconnected)) { var user = messageProtocol.GetDataAs <User>(); Logger.Info($"Client disconnected. {user}"); await BroadcastClientDisconnectedAsync(user); return; } await _innerProcessor.ProcessAsync(messageProtocol); }
private void Listen() { new Thread( async() => { while (_listening) { while (_tcpClient.Available <= 0) { Thread.Sleep(100); } var messageProtocol = await MessageCommunicator.ReadAsync(_tcpClient); await _protocolProcessor.ProcessAsync(messageProtocol); } }).Start(); }
private async Task HandleClientAsync(TcpClient client) { try { var meseClient = await GetMeseClientInformation(client); if (meseClient != null) { Logger.Info($"Client connected: {meseClient?.User}"); _clientsPool.Add(meseClient); IClientHandler meseClientHandler; if ((meseClientHandler = await _clientService.CompleteHandshakeAsync(meseClient)) != null) { meseClientHandler.MessageProtocolReceived += async(sender, args) => { if (args?.MessageProtocol == null) { return; } await _messageProtocolProcessor.ProcessAsync(args?.MessageProtocol); }; await meseClientHandler.StartListeningAsync(); } } } catch (ClientDisconnectedException ex) { await ForcelyDisconnectClient(ex.Client); } catch (Exception ex) { Logger.Error(ex.ToString()); } }