public async Task <IAppConnection> HandleAsync(ITransportConnection connection) { Log.Trace("Accepting new connection {0}", connection.Id); var channel = await connection.IncomingChannels.ReadAsync().ConfigureAwait(false); var frame = await channel.In.ReadAsync().ConfigureAwait(false); using (var payload = frame.Payload) using (var connectRequest = _serializer.DeserializeConnectRequest(payload)) { if (!_registryService.IsApplicationDefined(connectRequest.ApplicationId)) { throw new BrokerException($"Connection rejected because application id is unknown to broker: {connectRequest.ApplicationId}"); } using (var connectResponse = _messageFactory.CreateConnectResponse(connection.Id)) { var serializedResponse = _serializer.Serialize(connectResponse); try { Log.Trace("Sending connect response ({0} bytes): {1}", connectResponse, serializedResponse.Count); await channel.Out.WriteAsync(new TransportMessageFrame(serializedResponse)).ConfigureAwait(false); } catch { serializedResponse.Dispose(); throw; } channel.Out.TryComplete(); await channel.Completion.ConfigureAwait(false); var info = new AppConnectionDescriptor( connectResponse.ConnectionId, connectRequest.ApplicationId, connectRequest.ApplicationInstanceId); var clientConnection = _connectionTracker.AcceptConnection(connection, info); Log.Info("New connection accepted: {0}", clientConnection); return(clientConnection); } } }
private async Task ProcessConnectionAsync(object state) { var transportConnection = (ITransportConnection)state; Log.Debug("Accepting new incoming connection {0}", transportConnection.Id); var appConnectionDescriptor = await _authenticationHandler.AuthenticateAsync(transportConnection).ConfigureAwait(false); Log.Debug("New connection authenticated: {0}", appConnectionDescriptor); var clientConnection = _appLifecycleManager.AcceptConnection(transportConnection, appConnectionDescriptor); try { Log.Info("New connection accepted: {0}", clientConnection); var clientConnectionProcessor = new AppConnectionProcessor(clientConnection, _clientRequestHandler); await clientConnectionProcessor.ProcessAsync(() => _appLifecycleManager.TryRemoveConnection(clientConnection)).ConfigureAwait(false); } finally { _appLifecycleManager.TryRemoveConnection(clientConnection); } }