Skip to content

OzaoNet is a Java Networking Library based on Netty which provides multiple ways of sending and receiving packets, it is mainly inspired by SimpleNet and Kryonet

Notifications You must be signed in to change notification settings

adamaq01/ozao-net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OzaoNet (Java Networking Library)

OzaoNet is a Java Networking Library based on Netty which provides multiple ways of sending and receiving packets, it is mainly inspired by SimpleNet and Kryonet.

Add the dependency to your project

Release

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {
    compile 'com.github.Adamaq01:ozao-net:2.4.0'
}

Example

Here is a simple example of a UDP server and client that should produce this output.

[Server] Mike has arrived, he carries this message: Do you want some chocolate ?
[Client] Pike has arrived, he carries this message: Yes please, I love chocolate !
[Client] Received disconnect message 42

The two first packets that carries Bird objects will be compressed with Zstandard basic's functions (no dictionnary) and the last one will not be compressed.

Bird Class (which implements NetSerializable):
public class Bird implements NetSerializable<Bird> {

    private String name;
    private String message;

    public Bird(String name, String message) {
        this.name = name;
        this.message = message;
    }

    public String getName() {
        return name;
    }

    public String getMessage() {
        return message;
    }

    @Override
    public Buffer write(Buffer packet) {
        return packet.putString(name).putString(message);
    }

    @Override
    public Bird read(Buffer packet) {
        this.name = packet.getString();
        this.message = packet.getString();

        return this;
    }
}
UDP Server:
// Instantiate a new UDP server, set timeout delay, add a basic handler then bind the server
Server server = new UDPServer(new OzaoServerProtocol()).setTimeout(15).addHandler(new ServerHandlerAdapater() {

    @Override
    public void onPacketReceive(Server server, Connection connection, Packet packet) {
        // Read the packet payload into a Bird (See OzaoProtocol for keys specifications)
        Bird bird = packet.<Buffer>get("payload").to(Bird.class);
        System.out.println(String.format("[Server] %s has arrived, he carries this message: %s", bird.getName(), bird.getMessage()));
        // Respond with a compressed packet containing a Bird and its informations
        connection.sendPacket(Packet.create().put("compression", true).put("payload", Buffer.from(new Bird("Pike", "Yes please, I love chocolate !"))));
        // Send a new packet (not compressed) which contains a simple byte to tell the client to disconnect
        connection.sendPacket(Packet.create().put("compression", false).put("payload", Buffer.create().putByte((byte) 42)));
        // Close the server
        server.close();
    }
}).bind(2812);
UDP Client:
// Instantiate a new UDP client, add a basic handler then connect to the server
Client client = new UDPClient(new OzaoClientProtocol()).addHandler(new ClientHandlerAdapter() {

    @Override
    public void onConnect(Client client) {
        // Send a compressed packet which contains a Bird and its informations when the client gets connected to the server
        Bird bird = new Bird("Mike", "Do you want some chocolate ?");
        client.sendPacket(Packet.create().put("compression", true).put("payload", Buffer.from(bird)));
    }

    @Override
    public void onPacketReceive(Client client, Packet packet) {
        // Create a new variable that contains the packet's payload
        Buffer payload = packet.get("payload");
        if (payload.readableBytes() == 1) { // If there is only one byte then it's the packet that tells us to disconnect
            System.out.println("[Client] Received disconnect message " + payload.getByte());
            // Disconnect from the server and stop the client
            client.disconnect();
        } else {
            // Read the packet payload into a Bird
            Bird bird = payload.to(Bird.class);
            System.out.println(String.format("[Client] %s has arrived, he carries this message: %s", bird.getName(), bird.getMessage()));
        }
    }
}).connect("localhost", 2812);

Credits:

- luben's zstd-jni

About

OzaoNet is a Java Networking Library based on Netty which provides multiple ways of sending and receiving packets, it is mainly inspired by SimpleNet and Kryonet

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages