Echo server with separate channel handler operationnal.

This commit is contained in:
ProfMobius committed 2014-10-01 20:36:18 +02:00
1 parent e96ae1bc4a
commit 6eb3e5880c
2 files changed
+52 -30

No files matched your search

@@ -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();
}
}
@@ -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<SocketChannel>() { // (4)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@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();