Esempio n. 1
0
        public async Task ProcessWebSocketRequestAsync(WebSocket webSocket,
                                                       Option <EndPoint> localEndPoint,
                                                       EndPoint remoteEndPoint,
                                                       string correlationId,
                                                       Option <X509Certificate2> clientCert,
                                                       Option <IList <X509Certificate2> > clientCertChain)
        {
            try
            {
                DeviceIdentityProvider identityProvider = new DeviceIdentityProvider(this.authenticator, this.clientCredentialsFactory, true);
                var serverChannel = new ServerWebSocketChannel(
                    webSocket,
                    remoteEndPoint
                    );

                clientCert.Map(cert =>
                {
                    return(identityProvider.RemoteCertificateValidationCallback(cert, clientCertChain.Expect(() => new ArgumentException("Certificate chain was found to be null"))));
                });

                serverChannel
                .Option(ChannelOption.Allocator, this.byteBufferAllocator)
                .Option(ChannelOption.AutoRead, this.autoRead)
                .Option(ChannelOption.RcvbufAllocator, new AdaptiveRecvByteBufAllocator())
                .Option(ChannelOption.MessageSizeEstimator, DefaultMessageSizeEstimator.Default)
                .Pipeline.AddLast(
                    MqttEncoder.Instance,
                    new MqttDecoder(true, this.mqttDecoderMaxMessageSize),
                    new MqttAdapter(
                        this.settings,
                        this.sessionProviderFactory(),
                        identityProvider,
                        null,
                        this.messagingBridgeFactoryFunc));

                await this.workerGroup.GetNext().RegisterAsync(serverChannel);

                Events.Established(correlationId);

                await serverChannel.WebSocketClosed.Task;  // This will wait until the websocket is closed
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                Events.Exception(correlationId, ex);
                throw;
            }
        }
Esempio n. 2
0
        public async Task ProcessWebSocketRequestAsyncInternal(
            DeviceIdentityProvider identityProvider,
            WebSocket webSocket,
            Option <EndPoint> localEndPoint,
            EndPoint remoteEndPoint,
            string correlationId)
        {
            try
            {
                var serverChannel = new ServerWebSocketChannel(webSocket, remoteEndPoint);

                serverChannel
                .Option(ChannelOption.Allocator, this.byteBufferAllocator)
                .Option(ChannelOption.AutoRead, this.autoRead)
                .Option(ChannelOption.RcvbufAllocator, new AdaptiveRecvByteBufAllocator())
                .Option(ChannelOption.MessageSizeEstimator, DefaultMessageSizeEstimator.Default)
                .Pipeline.AddLast(
                    MqttEncoder.Instance,
                    new MqttDecoder(true, this.mqttDecoderMaxMessageSize),
                    new MqttAdapter(
                        this.settings,
                        this.sessionProviderFactory(),
                        identityProvider,
                        null,
                        this.messagingBridgeFactoryFunc));

                await this.workerGroup.GetNext().RegisterAsync(serverChannel);

                Events.Established(correlationId);

                await serverChannel.WebSocketClosed.Task; // This will wait until the websocket is closed
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                Events.Exception(correlationId, ex);
                throw;
            }
        }
Esempio n. 3
0
        public async Task ProcessWebSocketRequestAsync(HttpContext context, string correlationId)
        {
            try
            {
                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(this.SubProtocol);

                var serverChannel = new ServerWebSocketChannel(
                    webSocket,
                    new IPEndPoint(context.Connection.RemoteIpAddress, context.Connection.RemotePort)
                    );

                serverChannel
                .Option(ChannelOption.Allocator, this.byteBufferAllocator)
                .Option(ChannelOption.AutoRead, this.autoRead)
                .Option(ChannelOption.RcvbufAllocator, new AdaptiveRecvByteBufAllocator())
                .Option(ChannelOption.MessageSizeEstimator, DefaultMessageSizeEstimator.Default)
                .Pipeline.AddLast(
                    MqttEncoder.Instance,
                    new MqttDecoder(true, this.mqttDecoderMaxMessageSize),
                    new MqttAdapter(
                        this.settings,
                        this.sessionProviderFactory(),
                        this.identityProvider,
                        null,
                        this.messagingBridgeFactoryFunc));

                await this.workerGroup.GetNext().RegisterAsync(serverChannel);

                Events.Established(context.Request.Host.Host, correlationId);

                await serverChannel.WebSocketClosed.Task;  // This will wait until the websocket is closed
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                Events.Exception(context.Request.Host.Host, correlationId, ex);
                throw;
            }
        }