This is a server/client communication system using Netty. It communicates with the use of packets.
This project was made for self learning purposes.
- Java (Tested in Java 11)
- Maven
Building this project is pretty easy, dependencies will automatically be downloaded using maven:
- Clone this repository with
git clone https://github.com/ImSpooks/Netty-Core.git
- Browse to the cloned repository.
- Run the command
mvn package
in terminal or build with maven using your IDE. The jar files will be build in theout/
folder.
To run the server, go to the out
folder and type java -jar Server-1.0.jar
in the terminal.
To run the client, go to the out
folder and type java -jar Client-1.0.jar
in the terminal.
To use/edit this project all you need is a Java IDE such as IntelliJ with Maven. With that IDE, you can open this project's pom and Maven will do the rest.
Creating packet types is fairly easy, this is all you have to do:
class PacketRegisterer {
public static void main(String[] args) {
// Register packet type
PacketType.registerPacketType("Example");
// Get packet type
PacketType.getPacketType("Example");
}
}
To create packets:
- Create a class that extends the
Packet
class - Create variable fields and a constructor with these parameters and an empty constructor.
- Write all your code in the
send
andreceive
method.
Example:
public class PacketExample extends Packet {
private long verificationId;
private long time;
// empty constructor
public PacketExample() {}
public PacketExample(long verificationId) {
this.verificationId = verificationId;
this.time = System.currentTimeMillis();
}
@Override
public void send(WrappedOutputStream out) throws IOException {
out.writeLong(this.verificationId);
out.writeLong(this.time);
}
@Override
public void receive(WrappedInputStream in) throws IOException {
this.verificationId = in.readLong();
this.time = in.readLong();
}
public long getVerificationId() {
return verificationId;
}
public long getTime() {
return time;
}
}
To register the packet in the system:
// Registering packet
class PacketRegisterer {
public static void main(String[] args) {
// Register packet
PacketRegister.register(PacketType.getPacketType("Example"), PacketExample.class);
// Sending packet from client
core_instance.sendPacket(new PacketExample(vertificationId));
// Sending packet from server
client.sendPacket(new PacketExample(vertificationId));
}
}
To handle packets received by the client you must have a class that extends to a sub packet handler:
- Create a class that extends to SubPacketHandler
- Every packet handling method must have the
@PacketHandling
annotation. - Every packet handling method must have
ChannelHandlerContext
as their first parameter type, and the second parameter as the packet
Example:
public class ExamplePacketHandler extends SubPacketHandler {
public NetworkPacketHandler(PacketHandler packetHandler, ServerSettings settings) {
super(packetHandler, settings);
}
@PacketHandling
public void handlePacket(ChannelHandlerContext ctx, PacketExample packet) {
System.out.println("Verification ID: " + packet.getVerificationId() + ", latency: " + (System.currentTimeMillis() - packet.getTime()) + " ms");
ctx.writeAndFlush(new PacketExample(packet.getVerificationId()));
}
}
To handle packets received by the client, you must first send them:
public class Launcher {
public static void main(String[] args) {
CoreClient client = CoreClient.startClient();
long now = System.currentTimeMillis();
long verificationId = new Random().nextLong();
client.addIncomingListener(PacketExample.class, new IncomingListener<PacketExample>(10) {
@Override
protected boolean onReceive(PacketExample packet) {
// Packet received
if (packet.getVerificationId() == verificationId) {
Logger.info("Took {} ms to send and receive the packet", packet.Time() - now);
return true;
}
return false;
}
@Override
protected void onExpire() {
// Packet not received in given time
Logger.info("Packet wasn't received after 10 seconds.");
}
});
client.sendPacket(new PacketExample(verificationId));
}
}
Example to register the packet handler:
class PacketRegisterer {
public static void main(String[] args) {
// Register packet handler
PacketHandler.addPacketHandler(PacketType.getPacketType("Example"), ExamplePacketHandler.class);
}
}
To launch the client/server you need to do these few things:
class Launcher {
public static void main(String[] args) {
/* Registering custom packets */
PacketType.registerPacketType("Example");
PacketRegister.register(PacketType.getPacketType("Example"), PacketExample.class);
/* Register packet handler in server */
PacketHandler.addPacketHandler(PacketType.getPacketType("Example"), ExamplePacketHandler.class);
/* Launching */
// Launch client with
CoreClient.startClient();
// Or launch the server with
Core.startServer();
/* Send/receive packets with the client */
long now = System.currentTimeMillis();
long verificationId = new Random().nextLong();
client.addIncomingListener(PacketExample.class, new IncomingListener<PacketExample>(10) {
@Override
protected boolean onReceive(PacketExample packet) {
// Packet received
if (packet.getVerificationId() == verificationId) {
Logger.info("Took {} ms to send and receive the packet", packet.Time() - now);
return true;
}
return false;
}
@Override
protected void onExpire() {
// Packet not received in given time
Logger.info("Packet wasn't received after 10 seconds.");
}
});
client.sendPacket(new PacketExample(verificationId));
}
}