From 6eb3e5880c174e8e54fbabe8dea3c58231057443 Mon Sep 17 00:00:00 2001 From: ProfMobius Date: Wed, 1 Oct 2014 20:36:18 +0200 Subject: [PATCH] Echo server with separate channel handler operationnal. --- .../rconserver/RConInboundHandler.java | 43 +++++++++++++++++++ .../opis/network/rconserver/RConServer.java | 39 ++++------------- 2 files changed, 52 insertions(+), 30 deletions(-) create mode 100644 src/main/java/mcp/mobius/opis/network/rconserver/RConInboundHandler.java diff --git a/src/main/java/mcp/mobius/opis/network/rconserver/RConInboundHandler.java b/src/main/java/mcp/mobius/opis/network/rconserver/RConInboundHandler.java new file mode 100644 index 0000000..49748cc --- /dev/null +++ b/src/main/java/mcp/mobius/opis/network/rconserver/RConInboundHandler.java @@ -0,0 +1,43 @@ +package mcp.mobius.opis.network.rconserver; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.util.ReferenceCountUtil; + +class RConInboundHandler extends ChannelInboundHandlerAdapter{ + // One object of this type is created by connection + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + // This method is called when the connection is created. + ctx.fireChannelActive(); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + // This method is called when new data is readable in the stream. + + ctx.write(msg); + ctx.flush(); + + /* + ByteBuf in = (ByteBuf) msg; + try { + while (in.isReadable()) { // (1) + System.out.print((char) in.readByte()); + System.out.flush(); + } + } finally { + ReferenceCountUtil.release(msg); + } + */ + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } +} + diff --git a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java index 239e539..406c716 100644 --- a/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java +++ b/src/main/java/mcp/mobius/opis/network/rconserver/RConServer.java @@ -14,59 +14,38 @@ import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.util.ReferenceCountUtil; -public class RConServer extends ChannelInboundHandlerAdapter implements Runnable { +public class RConServer implements Runnable { public final static RConServer instance = new RConServer(); private int port = -1; - private RConServer(){ this.port = modOpis.rconport; } - @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - //((ByteBuf) msg).release(); - - try { - // Do something with msg - } finally { - ReferenceCountUtil.release(msg); - } - } - - @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { - cause.printStackTrace(); - ctx.close(); - } - @Override public void run(){ modOpis.log.info("Starting Opis remote control server."); - EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1) + EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { - ServerBootstrap b = new ServerBootstrap(); // (2) + ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) - .channel(NioServerSocketChannel.class) // (3) - .childHandler(new ChannelInitializer() { // (4) + .channel(NioServerSocketChannel.class) + .childHandler(new ChannelInitializer() { @Override public void initChannel(SocketChannel ch) throws Exception { - ch.pipeline().addLast(RConServer.instance); + ch.pipeline().addLast(new RConInboundHandler()); } }) - .option(ChannelOption.SO_BACKLOG, 128) // (5) - .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) + .option(ChannelOption.SO_BACKLOG, 128) + .childOption(ChannelOption.SO_KEEPALIVE, true); - // Bind and start to accept incoming connections. ChannelFuture f = b.bind(port).sync(); // (7) - // Wait until the server socket is closed. - // In this example, this does not happen, but you can do that to gracefully - // shut down your server. f.channel().closeFuture().sync(); + } catch (Exception e){ modOpis.log.severe("Error in Opis remote control server."); e.printStackTrace();