Transitioned to UDP client/server

This commit is contained in:
Collin Smith
2020-06-13 21:13:11 -07:00
parent eda10f1da9
commit dd54d11d9f
2 changed files with 44 additions and 42 deletions

View File

@ -5,13 +5,13 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import java.net.InetSocketAddress;
import com.badlogic.gdx.Application;
@ -40,11 +40,10 @@ public class Client extends ApplicationAdapter {
try {
Bootstrap b = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
protected void initChannel(DatagramChannel ch) {
ch.pipeline().addLast(new EchoClientHandler());
}
});
@ -58,10 +57,11 @@ public class Client extends ApplicationAdapter {
}
}
public static class EchoClientHandler extends ChannelInboundHandlerAdapter {
public static class EchoClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
private final ByteBuf buf;
public EchoClientHandler() {
EchoClientHandler() {
super(false);
buf = Unpooled.buffer(256);
for (int i = 0; i < buf.capacity(); i++) {
buf.writeByte((byte) i);
@ -76,18 +76,17 @@ public class Client extends ApplicationAdapter {
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
// Gdx.app.debug(TAG, msg.toString());
ctx.write(msg);
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
ctx.writeAndFlush(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
System.out.println("Read complete.");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Gdx.app.error(TAG, cause.getMessage(), cause);
ctx.close();
}

View File

@ -1,22 +1,23 @@
package com.riiablo.server.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.CharsetUtil;
import java.net.InetSocketAddress;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.lang3.StringEscapeUtils;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
@ -61,15 +62,15 @@ public class Main extends ApplicationAdapter {
Gdx.app.setLogLevel(Application.LOG_DEBUG);
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
// EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap()
.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childHandler(new ChannelInitializer<SocketChannel>() {
Bootstrap b = new Bootstrap()
.group(bossGroup)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
protected void initChannel(DatagramChannel ch) {
ch.pipeline().addLast(new EchoServerHandler());
}
})
@ -80,34 +81,36 @@ public class Main extends ApplicationAdapter {
} catch (Throwable t) {
Gdx.app.error(TAG, t.getMessage(), t);
} finally {
workerGroup.shutdownGracefully();
// workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
Gdx.app.log(TAG, "Connection from " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
public static class EchoServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
EchoServerHandler() {
super(false);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// Gdx.app.debug(TAG, msg.toString());
// ctx.write(msg);
ByteBuf buf = (ByteBuf) msg;
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println("Channel active.");
}
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
Gdx.app.log(TAG, "Packet from " + msg.sender().getHostName() + ":" + msg.sender().getPort());
ByteBuf in = msg.content();
try {
System.out.print(buf.toString(CharsetUtil.US_ASCII));
System.out.print(StringEscapeUtils.escapeJava(in.toString(CharsetUtil.US_ASCII)));
} finally {
buf.release();
in.release();
}
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
// ctx.flush();
System.out.flush();
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println();
System.out.println("Read complete.");
}
@Override