/// <summary> /// Checks if received data is a TMQ protocol message /// </summary> public async Task <ProtocolHandshakeResult> Handshake(IConnectionInfo info, byte[] data) { ProtocolHandshakeResult result = new ProtocolHandshakeResult(); if (data.Length < 8) { return(await Task.FromResult(result)); } result.Accepted = CheckProtocol(data); if (!result.Accepted) { return(result); } TmqReader reader = new TmqReader(); TmqMessage message = await reader.Read(info.GetStream()); //sends protocol message await info.GetStream().WriteAsync(PredefinedMessages.PROTOCOL_BYTES); bool alive = await ProcessFirstMessage(message, info, result); if (!alive) { return(result); } result.PipeConnection = true; info.State = ConnectionStates.Pipe; info.Protocol = this; return(result); }
/// <summary> /// Handles the connection and reads received TMQ messages /// </summary> public async Task HandleConnection(IConnectionInfo info, ProtocolHandshakeResult handshakeResult) { //if user makes a mistake in ready method, we should not interrupt connection handling try { await _handler.Ready(_server, (TmqServerSocket)handshakeResult.Socket); } catch (Exception e) { if (_server.Logger != null) { _server.Logger.LogException("Unhandled Exception", e); } } TmqReader reader = new TmqReader(); while (info.Client != null && info.Client.Connected) { TmqMessage message = await reader.Read(info.GetStream()); if (message == null) { info.Close(); return; } if (message.Ttl < 0) { continue; } await ProcessMessage(info, message, (TmqServerSocket)handshakeResult.Socket); } }