What is Thunder?
Thunder is an open-source and easy to use Java Networking Framework for Java 1.8 and higher Thunder uses TCP for guranteed data transfer or TLS if you want to use encryption. Thunder is thread safe and supports Compression of Packets.
Content:
The ThunderServer and the ThunderClient are based on the ThunderConnection The ThunderConnection has its own unique Session to identify it. You can get it by using ThunderConnection#getSession()
You can create a ThunderServer as the following. The Server is used to handle all Connections (ThunderClients).
ThunderServer thunderServer = Thunder.createServer();
thunderServer.start(1401).perform();
The ThunderClient connects to the ThunderServer and transfers data from one connection to another.
ThunderClient thunderClient = Thunder.createClient();
thunderClient.connect("127.0.0.1", 1401).perform();
The ThunderListener implements three methods to listen for events. The Listener can be add to a ThunderConnection (so either ThunderClient or ThunderServer)
ThunderServer thunderServer = Thunder.createServer(new ThunderListener() {
@Override
public void handlePacket(Packet packet, ThunderConnection thunderConnection) throws IOException {
System.out.println(packet.toString());
}
@Override
public void handleConnect(ThunderConnection thunderConnection) {
}
@Override
public void handleDisconnect(ThunderConnection thunderConnection) {
}
});
Packets are the objects that contain the data which is getting transferred between ThunderConnections. Packet.class is abstract and contains two default methods. Packet#write(PacketBuffer) and Packet#read(PacketBuffer).
Every Packet has its own UUID, a protocolVersion, a protocolId, and other values. An example Packet could look like the following:
@Getter @AllArgsConstructor
public class SamplePacket extends Packet {
private String name;
private int age;
@Override
public void write(PacketBuffer buf) {
buf.writeString(name);
buf.writeInt(age);
}
@Override
public void read(PacketBuffer buf) {
name = buf.readString();
age = buf.readInt();
}
}
The attributes inside of a Packet must be read and written from or into the PacketBuffer. A Packet can be send through a ThunderConnection.
SamplePacket samplePacket = new SamplePacket("Name", 32);
thunderConnection.sendPackrt(samplePacket);
When receiving Packets from a different ThunderConnection you can handle the received Packet with the integrated PacketAdapter and register a PacketHandler to handle the Packet.
thunderConnection.addPacketHandler(new PacketHandler() {
@Override
public void handle(ThunderPacket packet) {
//Check if packet is SamplePacket
if (packet instanceof SamplePacket) {
SamplePacket samplePacket = (SamplePacket) packet;
System.out.println(samplePacket.getAge());
System.out.println(samplePacket.getName());
System.out.println(samplePacket.getProcessingTime() + "ms"); //The time the packet took
System.out.println(samplePacket.toString()); //Information on the Packet
}
}
});
The PacketSystem takes advantage of a Response-based System. You can await for a Response when sending a Packet. You can respond to a Packet with a ResponseStatus and/or a Message.
An example could look like this:
(Waiting for the Response with Consumer)
thunderConnection.sendPacket(new SamplePacket("Luca", 16), new Consumer<Response>() {
@Override
public void accept(Response response) {
System.out.println(response.getStatus() + " - " + response.getMessage() + " [" + response.getProcessingTime() + "ms]");
}
});
(Waiting for the Response without Consumer)
SamplePacket samplePacket = new SamplePacket("Name", 16);
Response response = thunderConnection.transferToResponse(samplePacket);
System.out.println(response.getStatus() + " - " + response.getMessage());
(Responding to the Packet)
thunderConnection.addPacketHandler(new PacketHandler() {
@Override
public void handle(ThunderPacket packet) {
//Check if packet is SamplePacket
if (packet instanceof SamplePacket) {
packet.respond(ResponseStatus.SUCCESS, "Test Message");
}
}
});