Search in sources :

Example 41 with StringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project dubbo by alibaba.

the class QosProcessHandler method decode.

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (in.readableBytes() < 1) {
        return;
    }
    // read one byte to guess protocol
    final int magic = in.getByte(in.readerIndex());
    ChannelPipeline p = ctx.pipeline();
    p.addLast(new LocalHostPermitHandler(acceptForeignIp));
    if (isHttp(magic)) {
        // no welcome output for http protocol
        if (welcomeFuture != null && welcomeFuture.isCancellable()) {
            welcomeFuture.cancel(false);
        }
        p.addLast(new HttpServerCodec());
        p.addLast(new HttpObjectAggregator(1048576));
        p.addLast(new HttpProcessHandler());
        p.remove(this);
    } else {
        p.addLast(new LineBasedFrameDecoder(2048));
        p.addLast(new StringDecoder(CharsetUtil.UTF_8));
        p.addLast(new StringEncoder(CharsetUtil.UTF_8));
        p.addLast(new IdleStateHandler(0, 0, 5 * 60));
        p.addLast(new TelnetProcessHandler());
        p.remove(this);
    }
}
Also used : StringEncoder(io.netty.handler.codec.string.StringEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 42 with StringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project openremote by openremote.

the class TcpStringServer method addDecoders.

@Override
protected void addDecoders(SocketChannel channel) {
    // Add delimiter and string decoders to do the work
    channel.pipeline().addLast(new DelimiterBasedFrameDecoder(maxFrameLength, stripDelimiter, Unpooled.wrappedBuffer(delimiter.getBytes())));
    channel.pipeline().addLast(new StringDecoder());
}
Also used : DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder)

Example 43 with StringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project alibaba-mom by younfor.

the class DefaultProducer method start.

@Override
public void start() {
    SIP = System.getProperty("SIP");
    if (SIP == null)
        SIP = "127.0.0.1";
    System.out.println("connect:" + System.getProperty("SIP"));
    group = new NioEventLoopGroup();
    bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel channel) throws Exception {
            channel.pipeline().addLast(new RpcEncoder()).addLast(new LineBasedFrameDecoder(1024 * 256)).addLast(new StringDecoder()).addLast(new SimpleChannelInboundHandler<Object>() {

                @Override
                public void channelInactive(ChannelHandlerContext ctx) throws Exception {
                    logger.error("producer失去broker链接");
                    connect();
                }

                @Override
                protected void channelRead0(ChannelHandlerContext arg0, Object info) throws Exception {
                    logger.debug("recv ack: " + (String) info);
                    if (info != null) /*&&info.toString().startsWith("ackGroup"*/
                    {
                        String[] id = ((String) info).split("@");
                        // System.out.println(id.length+":len:"+id[0]+" | "+id[1]);
                        if (id[0].startsWith("fail")) {
                            // System.out.println("无人订阅");
                            SendResult sr = new SendResult();
                            sr.setMsgId(id[1]);
                            resultMap.get(id[1]).put(sr);
                        } else {
                            for (int i = 1; i < id.length; i++) {
                                // System.out.println(id[i]);
                                SendResult sr = new SendResult();
                                sr.setMsgId(id[i]);
                                resultMap.get(id[i]).put(sr);
                            }
                        }
                    // synchronized(lock){
                    // lock.notifyAll();
                    // }
                    } else {
                        // 异步方式接受数据
                        if (asyncResults.containsKey((String) info)) {
                            SendResult sr = new SendResult();
                            sr.setMsgId((String) info);
                            asyncResults.get((String) info).onResult(sr);
                            ;
                        } else {
                        }
                    }
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                    logger.error("--生产者的异常关闭--" + cause.getMessage());
                    cause.printStackTrace();
                    logger.error("重新连接");
                    ctx.close();
                    connect();
                }
            });
        }
    });
    connect();
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) RpcEncoder(com.alibaba.middleware.race.mom.serializer.RpcEncoder) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 44 with StringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project java by wavefrontHQ.

the class StringLineIngester method createDecoderList.

/**
 * Returns a copy of the given list plus inserts the 2 decoders needed for this specific ingester
 * (LineBasedFrameDecoder and StringDecoder)
 *
 * @param decoders the starting list
 * @return copy of the provided list with additional decodiers prepended
 */
private static List<Function<Channel, ChannelHandler>> createDecoderList(@Nullable final List<Function<Channel, ChannelHandler>> decoders) {
    final List<Function<Channel, ChannelHandler>> copy;
    if (decoders == null) {
        copy = new ArrayList<>();
    } else {
        copy = new ArrayList<>(decoders);
    }
    copy.add(0, new Function<Channel, ChannelHandler>() {

        @Override
        public ChannelHandler apply(Channel input) {
            return new LineBasedFrameDecoder(4096, true, false);
        }
    });
    copy.add(1, new Function<Channel, ChannelHandler>() {

        @Override
        public ChannelHandler apply(Channel input) {
            return new StringDecoder(Charsets.UTF_8);
        }
    });
    return copy;
}
Also used : Function(com.google.common.base.Function) Channel(io.netty.channel.Channel) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandler(io.netty.channel.ChannelHandler)

Example 45 with StringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project java by wavefrontHQ.

the class HistogramLineIngester method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // Round robin channel to handler assignment.
    int idx = (int) (Math.abs(connectionId.getAndIncrement()) % handlers.size());
    ChannelHandler handler = handlers.get(idx);
    // Add decoders and timeout, add handler()
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new LineBasedFrameDecoder(MAXIMUM_FRAME_LENGTH, true, false), new StringDecoder(Charsets.UTF_8), new IdleStateHandler(CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT, 0, 0), new ChannelDuplexHandler() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
                    logger.warning("terminating connection to histogram client due to inactivity after " + CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT + "s: " + ctx.channel());
                    ctx.close();
                }
            }
        }
    }, handler);
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) BindException(java.net.BindException)

Aggregations

StringDecoder (io.netty.handler.codec.string.StringDecoder)63 StringEncoder (io.netty.handler.codec.string.StringEncoder)42 ChannelPipeline (io.netty.channel.ChannelPipeline)35 SocketChannel (io.netty.channel.socket.SocketChannel)27 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelFuture (io.netty.channel.ChannelFuture)18 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)16 DelimiterBasedFrameDecoder (io.netty.handler.codec.DelimiterBasedFrameDecoder)16 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)15 Bootstrap (io.netty.bootstrap.Bootstrap)14 EventLoopGroup (io.netty.channel.EventLoopGroup)14 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)14 LengthFieldPrepender (io.netty.handler.codec.LengthFieldPrepender)13 LengthFieldBasedFrameDecoder (io.netty.handler.codec.LengthFieldBasedFrameDecoder)12 Channel (io.netty.channel.Channel)11 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)11 LoggingHandler (io.netty.handler.logging.LoggingHandler)8 ChannelHandler (io.netty.channel.ChannelHandler)6 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 Test (org.junit.Test)6