Example #1
0
        protected override ValueTask <ConnectState> ConnectAsync(EndPoint remoteEndPoint, ConnectState state, CancellationToken cancellationToken)
        {
            var stream = state.Stream;

            if (stream == null)
            {
                if (state.Socket != null)
                {
                    stream = new NetworkStream(state.Socket, true);
                }
                else
                {
                    throw new Exception("Stream from previous connector is null.");
                }
            }

            try
            {
                var gzipStream = new GZipReadWriteStream(stream, true);
                state.Stream = gzipStream;
                return(new ValueTask <ConnectState>(state));
            }
            catch (Exception e)
            {
                return(new ValueTask <ConnectState>(new ConnectState
                {
                    Result = false,
                    Exception = e
                }));
            }
        }
        public new IChannelCreator CreateChannelCreator <TPackageInfo>(ListenOptions options, ChannelOptions channelOptions, ILoggerFactory loggerFactory, object pipelineFilterFactory)
        {
            var filterFactory = pipelineFilterFactory as IPipelineFilterFactory <TPackageInfo>;

            channelOptions.Logger = loggerFactory.CreateLogger(nameof(IChannel));

            var channelFactoryLogger = loggerFactory.CreateLogger(nameof(TcpChannelCreator));

            var channelFactory = new Func <Socket, ValueTask <IChannel> >(async(s) =>
            {
                ApplySocketOptions(s, options, channelOptions, channelFactoryLogger);

                Stream stream = new NetworkStream(s, true);
                if (options.Security != SslProtocols.None)
                {
                    var authOptions = new SslServerAuthenticationOptions();

                    authOptions.EnabledSslProtocols       = options.Security;
                    authOptions.ServerCertificate         = options.CertificateOptions.Certificate;
                    authOptions.ClientCertificateRequired = options.CertificateOptions.ClientCertificateRequired;

                    if (options.CertificateOptions.RemoteCertificateValidationCallback != null)
                    {
                        authOptions.RemoteCertificateValidationCallback = options.CertificateOptions.RemoteCertificateValidationCallback;
                    }

                    var sslStream = new SslStream(stream, false);
                    await sslStream.AuthenticateAsServerAsync(authOptions, CancellationToken.None).ConfigureAwait(false);

                    stream = sslStream;
                }
                stream = new GZipReadWriteStream(stream, true);
                return(new StreamPipeChannel <TPackageInfo>(stream, s.RemoteEndPoint, s.LocalEndPoint, filterFactory.Create(s), channelOptions));
            });

            return(new TcpChannelCreator(options, channelFactory, channelFactoryLogger));
        }