示例#1
0
        /// <inheritdoc />
        public void Start()
        {
            try
            {
                ChannelOption optionReceive = new ChannelOption(ChannelOptions.MaxReceiveMessageLength, GrpcStream.MaxPackageSize);
                ChannelOption optionSend    = new ChannelOption(ChannelOptions.MaxSendMessageLength, GrpcStream.MaxPackageSize);

                if (System.IO.File.Exists("ca.crt"))
                {
                    var cacert         = System.IO.File.ReadAllText(@"ca.crt");
                    var servercert     = System.IO.File.ReadAllText(@"server.crt");
                    var serverkey      = System.IO.File.ReadAllText(@"server.key");
                    var keypair        = new KeyCertificatePair(servercert, serverkey);
                    var sslCredentials = new SslServerCredentials(new List <KeyCertificatePair>()
                    {
                        keypair
                    }, cacert, false);

                    this.grpcServer = new Server
                    {
                        Services = { GrpcServer.BindService(this) },
                        Ports    = { new ServerPort(this.Host.Address.ToString(), this.Host.Port, sslCredentials) }
                    };
                }
                else
                {
                    this.grpcServer = new Server(new List <ChannelOption>()
                    {
                        optionReceive, optionSend
                    })
                    {
                        Services = { GrpcServer.BindService(this) },
                        Ports    = { new ServerPort(this.Host.Address.ToString(), this.Host.Port, ServerCredentials.Insecure) }
                    };
                }
                this.grpcServer.Start();

                this.Log(this.ToString(), "GRPC-Server server listening on port " + this.Host.Port, LogLevel.Info);
            }
            catch (Exception ex)
            {
                throw new Exception("Grpc server error", ex);
            }
        }