public async Task <AppConnectionDescriptor> AuthenticateAsync(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}"); } if (!_connectionTracker.IsAppInstanceRegistered(connectRequest.ApplicationInstanceId)) { Log.Debug("Connection with unknown application instance id: " + $"ApplicationInstanceId={connectRequest.ApplicationInstanceId}, ApplicationId={connectRequest.ApplicationId}"); _connectionTracker.ReportConnectionError(new AppConnectionDescriptor( UniqueId.Empty, connectRequest.ApplicationId, connectRequest.ApplicationInstanceId, connection.TransportType)); if (_features.HasFlag(BrokerFeatures.CheckAppInstanceId)) { throw new BrokerException("Connection rejected because application instance id is unknown to broker: " + $"ApplicationInstanceId={connectRequest.ApplicationInstanceId}, ApplicationId={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); return(new AppConnectionDescriptor( connectResponse.ConnectionId, connectRequest.ApplicationId, connectRequest.ApplicationInstanceId, connection.TransportType)); } } }