/**
* {@inheritedDoc}
*/
public void start(final int port, final CountDownLatch counter, final byte[] data) throws IOException {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group);
bootstrap.option(ChannelOption.SO_SNDBUF, 64 * 1024);
bootstrap.option(ChannelOption.TCP_NODELAY, true);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
private void sendMessage(ChannelHandlerContext ctx, byte[] data) {
ByteBuf buf = ctx.alloc().buffer(data.length);
buf.writeBytes(data);
ctx.writeAndFlush(buf);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
ByteBuf buf = (ByteBuf)message;
for(int i=0; i < buf.readableBytes();i++) {
counter.countDown();
if (counter.getCount() > 0) {
sendMessage(ctx, data);
} else {
ctx.channel().close();
}
}
buf.release();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
sendMessage(ctx, data);
}
});
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
});
bootstrap.connect(new InetSocketAddress(port));
}