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;
}
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();
}
}
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);
}
Aggregations