Giter VIP home page Giter VIP logo

kryonetty's Introduction

KryoNetty

KryoNetty is a Java library that provides clean and simple API for efficient TCP client/server network communication using Netty. KryoNetty uses the Kryo serialization library to automatically and efficiently transfer object graphs across the network.

KryoNetty is very similar to KryoNet, which does the exact same thing but using its own NIO-based networking. KryoNet also has more features than KryoNetty.

Documentation

How to KryoNetty works

KryoNetty passes the configuration of the different components to the classes & references behind it.

    KryoNetty kryoNetty = new KryoNetty()
        .useLogging()
        .useExecution()
        .threadSize(16)
        .inputSize(4096)
        .outputSize(4096)
        .maxOutputSize(-1)
        .register(TestRequest.class);

Please note that Kryo gives every class an index-number. If you change the order, errors may occur if both programs do not have the same order of registered classes. To register more classes than one.

    KryoNetty kryoNetty = new KryoNetty()
        .useLogging()
        .useExecution()
        .threadSize(16)
        .inputSize(4096)
        .outputSize(4096)
        .maxOutputSize(-1)
        .register(
            TestRequest.class,
            TestResponse.class,
            SomeClass.class,
            Random.class
        )

Or with index-ids:

    KryoNetty kryoNetty = new KryoNetty()
        .useLogging()
        .useExecution()
        .threadSize(16)
        .inputSize(4096)
        .outputSize(4096)
        .maxOutputSize(-1)
        .register(100, TestRequest.class)
        .register(101, TestResponse.class)
        .register(102, SomeClass.class)
        .register(103, Random.class);

Every option returns the current instance of KryoNetty
Current Options:

  • useLogging()
    • enables usage of LoggingHandler.class
    • default: false
  • useExecution()
    • enables usage of EventExecutorGroup.class
    • default: false
  • threadSize()
    • sets threads of EventExecutorGroup.class
    • default: 0
  • inputSize()
    • sets buffer-size of Input.class
    • default: 2048
  • outputSize()
    • sets buffer-size of Output.class
    • default: 2048
  • maxOutputSize()
    • sets max-buffer-size of Output.class
    • default: -1

How to start the server

To start the Server, the following call start(int port) is all you need. The Server need a configured KryoNetty instance as argument.

    Server server = new Server(kryoNetty);
    server.start(56566);

How to connect the client

The Client configuration works quite similar to the Server. The only difference are the start(int port) and connect(String host, int port) method-calls.

    Client client = new Client(kryoNetty);
    client.connect("localhost", 56566);

How to register an Event

In contrast to KryoNet, the event system works a little differently. First of all there are the three following events:

  • ConnectEvent
    • server: new client connects
    • client: client connects to server
  • DisconnectEvent
    • server: client disconnects (server-side)
    • client: client disconnects (client-side)
  • ReceiveEvent
    • server: receives new object from client
    • client: receives new object from server

Here are two possibilities to register a Listener on an Event:

Class implementing Listener-Interface (recommended)

    public class ConnectionListener implements NetworkListener {
        @NetworkHandler
        public void onConnect(ConnectEvent connectEvent) {
            ChannelHandlerContext ctx = event.getCtx();
            System.out.println("Server: Client connected: " + ctx.channel().remoteAddress());
        }
    }

    // In main-method
    server.eventHandler().register(new ConnectionListener());

New Listener-Instance in register(NetworkListener listener):

    server.eventHandler().register(new NetworkListener() {
        @NetworkHandler
        public void onConnect(DisconnectEvent event) {
            ChannelHandlerContext ctx = event.getCtx();
            System.out.println("Server: Client disconnected: " + ctx.channel().remoteAddress());
        }
    });

Here an example to process an object which is fired via a ReceiveEvent.

    public class ReceiveListener implements NetworkListener {
        @NetworkHandler
        public void onReceive(ReceiveEvent event) {
            ChannelHandlerContext ctx = event.getCtx();
            Object object = event.getObject();
            System.out.println("Server: Client received: " + ctx.channel().remoteAddress() + "/" + object);
            if(object instanceof Boolean) {
                Boolean result = (Boolean) object;
                System.out.println("Result is: " + result);
            }
        }
    }

Add as dependency

First of all add jitpack.io as repository.

    repositories {
        maven { url 'https://jitpack.io' }
    }

After that you can add it as dependency.

    dependencies {
        implementation 'com.github.EsotericSoftware:kryonetty:master-SNAPSHOT'
    }

Build from source

If you want to build kryonetty from source, clone this repository and run ./gradlew shadowJar. The output-file will be in the directory: /build/libs/kryonetty-{version}-all.jar Gradle downloads the required dependencies and inserts all components into the output-file.

Why KryoSerialization

Why do we use Kryo and not for example the Netty ObjectEncoder? Quite simple. Kryo is noticeably faster and also easy to use. (Benchmark links:)

Since we work with a Kryo, Input & Output in a Pool<?> from kryo-5.3.0, classes are passed to the KryoSerialization constructor for registration & initialization. For more documentation see KryoSerialization.class

Please use the KryoNet discussion group for Kryo/KryoNet-specific support.

kryonetty's People

Contributors

koboo avatar nathansweet avatar philipwhiuk avatar seii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

kryonetty's Issues

Netty threads even running after closing.

When I try to stop the Kryonet clients it'll run even after I call the clientEndpoint.close() method and closing all channels in the ChannelHandlerContenxt.

User issue or is something wrong with the lib?

Java 17

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.