Search in sources :

Example 16 with TransportException

use of io.transport.sdk.exception.TransportException 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)

Example 17 with TransportException

use of io.transport.sdk.exception.TransportException in project transporter by wang4ever.

the class TransportConnector method close.

@Override
public void close() throws IOException {
    try {
        if (this.workerGroup != null)
            this.workerGroup.shutdownGracefully();
        if (this.channel != null)
            this.channel.close();
    } catch (Exception e) {
        throw new TransportException(e);
    } finally {
        this.channel = null;
        this.workerGroup = null;
        this.bootstrap = null;
        System.gc();
    }
}
Also used : TransportException(io.transport.sdk.exception.TransportException) IOException(java.io.IOException) TransportException(io.transport.sdk.exception.TransportException)

Example 18 with TransportException

use of io.transport.sdk.exception.TransportException in project transporter by wang4ever.

the class DeviceRegistMessage method writeBodyBufEncoder.

@Override
public void writeBodyBufEncoder(ByteBuf out) {
    byte[] appIdBuf = ByteBufs.coverFixedBuf(this.getAppId(), ConnectMessage.ID_LEN);
    StringBuffer deviceIds = new StringBuffer();
    for (String id : getClientDeviceIds()) {
        if (ByteBufs.toBytes(String.valueOf(id)).length != DeviceInfo.ID_LEN)
            throw new TransportException("Protocol error, parameter 'deviceId' length can only be " + DeviceInfo.ID_LEN);
        deviceIds.append(id);
        deviceIds.append(",");
    }
    if (deviceIds.toString().endsWith(","))
        deviceIds.delete(deviceIds.length() - 1, deviceIds.length());
    byte[] deviceIdsBuf = ByteBufs.toBytes(deviceIds.toString());
    int totalLen = Head.HEAD_LEN + ConnectMessage.ID_LEN + 4 + deviceIdsBuf.length;
    out.writeInt(totalLen);
    out.writeInt(this.getHead().getVersion());
    out.writeInt(this.getHead().getActionId());
    out.writeInt(this.getHead().getSequenceId());
    out.writeInt(this.getHead().getReserve());
    out.writeBytes(appIdBuf);
    out.writeInt(this.getExpired());
    out.writeBytes(deviceIdsBuf);
}
Also used : TransportException(io.transport.sdk.exception.TransportException)

Aggregations

TransportException (io.transport.sdk.exception.TransportException)18 IOException (java.io.IOException)6 TransportMessage (io.transport.sdk.protocol.message.internal.TransportMessage)4 HostAndPort (io.transport.sdk.Configuration.HostAndPort)3 Bootstrap (io.netty.bootstrap.Bootstrap)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)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 TransportMessageDecoder (io.transport.sdk.protocol.codec.TransportMessageDecoder)2 TransportMessageEncoder (io.transport.sdk.protocol.codec.TransportMessageEncoder)2 DeviceRegistMessage (io.transport.sdk.protocol.message.internal.DeviceRegistMessage)2 TransportAuthenticationException (io.transport.sdk.exception.TransportAuthenticationException)1 TransportInitializeException (io.transport.sdk.exception.TransportInitializeException)1 ArrayList (java.util.ArrayList)1