示例#1
0
        public void Initialize(IServiceConnectionContainerFactory connectionFactory)
        {
            if (connectionFactory == null)
            {
                throw new ArgumentNullException(nameof(connectionFactory));
            }

            if (_hubConnections != null)
            {
                // TODO: log something to indicate the connection is already initialized.
                return;
            }

            lock (_lock)
            {
                if (_hubConnections != null)
                {
                    return;
                }

                var connections = new Dictionary <string, IServiceConnectionContainer>();

                _appConnection = connectionFactory.Create(_appName);

                foreach (var hub in _hubs)
                {
                    var connection = connectionFactory.Create(hub);
                    connections.Add(hub, connection);
                }

                _hubConnections = connections;
            }
        }
        internal MultiEndpointServiceConnectionContainer(
            string hub,
            Func <HubServiceEndpoint, IServiceConnectionContainer> generator,
            IServiceEndpointManager endpointManager,
            IMessageRouter router,
            ILoggerFactory loggerFactory)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            _logger = loggerFactory?.CreateLogger <MultiEndpointServiceConnectionContainer>() ?? throw new ArgumentNullException(nameof(loggerFactory));

            // provides a copy to the endpoint per container
            _endpoints = endpointManager.GetEndpoints(hub);

            if (_endpoints.Count == 1)
            {
                _inner = generator(_endpoints[0]);
            }
            else
            {
                // router is required when endpoints > 1
                _router = router ?? throw new ArgumentNullException(nameof(router));
                ConnectionContainers = _endpoints.ToDictionary(s => (ServiceEndpoint)s, s => generator(s));
            }
        }
示例#3
0
        private async Task WriteMessage(IServiceConnectionContainer connection, AppMessage appMessage)
        {
            var message = appMessage.Message;

            switch (message)
            {
            // For group related messages, make sure messages are written to the same partition
            case JoinGroupMessage joinGroupMessage:
                try
                {
                    await connection.WriteAsync(joinGroupMessage.GroupName, joinGroupMessage);
                }
                finally
                {
                    _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                }
                break;

            case LeaveGroupMessage leaveGroupMessage:
                await connection.WriteAsync(leaveGroupMessage.GroupName, leaveGroupMessage);

                break;

            case GroupBroadcastDataMessage groupBroadcastMessage:
                await connection.WriteAsync(groupBroadcastMessage.GroupName, groupBroadcastMessage);

                break;

            default:
                await connection.WriteAsync(message);

                break;
            }
        }
示例#4
0
        private async Task WriteMessage(IServiceConnectionContainer connection, AppMessage appMessage)
        {
            var message = appMessage.Message;

            switch (message)
            {
            // For group related messages, make sure messages are written to the same partition
            case JoinGroupMessage joinGroupMessage:
                try
                {
                    await connection.WriteAsync(joinGroupMessage.GroupName, joinGroupMessage);
                }
                finally
                {
                    _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                }
                break;

            case LeaveGroupMessage leaveGroupMessage:
                try
                {
                    await connection.WriteAsync(leaveGroupMessage.GroupName, leaveGroupMessage);
                }
                finally
                {
                    _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                }
                break;

            case GroupBroadcastDataMessage groupBroadcastMessage:
                await connection.WriteAsync(groupBroadcastMessage.GroupName, groupBroadcastMessage);

                break;

            case ConnectionDataMessage connectionDataMessage:
                if (_clientConnectionManager.TryGetServiceConnection(connectionDataMessage.ConnectionId, out var serviceConnection))
                {
                    // If the client connection is connected to local server connection, send back directly from the established server connection
                    await serviceConnection.WriteAsync(message);
                }
                else
                {
                    await connection.WriteAsync(message);
                }
                break;

            default:
                await connection.WriteAsync(message);

                break;
            }
        }
        public void Initialize(Func <string, IServiceConnection> connectionGenerator, int connectionCount)
        {
            if (connectionGenerator == null)
            {
                throw new ArgumentNullException(nameof(connectionGenerator));
            }

            if (connectionCount <= 0)
            {
                throw new ArgumentException($"{nameof(connectionCount)} must be larger than 0.");
            }

            if (_serviceConnections != null)
            {
                // TODO: log something to indicate the connection is already initialized.
                return;
            }

            lock (_lock)
            {
                if (_serviceConnections != null)
                {
                    return;
                }

                var connections = new Dictionary <string, IServiceConnectionContainer>();

                _appConnection = new ServiceConnectionContainer(
                    () => connectionGenerator(_appName),
                    connectionCount);

                foreach (var hub in _hubs)
                {
                    var connection = new ServiceConnectionContainer(
                        () => connectionGenerator(hub),
                        connectionCount);
                    connections.Add(hub, connection);
                }

                _serviceConnections = connections;
            }
        }
示例#6
0
        public MultiEndpointServiceConnectionContainer(Func <ServiceEndpoint, IServiceConnectionContainer> generator, IServiceEndpointManager endpointManager, IMessageRouter router, ILoggerFactory loggerFactory)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            _logger = loggerFactory?.CreateLogger <MultiEndpointServiceConnectionContainer>() ?? NullLogger <MultiEndpointServiceConnectionContainer> .Instance;

            // provides a copy to the endpoint per container
            _endpoints = endpointManager.Endpoints.Select(s => new ServiceEndpoint(s)).ToArray();

            if (_endpoints.Count == 1)
            {
                _inner = generator(_endpoints[0]);
            }
            else
            {
                // router is required when endpoints > 1
                _router     = router ?? throw new ArgumentNullException(nameof(router));
                Connections = _endpoints.ToDictionary(s => s, s => generator(s));
            }
        }
        public MultiEndpointServiceConnectionContainer(Func <ServiceEndpoint, IServiceConnectionContainer> generator, IServiceEndpointManager endpointManager, IEndpointRouter router, ILoggerFactory loggerFactory)
        {
            if (generator == null)
            {
                throw new ArgumentNullException(nameof(generator));
            }

            _endpointManager = endpointManager ?? throw new ArgumentNullException(nameof(endpointManager));
            _logger          = loggerFactory?.CreateLogger <MultiEndpointServiceConnectionContainer>() ?? NullLogger <MultiEndpointServiceConnectionContainer> .Instance;

            var endpoints = endpointManager.Endpoints;

            if (endpoints.Length == 1)
            {
                _inner = generator(endpoints[0]);
            }
            else
            {
                // router is required when endpoints > 1
                _router     = router ?? throw new ArgumentNullException(nameof(router));
                Connections = endpoints.ToDictionary(s => s, s => generator(s));
            }
        }
 public void SetServiceConnection(IServiceConnectionContainer serviceConnection)
 {
     throw new NotImplementedException();
 }
示例#9
0
 public void SetServiceConnection(IServiceConnectionContainer serviceConnection)
 {
     _serviceConnection = serviceConnection;
 }
示例#10
0
        private async Task WriteMessage(IServiceConnectionContainer connection, AppMessage appMessage)
        {
            var message = appMessage.Message;

            try
            {
                switch (message)
                {
                // For group related messages, make sure messages are written to the same partition
                case JoinGroupWithAckMessage joinGroupMessage:
                    try
                    {
                        await connection.WriteAckableMessageAsync(joinGroupMessage);
                    }
                    finally
                    {
                        _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                    }
                    break;

                case LeaveGroupWithAckMessage leaveGroupMessage:
                    try
                    {
                        await connection.WriteAckableMessageAsync(leaveGroupMessage);
                    }
                    finally
                    {
                        _ackHandler.TriggerAck(appMessage.RawMessage.CommandId);
                    }
                    break;

                case ConnectionDataMessage connectionDataMessage:
                    var connectionId = connectionDataMessage.ConnectionId;
                    if (_clientConnectionManager.TryGetClientConnection(connectionId, out var conn))
                    {
                        // If the client connection is connected to local server connection,
                        // send back directly from the established server connection
                        await conn.WriteMessageAsync(connectionDataMessage);
                    }
                    else
                    {
                        await connection.WriteAsync(message);
                    }
                    break;

                default:
                    await connection.WriteAsync(message);

                    break;
                }

                if (message is IMessageWithTracingId msg && msg.TracingId != null)
                {
                    MessageLog.SucceededToSendMessage(_logger, msg);
                }
            }
            catch (Exception ex)
            {
                if (message is IMessageWithTracingId msg && msg.TracingId != null)
                {
                    MessageLog.FailedToSendMessage(_logger, msg, ex);
                }
                throw;
            }
        }
 public ConnectionService(IServiceConnectionContainer connectionContainer)
 {
     _connectionContainer = connectionContainer;
 }