/// <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 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> /// After protocol handshake is completed, this method is called to handle events for the specified client /// </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, (WsServerSocket)handshakeResult.Socket); } catch (Exception e) { if (_server.Logger != null) { _server.Logger.LogException("Unhandled Exception", e); } } WebSocketReader reader = new WebSocketReader(); Stream stream = info.GetStream(); while (info.Socket != null && info.Socket.IsConnected) { WebSocketMessage message = await reader.Read(stream); if (message == null) { info.Close(); return; } handshakeResult.Socket.KeepAlive(); await ProcessMessage(info, handshakeResult.Socket, message); } }
/// <summary> /// Socket base constructor for server sockets /// </summary> protected SocketBase(IConnectionInfo info) { IsSsl = info.IsSsl; IsConnected = true; Stream = info.GetStream(); if (IsSsl) { _ss = new SemaphoreSlim(1, 1); } }
/// <summary> /// After protocol handshake is completed, this method is called to handle events for the specified client /// </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, handshakeResult.Socket); } catch (Exception e) { if (_server.Logger != null) { _server.Logger.LogException("Unhandled Exception", e); } } HttpReader reader = new HttpReader(Options); HttpWriter writer = new HttpWriter(Options); reader.HandshakeResult = handshakeResult; HandleStatus status; do { HttpMessage message = await reader.Read(info.GetStream()); if (message.Request != null) { message.Request.IpAddress = FindIPAddress(info.Client); } status = await ProcessMessage(info, writer, message, reader.ContentLength); if (status == HandleStatus.ReadAgain) { reader.Reset(); } } while (status == HandleStatus.ReadAgain); if (status == HandleStatus.Close) { info.Close(); } }
/// <summary> /// Switches client's protocol to new protocol (finds by name) /// </summary> public async Task SwitchProtocol(IConnectionInfo info, string newProtocolName, ConnectionData data) { foreach (ITwinoProtocol protocol in Protocols) { if (protocol.Name.Equals(newProtocolName, StringComparison.InvariantCultureIgnoreCase)) { ProtocolHandshakeResult hsresult = await protocol.SwitchTo(info, data); if (!hsresult.Accepted) { info.Close(); return; } ITwinoProtocol previous = info.Protocol; info.Protocol = protocol; info.Socket = hsresult.Socket; if (info.Socket != null) { info.Socket.SetOnConnected(); } if (hsresult.Response != null) { await info.GetStream().WriteAsync(hsresult.Response); } if (info.Socket != null) { info.Socket.SetOnProtocolSwitched(previous, info.Protocol); } await protocol.HandleConnection(info, hsresult); return; } } }