public ServiceHubDispatcher(
            IServiceProtocol serviceProtocol,
            IHubContext <THub> context,
            IServiceConnectionManager <THub> serviceConnectionManager,
            IClientConnectionManager clientConnectionManager,
            IServiceEndpointManager serviceEndpointManager,
            IOptions <ServiceOptions> options,
            ILoggerFactory loggerFactory,
            IEndpointRouter router,
            IServerNameProvider nameProvider,
            ServerLifetimeManager serverLifetimeManager,
            IClientConnectionFactory clientConnectionFactory,
            IServiceEventHandler serviceEventHandler)
        {
            _serviceProtocol          = serviceProtocol;
            _serviceConnectionManager = serviceConnectionManager;
            _clientConnectionManager  = clientConnectionManager;
            _serviceEndpointManager   = serviceEndpointManager;
            _options = options != null ? options.Value : throw new ArgumentNullException(nameof(options));

            Context = context;

            _router                  = router ?? throw new ArgumentNullException(nameof(router));
            _loggerFactory           = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory));
            _logger                  = loggerFactory.CreateLogger <ServiceHubDispatcher <THub> >();
            _clientConnectionFactory = clientConnectionFactory;
            _nameProvider            = nameProvider;
            _hubName                 = typeof(THub).Name;
            _serviceEventHandler     = serviceEventHandler;

            serverLifetimeManager?.Register(ShutdownAsync);
        }
Example #2
0
 public TestServiceConnection(TestConnectionContainer container,
                              IServiceProtocol serviceProtocol,
                              TestClientConnectionManager clientConnectionManager,
                              IConnectionFactory connectionFactory,
                              ILoggerFactory loggerFactory,
                              ConnectionDelegate connectionDelegate,
                              IClientConnectionFactory clientConnectionFactory,
                              string serverId,
                              string connectionId,
                              HubServiceEndpoint endpoint,
                              IServiceMessageHandler serviceMessageHandler,
                              IServiceEventHandler serviceEventHandler,
                              ServiceConnectionType connectionType = ServiceConnectionType.Default,
                              GracefulShutdownMode mode            = GracefulShutdownMode.Off,
                              int closeTimeOutMilliseconds         = 10000) : base(
         serviceProtocol,
         clientConnectionManager,
         connectionFactory,
         loggerFactory,
         connectionDelegate,
         clientConnectionFactory,
         serverId,
         connectionId,
         endpoint,
         serviceMessageHandler,
         serviceEventHandler,
         connectionType: connectionType,
         mode: mode,
         closeTimeOutMilliseconds: closeTimeOutMilliseconds)
 {
     _container = container;
     ClientConnectionManager = clientConnectionManager;
 }
Example #3
0
        /// <summary>
        /// 启动服务 并将GameServer交给Service回调OnConeection and OnException
        /// </summary>
        /// <param name="address"></param>
        /// <param name="port"></param>
        /// <param name="handler"></param>
        public void Start(IPAddress address, int port, IServiceEventHandler handler)
        {
            if (handler == null)
            {
                throw new ArgumentNullException(handler.GetType().FullName);
            }

            m_serviceEventHandler = handler;

            // 启动监听
            m_listener = new TcpListener(address, port);
            m_listener.Start();

            // 监听成功开启后开始持续工作
            m_state = State.Running;
            var result = new Task
                         (
                () => { ServiceStart().Wait(); },
                TaskCreationOptions.LongRunning
                         );

            result.ContinueWith(OnException, TaskContinuationOptions.OnlyOnFaulted);//发生错误就通知ServerBase
            result.Start(TaskSchedulerHelper.TaskSchedulers[m_priority]);
            m_mainTask = result;
        }
        private static TestServiceConnection CreateServiceConnection(Action <ConnectionBuilder> use = null,
                                                                     TestClientConnectionManager clientConnectionManager = null,
                                                                     string serverId                                  = null,
                                                                     string connectionId                              = null,
                                                                     GracefulShutdownMode?mode                        = null,
                                                                     IServiceMessageHandler messageHandler            = null,
                                                                     IServiceEventHandler eventHandler                = null,
                                                                     IClientConnectionFactory clientConnectionFactory = null,
                                                                     ILoggerFactory loggerFactory                     = null)
        {
            clientConnectionManager ??= new TestClientConnectionManager();
            clientConnectionFactory ??= new ClientConnectionFactory();

            var container         = new TestConnectionContainer();
            var connectionFactory = new TestConnectionFactory(conn =>
            {
                container.Instance = conn;
                return(Task.CompletedTask);
            });

            var services = new ServiceCollection();
            var builder  = new ConnectionBuilder(services.BuildServiceProvider());

            if (use == null)
            {
                use = (builder) => builder.UseConnectionHandler <TestConnectionHandler>();
            }
            use(builder);

            builder.UseConnectionHandler <TestConnectionHandler>();

            ConnectionDelegate handler = builder.Build();

            return(new TestServiceConnection(
                       container,
                       new ServiceProtocol(),
                       clientConnectionManager,
                       connectionFactory,
                       loggerFactory ?? NullLoggerFactory.Instance,
                       handler,
                       clientConnectionFactory,
                       serverId ?? "serverId",
                       connectionId ?? Guid.NewGuid().ToString("N"),
                       null,
                       messageHandler ?? new TestServiceMessageHandler(),
                       eventHandler ?? new TestServiceEventHandler(),
                       mode: mode ?? GracefulShutdownMode.Off
                       ));
        }
 public ServiceConnectionFactory(
     IServiceProtocol serviceProtocol,
     IClientConnectionManager clientConnectionManager,
     IConnectionFactory connectionFactory,
     ILoggerFactory logger,
     IServerNameProvider nameProvider,
     IServiceEventHandler serviceEventHandler)
 {
     _serviceProtocol         = serviceProtocol;
     _clientConnectionManager = clientConnectionManager;
     _connectionFactory       = connectionFactory;
     _logger              = logger;
     _nameProvider        = nameProvider;
     _serviceEventHandler = serviceEventHandler;
 }
Example #6
0
        private static TestServiceConnection CreateServiceConnection(ConnectionHandler handler = null,
                                                                     TestClientConnectionManager clientConnectionManager = null,
                                                                     string serverId                                  = null,
                                                                     string connectionId                              = null,
                                                                     GracefulShutdownMode?mode                        = null,
                                                                     IServiceMessageHandler messageHandler            = null,
                                                                     IServiceEventHandler eventHandler                = null,
                                                                     IClientConnectionFactory clientConnectionFactory = null,
                                                                     HubServiceEndpoint hubServiceEndpoint            = null,
                                                                     ILoggerFactory loggerFactory                     = null)
        {
            clientConnectionManager ??= new TestClientConnectionManager();
            clientConnectionFactory ??= new TestClientConnectionFactory();

            var container         = new TestConnectionContainer();
            var connectionFactory = new TestConnectionFactory(conn =>
            {
                container.Instance = conn;
                return(Task.CompletedTask);
            });

            var services = new ServiceCollection();
            var builder  = new ConnectionBuilder(services.BuildServiceProvider());

            if (handler == null)
            {
                handler = new TestConnectionHandler();
            }

            return(new TestServiceConnection(
                       container,
                       new ServiceProtocol(),
                       clientConnectionManager,
                       connectionFactory,
                       loggerFactory ?? NullLoggerFactory.Instance,
                       handler.OnConnectedAsync,
                       clientConnectionFactory,
                       serverId ?? "serverId",
                       connectionId ?? Guid.NewGuid().ToString("N"),
                       hubServiceEndpoint ?? new TestHubServiceEndpoint(),
                       messageHandler ?? new TestServiceMessageHandler(),
                       eventHandler ?? new TestServiceEventHandler(),
                       mode: mode ?? GracefulShutdownMode.Off
                       ));
        }
Example #7
0
 public TestServiceConnection(ServiceConnectionStatus status = ServiceConnectionStatus.Connected, bool throws = false,
                              ILogger logger = null,
                              IServiceMessageHandler serviceMessageHandler = null,
                              IServiceEventHandler serviceEventHandler     = null
                              ) : base(
         new ServiceProtocol(),
         "serverId",
         Guid.NewGuid().ToString(),
         new HubServiceEndpoint(),
         serviceMessageHandler,
         serviceEventHandler,
         ServiceConnectionType.Default,
         logger ?? NullLogger.Instance
         )
 {
     _expectedStatus = status;
     _throws         = throws;
 }
Example #8
0
 public ServiceConnection(IServiceProtocol serviceProtocol,
                          IClientConnectionManager clientConnectionManager,
                          IConnectionFactory connectionFactory,
                          ILoggerFactory loggerFactory,
                          ConnectionDelegate connectionDelegate,
                          IClientConnectionFactory clientConnectionFactory,
                          string serverId,
                          string connectionId,
                          HubServiceEndpoint endpoint,
                          IServiceMessageHandler serviceMessageHandler,
                          IServiceEventHandler serviceEventHandler,
                          ServiceConnectionType connectionType = ServiceConnectionType.Default,
                          GracefulShutdownMode mode            = GracefulShutdownMode.Off,
                          int closeTimeOutMilliseconds         = DefaultCloseTimeoutMilliseconds
                          ) : base(serviceProtocol, serverId, connectionId, endpoint, serviceMessageHandler, serviceEventHandler, connectionType, loggerFactory?.CreateLogger <ServiceConnection>(), mode)
 {
     _clientConnectionManager  = clientConnectionManager;
     _connectionFactory        = connectionFactory;
     _connectionDelegate       = connectionDelegate;
     _clientConnectionFactory  = clientConnectionFactory;
     _closeTimeOutMilliseconds = closeTimeOutMilliseconds;
 }
Example #9
0
 public ServiceConnection(
     string serverId,
     string connectionId,
     HubServiceEndpoint endpoint,
     IServiceProtocol serviceProtocol,
     IConnectionFactory connectionFactory,
     IClientConnectionManager clientConnectionManager,
     ILoggerFactory loggerFactory,
     IServiceMessageHandler serviceMessageHandler,
     IServiceEventHandler serviceEventHandler,
     ServiceConnectionType connectionType = ServiceConnectionType.Default)
     : base(
         serviceProtocol,
         serverId,
         connectionId,
         endpoint,
         serviceMessageHandler,
         serviceEventHandler,
         connectionType,
         loggerFactory?.CreateLogger <ServiceConnection>())
 {
     _connectionFactory       = connectionFactory;
     _clientConnectionManager = clientConnectionManager;
 }
Example #10
0
 private async Task HandleMessage(IServiceEventHandler handler, EventMessage integrationMessage, ILog log,
                                  CancellationToken cancellationToken)
 {
     await handler.Handle(JObject.Parse(integrationMessage.EventData), log, cancellationToken);
 }
Example #11
0
 async Task HandleMessage(IServiceEventHandler handler, KafkaMessage integrationMessage, CancellationToken cancellationToken)
 {
     await handler.Handle(JObject.Parse(integrationMessage.EventData), cancellationToken);
 }