use of io.netty.handler.codec.LengthFieldPrepender in project jMiniLang by bajdcc.
the class ModuleNetClient method run.
public void run() {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
if (addr.startsWith(":")) {
addr = "127.0.0.1" + addr;
}
URI uri = new URI("http://" + addr);
if (uri.getPort() < 0)
throw new URISyntaxException(addr, "invalid address");
Bootstrap b = new Bootstrap();
b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetClientHandler(msgQueue));
}
});
ChannelFuture f = b.connect(uri.getHost(), uri.getPort());
CHANNEL_GROUP.add(f.channel());
f = f.sync();
if (f.isDone()) {
status = Status.RUNNING;
}
this.addr = f.channel().localAddress().toString();
f.channel().closeFuture().sync();
status = Status.ERROR;
error = "Client closed.";
} catch (Exception e) {
status = Status.ERROR;
error = "Error: " + e.getMessage();
if (error == null)
error = e.getClass().getSimpleName();
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
}
}
use of io.netty.handler.codec.LengthFieldPrepender in project jMiniLang by bajdcc.
the class ModuleNetServer method run.
public void run() {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)).addLast("encoder", new LengthFieldPrepender(4, false)).addLast(new StringDecoder(Charset.forName("utf-8"))).addLast(new StringEncoder(Charset.forName("utf-8"))).addLast(new ModuleNetServerHandler(msgQueue));
}
}).option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port);
CHANNEL_GROUP.add(f.channel());
f = f.sync();
if (f.isDone()) {
status = Status.RUNNING;
}
f.channel().closeFuture().sync();
status = Status.ERROR;
error = "Server closed.";
} catch (Exception e) {
status = Status.ERROR;
error = "Error: " + e.getMessage();
if (error == null)
error = e.getClass().getSimpleName();
e.printStackTrace();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
use of io.netty.handler.codec.LengthFieldPrepender in project JaPS by JackWhite20.
the class ServerChannelInitializer method initChannel.
@Override
protected void initChannel(Channel channel) throws Exception {
try {
channel.config().setOption(ChannelOption.IP_TOS, 0x18);
} catch (ChannelException e) {
// Not supported
}
channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
channel.pipeline().addLast(new JSONObjectDecoder());
channel.pipeline().addLast(new LengthFieldPrepender(4));
channel.pipeline().addLast(new JSONObjectEncoder());
channel.pipeline().addLast(new Connection(jaPSServer, channel));
}
use of io.netty.handler.codec.LengthFieldPrepender in project JaPS by JackWhite20.
the class ClientChannelInitializer method initChannel.
@Override
protected void initChannel(Channel channel) throws Exception {
try {
channel.config().setOption(ChannelOption.IP_TOS, 0x18);
} catch (ChannelException e) {
// Not supported
}
channel.config().setAllocator(PooledByteBufAllocator.DEFAULT);
channel.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4));
channel.pipeline().addLast(new JSONObjectDecoder());
channel.pipeline().addLast(new LengthFieldPrepender(4));
channel.pipeline().addLast(new JSONObjectEncoder());
channel.pipeline().addLast(nioSocketClient);
}
use of io.netty.handler.codec.LengthFieldPrepender in project netty by netty.
the class LengthFieldPrependerTest method testAdjustedLengthLessThanZero.
@Test
public void testAdjustedLengthLessThanZero() throws Exception {
final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, -2));
try {
ch.writeOutbound(msg);
fail(EncoderException.class.getSimpleName() + " must be raised.");
} catch (EncoderException e) {
// Expected
}
}
Aggregations