Пример #1
0
 public static ISuperSocketHostBuilder <WebSocketPackage> UseWebSocketMessageHandler(this ISuperSocketHostBuilder <WebSocketPackage> builder, Func <WebSocketSession, WebSocketPackage, Task> handler)
 {
     return(builder.ConfigureServices((ctx, services) =>
     {
         services.AddSingleton <Func <WebSocketSession, WebSocketPackage, Task> >(handler);
     }) as ISuperSocketHostBuilder <WebSocketPackage>);
 }
 public static ISuperSocketHostBuilder UseUdp(this ISuperSocketHostBuilder hostBuilder)
 {
     return(hostBuilder.ConfigureServices((context, services) =>
     {
         services.AddSingleton <IChannelCreatorFactory, UdpChannelCreatorFactory>();
     }) as ISuperSocketHostBuilder);
 }
 public static ISuperSocketHostBuilder <WebSocketPackage> UseWebSocketMessageHandler(this ISuperSocketHostBuilder <WebSocketPackage> builder, string protocol, Func <WebSocketSession, WebSocketPackage, ValueTask> handler)
 {
     return(builder.ConfigureServices((ctx, services) =>
     {
         services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(ISubProtocolHandler), new DelegateSubProtocolHandler(protocol, handler)));
     }) as ISuperSocketHostBuilder <WebSocketPackage>);
 }
Пример #4
0
 public static ISuperSocketHostBuilder <WebSocketPackage> UsePerMessageCompression(this ISuperSocketHostBuilder <WebSocketPackage> builder)
 {
     return(builder.ConfigureServices((ctx, services) =>
     {
         services.TryAddEnumerable(ServiceDescriptor.Singleton <IWebSocketExtensionFactory, WebSocketPerMessageCompressionExtensionFactory>());
     }));
 }
Пример #5
0
 private static ISuperSocketHostBuilder ConfigureCommand(this ISuperSocketHostBuilder builder)
 {
     return(builder.ConfigureServices((hostCxt, services) =>
     {
         services.Configure <CommandOptions>(hostCxt.Configuration?.GetSection("serverOptions")?.GetSection("commands"));
     }) as ISuperSocketHostBuilder);
 }
Пример #6
0
 public static ISuperSocketHostBuilder UseChannelCreatorFactory <TChannelCreatorFactory>(this ISuperSocketHostBuilder builder)
     where TChannelCreatorFactory : class, IChannelCreatorFactory
 {
     return(builder.ConfigureServices((ctx, services) =>
     {
         services.AddSingleton <IChannelCreatorFactory, TChannelCreatorFactory>();
     }).AsSuperSocketBuilder());
 }
/// <summary>
/// 使用中间件
/// </summary>
/// <typeparam name="TMiddleware">中间件类型</typeparam>
/// <param name="builder">Host Builder</param>
/// <param name="implementationFactory">实施工厂</param>
/// <returns></returns>
        public static ISuperSocketHostBuilder UseMiddleware <TMiddleware>(this ISuperSocketHostBuilder builder, Func <IServiceProvider, TMiddleware> implementationFactory)
            where TMiddleware : class, IMiddleware
        {
            return(builder.ConfigureServices((ctx, services) =>
            {
                services.TryAddEnumerable(ServiceDescriptor.Singleton <IMiddleware, TMiddleware>(implementationFactory));
            }).AsSuperSocketBuilder());
        }
 public static ISuperSocketHostBuilder ConfigureSocketOptionsSetter(this ISuperSocketHostBuilder hostBuilder, Func <Socket> socketOptionsSetter)
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.AddSingleton <Func <Socket> >(socketOptionsSetter);
     }
                ) as ISuperSocketHostBuilder);
 }
 public static ISuperSocketHostBuilder ConfigureSuperSocket(this ISuperSocketHostBuilder hostBuilder, Action <ServerOptions> configurator)
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.Configure <ServerOptions>(configurator);
     }
                ) as ISuperSocketHostBuilder);
 }
 public static IHostBuilder UseSerialIO(this ISuperSocketHostBuilder hostBuilder)
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.AddSingleton <IChannelCreatorFactory, SerialIOChannelCreatorFactory>();
     }
                ));
 }
Пример #11
0
 public static ISuperSocketHostBuilder <TReceivePackage> ConfigureErrorHandler <TReceivePackage>(this ISuperSocketHostBuilder <TReceivePackage> hostBuilder, Func <IAppSession, PackageHandlingException <TReceivePackage>, ValueTask <bool> > errorHandler)
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.AddSingleton <Func <IAppSession, PackageHandlingException <TReceivePackage>, ValueTask <bool> > >(errorHandler);
     }
                ));
 }
Пример #12
0
 public static ISuperSocketHostBuilder <TReceivePackage> ConfigureSuperSocket <TReceivePackage>(this ISuperSocketHostBuilder <TReceivePackage> hostBuilder, Action <ServerOptions> configurator)
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.Configure <ServerOptions>(configurator);
     }
                ));
 }
Пример #13
0
 internal static ISuperSocketHostBuilder <WebSocketPackage> UseWebSocketMiddleware(this ISuperSocketHostBuilder <WebSocketPackage> builder)
 {
     return(builder
            .ConfigureServices((ctx, services) =>
     {
         services.AddSingleton <IWebSocketServerMiddleware, WebSocketServerMiddleware>();
     })
            .UseMiddleware <WebSocketServerMiddleware>(s => s.GetService <IWebSocketServerMiddleware>() as WebSocketServerMiddleware)
            as ISuperSocketHostBuilder <WebSocketPackage>);
 }
Пример #14
0
 public static ISuperSocketHostBuilder <TReceivePackage> ConfigureSocketOptions <TReceivePackage>(this ISuperSocketHostBuilder <TReceivePackage> hostBuilder, Action <Socket> socketOptionsSetter)
     where TReceivePackage : class
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.AddSingleton <SocketOptionsSetter>(new SocketOptionsSetter(socketOptionsSetter));
     }
                ));
 }
Пример #15
0
 public static ISuperSocketHostBuilder UseInProcSessionContainer(this ISuperSocketHostBuilder builder)
 {
     return(builder.ConfigureServices((ctx, services) =>
     {
         services.AddSingleton <InProcSessionContainerMiddleware>();
         services.AddSingleton <ISessionContainer>((s) => s.GetRequiredService <InProcSessionContainerMiddleware>());
         services.AddSingleton <IAsyncSessionContainer>((s) => s.GetRequiredService <ISessionContainer>().ToAsyncSessionContainer());
         services.TryAddEnumerable(ServiceDescriptor.Singleton <IMiddleware, InProcSessionContainerMiddleware>(s => s.GetRequiredService <InProcSessionContainerMiddleware>()));
     }) as ISuperSocketHostBuilder);
 }
 public virtual void Configure(ISuperSocketHostBuilder hostBuilder)
 {
     hostBuilder.ConfigureServices((ctx, services) =>
     {
         services.Configure <ServerOptions>((options) =>
         {
             var listener = options.Listeners[0];
             Listener     = listener;
         });
     });
 }
Пример #17
0
        public static ISuperSocketHostBuilder <TReceivePackage> UsePipelineFilterFactory <TReceivePackage>(this ISuperSocketHostBuilder <TReceivePackage> hostBuilder, Func <object, IPipelineFilter <TReceivePackage> > filterFactory)
        {
            hostBuilder.ConfigureServices(
                (hostCtx, services) =>
            {
                services.AddSingleton <Func <object, IPipelineFilter <TReceivePackage> > >(filterFactory);
            }
                );

            return(hostBuilder.UsePipelineFilterFactory <DelegatePipelineFilterFactory <TReceivePackage> >());
        }
        public static ISuperSocketHostBuilder UseSessionFactory <TSessionFactory>(this ISuperSocketHostBuilder hostBuilder)
            where TSessionFactory : class, ISessionFactory
        {
            hostBuilder.ConfigureServices(
                (hostCtx, services) =>
            {
                services.AddSingleton <ISessionFactory, TSessionFactory>();
            }
                );

            return(hostBuilder);
        }
Пример #19
0
        public override void Configure(ISuperSocketHostBuilder hostBuilder)
        {
            hostBuilder.ConfigureServices((ctx, services) =>
            {
                services.Configure <ServerOptions>((options) =>
                {
                    var listener = options.Listeners[0];
                });
            });
            hostBuilder.UseGZip();

            base.Configure(hostBuilder);
        }
Пример #20
0
 public static ISuperSocketHostBuilder <TReceivePackage> UseSessionHandler <TReceivePackage>(this ISuperSocketHostBuilder <TReceivePackage> hostBuilder, Func <IAppSession, ValueTask> onConnected = null, Func <IAppSession, ValueTask> onClosed = null)
 {
     return(hostBuilder.ConfigureServices(
                (hostCtx, services) =>
     {
         services.AddSingleton <SessionHandlers>(new SessionHandlers
         {
             Connected = onConnected,
             Closed = onClosed
         });
     }
                ));
 }
Пример #21
0
        public static ISuperSocketHostBuilder <WebSocketPackage> UseCommand <TPackageInfo, TPackageMapper>(this ISuperSocketHostBuilder <WebSocketPackage> builder)
            where TPackageInfo : class
            where TPackageMapper : class, IPackageMapper <WebSocketPackage, TPackageInfo>
        {
            var keyType = CommandMiddlewareExtensions.GetKeyType <TPackageInfo>();
            var commandMiddlewareType = typeof(WebSocketCommandMiddleware <,>).MakeGenericType(keyType, typeof(TPackageInfo));

            return(builder.ConfigureServices((ctx, services) =>
            {
                services.AddSingleton(typeof(IWebSocketCommandMiddleware), commandMiddlewareType);
                services.AddSingleton <IPackageMapper <WebSocketPackage, TPackageInfo>, TPackageMapper>();
            }).ConfigureServices((ctx, services) =>
            {
                services.Configure <CommandOptions>(ctx.Configuration?.GetSection("serverOptions")?.GetSection("commands"));
            }) as ISuperSocketHostBuilder <WebSocketPackage>);
        }
Пример #22
0
        // move to extensions
        public static ISuperSocketHostBuilder <TReceivePackage> UsePackageHandler <TReceivePackage>(this ISuperSocketHostBuilder <TReceivePackage> hostBuilder, Func <IAppSession, TReceivePackage, ValueTask> packageHandler, Func <IAppSession, PackageHandlingException <TReceivePackage>, ValueTask <bool> > errorHandler = null)
        {
            return(hostBuilder.ConfigureServices(
                       (hostCtx, services) =>
            {
                if (packageHandler != null)
                {
                    services.AddSingleton <IPackageHandler <TReceivePackage> >(new DelegatePackageHandler <TReceivePackage>(packageHandler));
                }

                if (errorHandler != null)
                {
                    services.AddSingleton <Func <IAppSession, PackageHandlingException <TReceivePackage>, ValueTask <bool> > >(errorHandler);
                }
            }
                       ));
        }
        public static ISuperSocketHostBuilder <WebSocketPackage> UseCommand <TPackageInfo, TPackageMapper>(this ISuperSocketHostBuilder <WebSocketPackage> builder, string protocol, Action <CommandOptions> commandOptionsAction = null)
            where TPackageInfo : class
            where TPackageMapper : class, IPackageMapper <WebSocketPackage, TPackageInfo>
        {
            return(builder.ConfigureServices((ctx, services) =>
            {
                var commandOptions = new CommandOptions();
                ctx.Configuration?.GetSection("serverOptions")?.GetSection("commands")?.GetSection(protocol)?.Bind(commandOptions);
                commandOptionsAction?.Invoke(commandOptions);
                var commandOptionsWraper = new OptionsWrapper <CommandOptions>(commandOptions);

                services.TryAddEnumerable(ServiceDescriptor.Singleton <ISubProtocolHandler, CommandSubProtocolHandler <TPackageInfo> >((sp) =>
                {
                    var mapper = ActivatorUtilities.CreateInstance <TPackageMapper>(sp);
                    return new CommandSubProtocolHandler <TPackageInfo>(protocol, sp, commandOptionsWraper, mapper);
                }));
            }) as ISuperSocketHostBuilder <WebSocketPackage>);
        }
Пример #24
0
        public static ISuperSocketHostBuilder UseUdp(this ISuperSocketHostBuilder hostBuilder)
        {
            return((hostBuilder.ConfigureServices((context, services) =>
            {
                services.AddSingleton <IChannelCreatorFactory, UdpChannelCreatorFactory>();
                services.AddSingleton <IUdpSessionIdentifierProvider, IPAddressUdpSessionIdentifierProvider>();
            }) as ISuperSocketHostBuilder).ConfigureSupplementServices((context, services) =>
            {
                if (services.Any(s => s.ServiceType == typeof(IAsyncSessionContainer)))
                {
                    return;
                }

                services.TryAddEnumerable(ServiceDescriptor.Singleton <IMiddleware, InProcSessionContainerMiddleware>(s => s.GetRequiredService <InProcSessionContainerMiddleware>()));
                services.AddSingleton <InProcSessionContainerMiddleware>();
                services.AddSingleton <ISessionContainer>((s) => s.GetRequiredService <InProcSessionContainerMiddleware>());
                services.AddSingleton <IAsyncSessionContainer>((s) => s.GetRequiredService <ISessionContainer>().ToAsyncSessionContainer());
            }));
        }
        public override void Configure(ISuperSocketHostBuilder hostBuilder)
        {
            hostBuilder.ConfigureServices((ctx, services) =>
            {
                services.Configure <ServerOptions>((options) =>
                {
                    var listener = options.Listeners[0];

                    if (listener.Security == SslProtocols.None)
                    {
                        listener.Security = GetServerEnabledSslProtocols();
                    }

                    listener.CertificateOptions = new CertificateOptions
                    {
                        FilePath = "supersocket.pfx",
                        Password = "******"
                    };
                });
            });

            base.Configure(hostBuilder);
        }