/// <summary> /// Handles the connection and reads received HMQ 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, (HorseServerSocket)handshakeResult.Socket); } catch (Exception e) { if (_server.Logger != null) { _server.Logger.LogException("Unhandled Exception", e); } } HmqReader reader = new HmqReader(); while (info.Client != null && info.Client.Connected) { HorseMessage message = await reader.Read(info.GetStream()); if (message == null) { info.Close(); return; } await ProcessMessage(info, message, (HorseServerSocket)handshakeResult.Socket); } }
/// <summary> /// Checks if received data is a HMQ 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)); } ProtocolVersion version = CheckProtocol(data); result.Accepted = version != ProtocolVersion.Unknown; if (!result.Accepted) { return(result); } HmqReader reader = new HmqReader(); HorseMessage message = await reader.Read(info.GetStream()); //sends protocol message await info.GetStream().WriteAsync(PredefinedMessages.PROTOCOL_BYTES_V2); bool alive = await ProcessFirstMessage(message, info, result); if (!alive) { return(result); } result.PipeConnection = true; info.State = ConnectionStates.Pipe; info.Protocol = this; return(result); }