Development Log - 2023-11-26
Author: b0in
The second and third modules!
The second module is now done. The first module, crow.game.codec.netty, is the FrameCodec and PacketCodec implementations
which allow a netty client and server listen for messages. The second module,
crow.game.examples.pingpong is an example of a TCP client and server using
the two core modules implemented now.
Module Statuses
From the previous post ./2023-11-24.html, but just the modules which have changed or added.
crow.game.codec.api - [95%]
We tweaked this one to use ‘short’ type for packet ID everywhere, though I'm worried this is going to be a pain due to signed/unsigned issues. My sample packets were 0x99NN which rolled over. This might not be a problem in the future but, we will see.
crow.game.codec.netty - [90%]
Working! Not much to say as this package is incredibly simple. I would like to move all ByteBuf references in codec.api into this package, and either use a shared interface in codec.api OR use nio ByteBuffer. Not sure which way I will go yet.
crow.game.examples.pingpong - [90%]
An example application using Netty, slf4j, and not much else. The server merely waits for Ping packets and responds with Pongs and the client just connects, sends Ping, then disconnects when it receives the Pong.
Here is the results of running this example:
[main] INFO utils.Main - running...
[client-worker-0] INFO client.PingPongClientInitializer - initializing channel
[server-worker-0] INFO server.PingPongServerInitializer - GOT PING, RESPONDING
[client-worker-0] INFO client.PingPongClientInitializer - GOT PONG
[client-worker-0] INFO client.PingPongClientInitializer - channel done
[main] INFO client.MainClient - client finished
Process finished with exit code 0
and to show how you would put it all together, here is the initChannel implementation for the server (without the context and exception handling):
class PingPongServerInitializer {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline() //
.addLast(new FrameCodec(context)) //
.addLast(new PacketCodec(context)) //
.addLast(new SimpleChannelInboundHandler<Ping>() {
@Override
void channelRead0(ChannelHandlerContext ctx, Ping ping) {
ctx.writeAndFlush(new Pong());
}
});
}
}
