Пример #1
0
 public GrpcHostBuilder(IServiceProvider serviceProvider, ISerializer serializer, IOptions <RpcServerOptions> options)
 {
     this._serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
     this._builder         = ServerServiceDefinition.CreateBuilder();
     this._serializer      = serializer;
     this._options         = options.Value;
 }
Пример #2
0
        static void Main(string[] args)
        {
            var options = new RpcServerOptions();

            options.callback = Program.Callback;
            var rpcServer = RpcServer.Create(options);
        }
Пример #3
0
        public TcpServerTransport(
			ServerEventLoop eventLoop,
			RpcServerOptions options,
			RequestMessageSerializer requestMessageSerializer,
			ResponseMessageSerializer responseMessageSerializer
		)
        {
            if ( eventLoop == null )
            {
                throw new ArgumentNullException( "eventLoop" );
            }

            if ( requestMessageSerializer == null )
            {
                throw new ArgumentNullException( "requestMessageSerializer" );
            }

            if ( responseMessageSerializer == null )
            {
                throw new ArgumentNullException( "responseMessageSerializer" );
            }

            Contract.EndContractBlock();

            this._eventLoop = eventLoop;
            this._options = options;
            this._requestSerializer = requestMessageSerializer;
            this._responseSerializer = responseMessageSerializer;
        }
Пример #4
0
 public GrpcFullStackTests(IRpcSerializer serializer)
 {
     this.options = new RpcServerOptions {
         Serializer = serializer
     };
     this.clientOptions = new RpcClientOptions {
         Serializer = serializer
     };
 }
Пример #5
0
        public DefaultServiceRegister(
            IServiceRegistrationProvider registrationProvider,
            IServiceProvider serviceProvider,
            IOptions <RpcServerOptions> options, ILogger <DefaultServiceRegister> logger)
        {
            this._registrationProvider = registrationProvider;
            this._serviceProvider      = serviceProvider;
            this._logger       = logger;
            this._optionValues = options.Value;

            this._localBindAddress = new IPEndPoint(GetBindAddress(), this._optionValues.Port);
        }
Пример #6
0
        public void GlobalSetup()
        {
            var serverId            = RpcServerId.NewId();
            var definitionsProvider = new RpcServiceDefinitionsBuilder();
            // var serializer = new JsonRpcSerializer();
            var serializer    = new ProtobufRpcSerializer(RuntimeTypeModel.Create());
            var serverOptions = new RpcServerOptions {
                Serializer = serializer
            };

            this.clientOptions = new RpcClientOptions {
                Serializer = serializer
            }.AsImmutable();

            switch (this.ConnectionType)
            {
            //case RpcConnectionType.LightweightInproc:
            //    {
            //        var connector = new InprocRpcConnector(clientOptions);
            //        this.server = new LightweightRpcServer(serverId, definitionsProvider, null, serverOptions);
            //        this.server.AddEndPoint(connector.EndPoint);
            //        this.server.ServicePublisher.PublishSingleton<ISimpleService>(new SimpleServiceImpl());
            //        this.server.Start();

            //        //this.clientConnection = connector.Connection;
            //        //clientService = this.clientConnection.GetServiceSingleton<ISimpleServiceClient>();
            //        break;
            //    }
            case RpcConnectionType.LightweightTcp:
            {
                this.server = new LightweightRpcServer(serverId, definitionsProvider, null, serverOptions);
                this.server.AddEndPoint(new TcpRpcEndPoint("127.0.0.1", 50051, false));
                this.server.ServicePublisher.PublishSingleton <ISimpleService>(new SimpleServiceImpl());
                this.server.Start();

                this.connectionInfo = new RpcConnectionInfo(new Uri("lightweight.tcp://localhost:50051"));
                break;
            }

            case RpcConnectionType.LightweightNamedPipe:
            {
                this.server = new LightweightRpcServer(serverId, definitionsProvider, null, serverOptions);
                this.server.AddEndPoint(new NamedPipeRpcEndPoint("RpcBenchmark"));
                this.server.ServicePublisher.PublishSingleton <ISimpleService>(new SimpleServiceImpl());
                this.server.Start();

                this.connectionInfo = new RpcConnectionInfo(new Uri($"{WellKnownRpcSchemes.LightweightPipe}://./RpcBenchmark"));
                break;
            }
            }
        }
Пример #7
0
        protected ServerEventLoop( Func<ServerSessionContext, RpcServerSession> sessionFactory, RpcTransportProtocol protocol, RpcServerOptions options, EventHandler<RpcTransportErrorEventArgs> errorHandler )
            : base(errorHandler)
        {
            if ( sessionFactory == null )
            {
                throw new ArgumentNullException( "sessionFactory" );
            }

            Contract.EndContractBlock();

            this._sessionFactory = sessionFactory;
            this._options = options;
            this._exeuctingWorkerThreadTable = new ConcurrentDictionary<int, WorkerThreadInfo>();
            this._sessionPool = new ConcurrentDictionary<EndPoint, RpcServerSession>();
            this._timeoutWatchDog = new Timer( this.CheckTimeout, null, ( long )this.TimeoutWatchPeriod.TotalMilliseconds, Timeout.Infinite );
        }
        private static IRpcServerHost CreateNetGrpcServer(
            IRpcServiceDefinitionsProvider serviceDefinitionsProvider,
            RpcServerId serverId,
            RpcServerOptions options,
            Action <IServiceCollection> configureServices)
        {
            var hostBuilder = WebHost.CreateDefaultBuilder()
                              .ConfigureKestrel(options =>
            {
                options.ListenLocalhost(GrpcCoreFullStackTestsBase.GrpcTestPort, listenOptions =>
                {
                    listenOptions.UseHttps(TestCertificates.ServerPFXPath, "1111");
                    //listenOptions.UseHttps(certPath, "1111", o =>
                    //{
                    //    o.ClientCertificateMode = ClientCertificateMode.AllowCertificate;
                    //});
                    listenOptions.Protocols = HttpProtocols.Http2;
                });
            })
                              .ConfigureLogging(b => ConfigureLogging(b))
                              .ConfigureServices(s =>
            {
                s.AddSingleton(serviceDefinitionsProvider ?? new RpcServiceDefinitionsBuilder());
                s.Configure <RpcServicePublisherOptions>(o => o.ServerId = serverId);
                s.AddSingleton <IOptions <RpcServerOptions> >(new OptionsWrapper <RpcServerOptions>(options));

                configureServices?.Invoke(s);
            })
                              .UseStartup <NetStartup>();

            var host = hostBuilder.Build();

            var rpcServer = (NetGrpcServer)host.Services.GetService(typeof(NetGrpcServer));

            return(new NetGrpcTestServer(host, rpcServer));
        }
Пример #9
0
 protected virtual string GetServiceName(RpcServerOptions optionValue, IServiceActor <AmpMessage> actor)
 {
     return($"{optionValue.AppName}-{actor.GroupName}");
 }
 public IOCompletionPortServerEventLoop( Func<ServerSessionContext, RpcServerSession> sessionFactory, RpcTransportProtocol protocol, RpcServerOptions options, EventHandler<RpcTransportErrorEventArgs> errorHandler )
     : base(sessionFactory, protocol, options, errorHandler)
 {
 }
        /// <summary>
        /// TODO: Use factories instead of using this.connnectionType.
        /// </summary>
        /// <param name="serviceDefinitionsProvider"></param>
        /// <param name="proxyDefinitionsProvider"></param>
        /// <returns></returns>
        protected (IRpcServerHost, IRpcChannel) CreateServerAndConnection(
            IRpcServiceDefinitionsProvider serviceDefinitionsProvider = null,
            Action <RpcServerOptions> configServerOptions             = null,
            Action <RpcClientOptions> configClientOptions             = null,
            Action <IServiceCollection> configureServices             = null)
        {
            var rpcServerId = RpcServerId.NewId();

            var serverOptions = new RpcServerOptions {
                Serializer = this.serializer
            };
            var clientOptions = new RpcClientOptions {
                Serializer = this.serializer
            };

            configServerOptions?.Invoke(serverOptions);
            configClientOptions?.Invoke(clientOptions);

            IServiceProvider services = GetServiceProvider(configureServices);

            switch (this.ConnectionType)
            {
            case RpcConnectionType.LightweightTcp:
            case RpcConnectionType.LightweightSslTcp:
            {
                var host = new LightweightRpcServer(rpcServerId, serviceDefinitionsProvider, services, serverOptions, this.LightweightOptions,
                                                    loggerFactory: services.GetService <ILoggerFactory>());

                SslServerOptions sslServerOptions = null;
                if (this.ConnectionType == RpcConnectionType.LightweightSslTcp)
                {
                    sslServerOptions = new SslServerOptions(new X509Certificate2(TestCertificates.ServerPFXPath, "1111"));
                }

                host.AddEndPoint(new TcpRpcEndPoint("127.0.0.1", TcpTestPort, false, sslServerOptions));

                SslClientOptions sslClientOptions = null;
                if (this.ConnectionType == RpcConnectionType.LightweightSslTcp)
                {
                    sslClientOptions = new SslClientOptions {
                        RemoteCertificateValidationCallback = this.ValidateTestCertificate
                    };
                }
                var connection = new TcpRpcConnection(
                    new RpcConnectionInfo("TCP", new Uri($"lightweight.tcp://127.0.0.1:{TcpTestPort}"), rpcServerId),
                    sslClientOptions,
                    clientOptions.AsImmutable(),
                    this.LightweightOptions);

                return(host, connection);
            }

            case RpcConnectionType.LightweightNamedPipe:
            {
                var server = new LightweightRpcServer(rpcServerId, serviceDefinitionsProvider, services, serverOptions, this.LightweightOptions,
                                                      loggerFactory: services.GetService <ILoggerFactory>());
                server.AddEndPoint(new NamedPipeRpcEndPoint("testpipe"));

                var connection = new NamedPipeRpcConnection(
                    new RpcConnectionInfo(new Uri("lightweight.pipe://./testpipe")),
                    clientOptions.AsImmutable(),
                    this.LightweightOptions);

                return(server, connection);
            }

            case RpcConnectionType.LightweightInproc:
            {
                Pipe requestPipe  = new Pipe(new PipeOptions(readerScheduler: PipeScheduler.ThreadPool));
                Pipe responsePipe = new Pipe(new PipeOptions(readerScheduler: PipeScheduler.Inline));

                var host = new LightweightRpcServer(rpcServerId, serviceDefinitionsProvider, services, serverOptions,
                                                    loggerFactory: services.GetService <ILoggerFactory>());
                host.AddEndPoint(new InprocRpcEndPoint(new DirectDuplexPipe(requestPipe.Reader, responsePipe.Writer)));

                var connection = new InprocRpcConnection(new RpcConnectionInfo("Direct", new Uri("direct:localhost"), rpcServerId),
                                                         new DirectDuplexPipe(responsePipe.Reader, requestPipe.Writer), clientOptions.AsImmutable());
                return(host, connection);
            }

            case RpcConnectionType.Grpc:
            {
                var host = new GrpcServer(rpcServerId, serviceDefinitionsProvider, services, serverOptions);
                host.AddEndPoint(GrpcCoreFullStackTestsBase.CreateEndPoint());

                var connection = new GrpcConnection(
                    new RpcConnectionInfo("TCP", new Uri($"grpc://localhost:{GrpcCoreFullStackTestsBase.GrpcTestPort}"), rpcServerId),
                    TestCertificates.GrpcSslCredentials, clientOptions.AsImmutable());
                return(host, connection);
            }

#if PLAT_NET_GRPC
            case RpcConnectionType.NetGrpc:
            {
                var server = CreateNetGrpcServer(serviceDefinitionsProvider, rpcServerId, serverOptions, configureServices);
                //var host = new GrpcServer(rpcServerId, serviceDefinitionsBuilder, null, options);
                //host.AddEndPoint(GrpcCoreFullStackTestsBase.CreateEndPoint());

                var handler = new System.Net.Http.HttpClientHandler();
                handler.ServerCertificateCustomValidationCallback =
                    (httpRequestMessage, cert, cetChain, policyErrors) =>
                {
                    return(true);
                };
                var channelOptions = new GrpcNet.Client.GrpcChannelOptions()
                {
                    HttpClient        = new System.Net.Http.HttpClient(handler),
                    DisposeHttpClient = true
                };


                var connection = new NetGrpcConnection(
                    new RpcConnectionInfo("net-grpc", new Uri($"grpc://localhost:{GrpcCoreFullStackTestsBase.GrpcTestPort}"), rpcServerId),
                    clientOptions.AsImmutable(), channelOptions);
                return(server, connection);
            }
#endif
            }

            throw new NotSupportedException();
        }
 public NetGrpcServiceActivatorConfig(IOptions <RpcServiceOptions <TService> > options)
 {
     this.rpcOptions = options.Value;
 }