Search in sources :

Example 1 with TransportMessageEncoder

use of io.transport.sdk.protocol.codec.TransportMessageEncoder in project transporter by wang4ever.

the class TransportChannel method configure.

/**
 * 建立连接并初始化Nio channel
 *
 * @return this
 * @throws InterruptedException
 */
public TransportChannel configure() {
    try {
        if (this.bootstrap != null) {
            this.config.getLogger().warn("Initialized bootloader.");
            return this;
        }
        // 1.0 启动引导程序
        this.bootstrap = new Bootstrap();
        // 通过nio方式来接收连接和处理连接
        this.workerGroup = new NioEventLoopGroup();
        // 设置nio类型的channel
        this.bootstrap.group(workerGroup);
        this.bootstrap.channel(NioSocketChannel.class);
        this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        // this.bootstrap.option(ChannelOption.SO_TIMEOUT,
        // this.config.soTimeout());
        this.bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.config.getConnecTimeout() * 1000);
        // 有连接到达时会创建一个channel
        this.bootstrap.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
                ChannelPipeline p = ch.pipeline();
                if (config.isLoggingEnable() && config.getLevel().getValue() >= Level.DEBUG.getValue())
                    p.addLast(new LoggingHandler(LogLevel.valueOf(config.getLevel().name())));
                p.addLast(new IdleStateHandler(config.getReadIdleSeconds(), config.getWriteIdleSeconds(), config.getAllIdleSeconds()));
                p.addLast("decoder", new TransportMessageDecoder());
                p.addLast("encoder", new TransportMessageEncoder());
                p.addLast("receiveTextHandler", config.getHandler().newInstance());
            }
        });
    // 线程在这里开始等待,除非有socket事件唤醒
    // f.channel().closeFuture().sync();
    // 2.0 反射强制设置默认JDK日志级别
    // try {
    // // Netty JdkLogger instance
    // Field nettyJdkLogField =
    // Bootstrap.class.getDeclaredField("logger");
    // nettyJdkLogField.setAccessible(true);
    // Object nettyJdkLogObj = nettyJdkLogField.get(null);
    // // JdkLogger instance
    // Field jdkLogField =
    // nettyJdkLogObj.getClass().getDeclaredField("logger");
    // jdkLogField.setAccessible(true);
    // java.util.logging.Logger jdkLog = (java.util.logging.Logger)
    // jdkLogField.get(nettyJdkLogObj);
    // jdkLog.setLevel(Level.parse(this.config.getJdkLogLevel()));
    // } catch (Exception e) {
    // this.config.getLogger().error("设置Netty debugger日志级别失败. " +
    // e.getMessage());
    // }
    } catch (Exception e) {
        throw new TransportException("Connection channel configuration error.", e);
    }
    return this;
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) TransportMessageDecoder(io.transport.sdk.protocol.codec.TransportMessageDecoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) TransportMessageEncoder(io.transport.sdk.protocol.codec.TransportMessageEncoder) TransportException(io.transport.sdk.exception.TransportException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 2 with TransportMessageEncoder

use of io.transport.sdk.protocol.codec.TransportMessageEncoder in project transporter by wang4ever.

the class TransportConnector method configure.

/**
 * 建立连接并初始化Nio channel
 *
 * @return this
 * @throws InterruptedException
 */
public TransportConnector configure() {
    try {
        if (this.bootstrap != null) {
            this.config.getLogger().warn("Initialized bootloader.");
            return this;
        }
        // Bootstrap program.
        this.bootstrap = new Bootstrap();
        // Receiving connections and processing connections by Nio.
        this.workerGroup = new NioEventLoopGroup();
        this.bootstrap.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.config.getConnecTimeout() * 1000);
        // this.bootstrap.option(ChannelOption.SO_TIMEOUT,
        // this.config.soTimeout());
        // When a connection arrives, a channel will be created.
        this.bootstrap.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
                ChannelPipeline p = ch.pipeline();
                if (config.isLoggingEnable() && config.getLevel().getValue() >= Level.DEBUG.getValue())
                    p.addLast(new LoggingHandler(LogLevel.valueOf(config.getLevel().name())));
                p.addLast(new IdleStateHandler(config.getReadIdleSeconds(), config.getWriteIdleSeconds(), config.getAllIdleSeconds()));
                p.addLast("decoder", new TransportMessageDecoder(config));
                p.addLast("encoder", new TransportMessageEncoder(config));
                p.addLast("receiveTextHandler", config.getHandler().newInstance().setClient(client));
            }
        });
    // The thread begins to wait here unless there is a socket event
    // wake-up.
    // f.channel().closeFuture().sync();
    } catch (Exception e) {
        throw new TransportException("Connection channel configuration error.", e);
    }
    return this;
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) TransportMessageDecoder(io.transport.sdk.protocol.codec.TransportMessageDecoder) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap) TransportMessageEncoder(io.transport.sdk.protocol.codec.TransportMessageEncoder) TransportException(io.transport.sdk.exception.TransportException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)2 Channel (io.netty.channel.Channel)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)2 LoggingHandler (io.netty.handler.logging.LoggingHandler)2 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)2 TransportException (io.transport.sdk.exception.TransportException)2 TransportMessageDecoder (io.transport.sdk.protocol.codec.TransportMessageDecoder)2 TransportMessageEncoder (io.transport.sdk.protocol.codec.TransportMessageEncoder)2 IOException (java.io.IOException)2