@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("mqtt-decoder", new MqttDecoder()); pipeline.addLast("mqtt-encoder", MqttEncoder.INSTANCE); pipeline.addLast("channel-idle-handler", new MqttIdleHandler()); pipeline.addLast("message-dispatcher", messageDispatcher); pipeline.addLast("connection-manager", connectionHandler); } });
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", new MqttDecoder()); pipeline.addLast("encoder", MqttEncoder.INSTANCE); pipeline.addLast("handler", handler); } });
private void configureMQTTPipeline(ChannelPipeline pipeline, MoquetteIdleTimeoutHandler timeoutHandler, NewNettyMQTTHandler handler) { pipeline.addFirst("idleStateHandler", new IdleStateHandler(nettyChannelTimeoutSeconds, 0, 0)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); if (errorsCather.isPresent()) { pipeline.addLast("bugsnagCatcher", errorsCather.get()); } pipeline.addFirst("bytemetrics", new BytesMetricsHandler(bytesMetricsCollector)); pipeline.addLast("autoflush", new AutoFlushHandler(1, TimeUnit.SECONDS)); pipeline.addLast("decoder", new MqttDecoder(maxBytesInMessage)); pipeline.addLast("encoder", MqttEncoder.INSTANCE); pipeline.addLast("metrics", new MessageMetricsHandler(metricsCollector)); pipeline.addLast("messageLogger", new MQTTMessageLogger()); if (metrics.isPresent()) { pipeline.addLast("wizardMetrics", metrics.get()); } pipeline.addLast("handler", handler); }
@Override public void addChannelHandlers(ChannelPipeline pipeline) { pipeline.addLast(MqttEncoder.INSTANCE); pipeline.addLast(new MqttDecoder(MQTTUtil.MAX_MESSAGE_SIZE)); pipeline.addLast(new MQTTProtocolHandler(server, this)); }
@Override public void addChannelHandlers(ChannelPipeline pipeline) { pipeline.addLast(MqttEncoder.INSTANCE); pipeline.addLast(new MqttDecoder(MQTTUtil.MAX_MESSAGE_SIZE)); pipeline.addLast(new MQTTProtocolHandler(server, this)); }
@Override protected void initChannel(ChannelPipeline pipeline) { if (logEnabled) { pipeline.addLast("logging", new LoggingHandler()); } pipeline.addLast("mqttEncoder", MqttEncoder.INSTANCE); if (this.mqttServerOptions.getMaxMessageSize() > 0) { pipeline.addLast("mqttDecoder", new MqttDecoder(this.mqttServerOptions.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addLast("mqttDecoder", new MqttDecoder()); } }
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // ssl if (ssl) { p.addLast("ssl", sslContext.newHandler(ch.alloc())); } // idle p.addFirst("idleHandler", new IdleStateHandler(0, 0, keepAlive)); // mqtt encoder & decoder p.addLast("encoder", MqttEncoder.INSTANCE); p.addLast("decoder", new MqttDecoder()); // logic handler // p.addLast(handlerGroup, "logicHandler", new SyncRedisHandler(authenticator, cluster, storage, registry, validator, brokerId, keepAlive, keepAliveMax)); p.addLast("logicHandler", new SyncStorageHandler(authenticator, cluster, storage, registry, validator, brokerId, keepAlive, keepAliveMax)); } })
/** * @param channelPipeline channelPipeline * @param clientBean 客户端配置参数 */ protected void initHandler(ChannelPipeline channelPipeline, ConnectOptions clientBean, MqttHander mqttHander){ if(clientBean.isSsl()){ initSsl(); SSLEngine engine = CLIENT_CONTEXT.createSSLEngine(); engine.setUseClientMode(true); channelPipeline.addLast("ssl", new SslHandler(engine)); } channelPipeline.addLast("decoder", new MqttDecoder()); channelPipeline.addLast("encoder", MqttEncoder.INSTANCE); channelPipeline.addLast(new IdleStateHandler(clientBean.getHeart(),0,0)); channelPipeline.addLast(mqttHander); }
@Override void init(ChannelPipeline pipeline) { pipeline.addFirst("idleStateHandler", new IdleStateHandler(nettyChannelTimeoutSeconds, 0, 0)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); if (errorsCather.isPresent()) { pipeline.addLast("bugsnagCatcher", errorsCather.get()); } pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MqttDecoder(maxBytesInMessage)); pipeline.addLast("encoder", MqttEncoder.INSTANCE); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("messageLogger", new MQTTMessageLogger()); if (metrics.isPresent()) { pipeline.addLast("wizardMetrics", metrics.get()); } pipeline.addLast("handler", handler); } });
@Override void init(ChannelPipeline pipeline) throws Exception { pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth)); pipeline.addFirst("idleStateHandler", new IdleStateHandler(nettyChannelTimeoutSeconds, 0, 0)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); // pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR)); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MqttDecoder(maxBytesInMessage)); pipeline.addLast("encoder", MqttEncoder.INSTANCE); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("messageLogger", new MQTTMessageLogger()); pipeline.addLast("handler", handler); } });
private void intProtocolHandler(ChannelPipeline channelPipeline,InitBean serverBean){ switch (serverBean.getProtocol()){ case MQTT: channelPipeline.addLast("encoder", MqttEncoder.INSTANCE); channelPipeline.addLast("decoder", new MqttDecoder()); break; case MQTT_WS_MQTT: channelPipeline.addLast("httpCode", new HttpServerCodec()); channelPipeline.addLast("aggregator", new HttpObjectAggregator(65536)); channelPipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/", MQTT_CSV_LIST)); channelPipeline.addLast("wsDecoder", new WebSocketFrameToByteBufDecoder()); channelPipeline.addLast("wsEncoder", new ByteBufToWebSocketFrameEncoder()); channelPipeline.addLast("decoder", new MqttDecoder()); channelPipeline.addLast("encoder", MqttEncoder.INSTANCE); break; case MQTT_WS_PAHO: channelPipeline.addLast("httpCode", new HttpServerCodec()); channelPipeline.addLast("aggregator", new HttpObjectAggregator(65536)); channelPipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_CSV_LIST)); channelPipeline.addLast("wsDecoder", new WebSocketFrameToByteBufDecoder()); channelPipeline.addLast("wsEncoder", new ByteBufToWebSocketFrameEncoder()); channelPipeline.addLast("decoder", new MqttDecoder()); channelPipeline.addLast("encoder", MqttEncoder.INSTANCE); break; } }
@Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline channelPipeline = socketChannel.pipeline(); // Netty提供的心跳检测 channelPipeline.addFirst("idle", new IdleStateHandler(0, 0, brokerProperties.getKeepAlive())); // Netty提供的SSL处理 if (brokerProperties.getSslEnabled()) { SSLEngine sslEngine = sslContext.newEngine(socketChannel.alloc()); sslEngine.setUseClientMode(false); // 服务端模式 sslEngine.setNeedClientAuth(false); // 不需要验证客户端 channelPipeline.addLast("ssl", new SslHandler(sslEngine)); } channelPipeline.addLast("decoder", new MqttDecoder()); channelPipeline.addLast("encoder", MqttEncoder.INSTANCE); channelPipeline.addLast("broker", ioc.get(BrokerHandler.class)); } })
@Override void init(ChannelPipeline pipeline) { pipeline.addLast(new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST)); pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder()); pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder()); pipeline.addFirst("idleStateHandler", new IdleStateHandler(nettyChannelTimeoutSeconds, 0, 0)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MqttDecoder(maxBytesInMessage)); pipeline.addLast("encoder", MqttEncoder.INSTANCE); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("messageLogger", new MQTTMessageLogger()); pipeline.addLast("handler", handler); } });
@Override void init(ChannelPipeline pipeline) throws Exception { pipeline.addLast("ssl", createSslHandler(sslContext, needsClientAuth)); pipeline.addLast("httpEncoder", new HttpResponseEncoder()); pipeline.addLast("httpDecoder", new HttpRequestDecoder()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler("/mqtt", MQTT_SUBPROTOCOL_CSV_LIST)); pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder()); pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder()); pipeline.addFirst("idleStateHandler", new IdleStateHandler(nettyChannelTimeoutSeconds, 0, 0)); pipeline.addAfter("idleStateHandler", "idleEventHandler", timeoutHandler); pipeline.addFirst("bytemetrics", new BytesMetricsHandler(m_bytesMetricsCollector)); pipeline.addLast("decoder", new MqttDecoder(maxBytesInMessage)); pipeline.addLast("encoder", MqttEncoder.INSTANCE); pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector)); pipeline.addLast("messageLogger", new MQTTMessageLogger()); pipeline.addLast("handler", handler); } });
@Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline channelPipeline = socketChannel.pipeline(); // Netty提供的心跳检测 channelPipeline.addFirst("idle", new IdleStateHandler(0, 0, brokerProperties.getKeepAlive())); // Netty提供的SSL处理 if (brokerProperties.getSslEnabled()) { SSLEngine sslEngine = sslContext.newEngine(socketChannel.alloc()); sslEngine.setUseClientMode(false); // 服务端模式 sslEngine.setNeedClientAuth(false); // 不需要验证客户端 channelPipeline.addLast("ssl", new SslHandler(sslEngine)); } // 将请求和应答消息编码或解码为HTTP消息 channelPipeline.addLast("http-codec", new HttpServerCodec()); // 将HTTP消息的多个部分合成一条完整的HTTP消息 channelPipeline.addLast("aggregator", new HttpObjectAggregator(1048576)); // 将HTTP消息进行压缩编码 channelPipeline.addLast("compressor ", new HttpContentCompressor()); channelPipeline.addLast("protocol", new WebSocketServerProtocolHandler(brokerProperties.getWebsocketPath(), "mqtt,mqttv3.1,mqttv3.1.1", true, 65536)); channelPipeline.addLast("mqttWebSocket", new MqttWebSocketCodec()); channelPipeline.addLast("decoder", new MqttDecoder()); channelPipeline.addLast("encoder", MqttEncoder.INSTANCE); channelPipeline.addLast("broker", ioc.get(BrokerHandler.class)); } })
private void initChannel(ChannelPipeline pipeline) { pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } // adding the idle state handler for timeout on CONNECT packet pipeline.addBefore("handler", "idle", new IdleStateHandler(this.options.timeoutOnConnect(), 0, 0)); pipeline.addBefore("handler", "timeoutOnConnect", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.READER_IDLE) { // as MQTT 3.1.1 describes, if no packet is sent after a "reasonable" time (here CONNECT timeout) // the connection is closed ctx.channel().close(); } } } }); } }
private void initChannel(ChannelPipeline pipeline) { pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } // adding the idle state handler for timeout on CONNECT packet pipeline.addBefore("handler", "idle", new IdleStateHandler(this.options.timeoutOnConnect(), 0, 0)); pipeline.addBefore("handler", "timeoutOnConnect", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.READER_IDLE) { // as MQTT 3.1.1 describes, if no packet is sent after a "reasonable" time (here CONNECT timeout) // the connection is closed ctx.channel().close(); } } } }); } }
private void initChannel(ChannelPipeline pipeline) { // add into pipeline netty's (en/de)coder pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } if (this.options.isAutoKeepAlive() && this.options.getKeepAliveTimeSeconds() != 0) { pipeline.addBefore("handler", "idle", new IdleStateHandler(0, this.options.getKeepAliveTimeSeconds(), 0)); pipeline.addBefore("handler", "keepAliveHandler", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.WRITER_IDLE) { ping(); } } } }); } }
private void initChannel(ChannelPipeline pipeline) { // add into pipeline netty's (en/de)coder pipeline.addBefore("handler", "mqttEncoder", MqttEncoder.INSTANCE); if (this.options.getMaxMessageSize() > 0) { pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder(this.options.getMaxMessageSize())); } else { // max message size not set, so the default from Netty MQTT codec is used pipeline.addBefore("handler", "mqttDecoder", new MqttDecoder()); } if (this.options.isAutoKeepAlive() && this.options.getKeepAliveTimeSeconds() != 0) { pipeline.addBefore("handler", "idle", new IdleStateHandler(0, this.options.getKeepAliveTimeSeconds(), 0)); pipeline.addBefore("handler", "keepAliveHandler", new ChannelDuplexHandler() { @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof IdleStateEvent) { IdleStateEvent e = (IdleStateEvent) evt; if (e.state() == IdleState.WRITER_IDLE) { ping(); } } } }); } }