Giter Site home page Giter Site logo

crykn / kryonet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from esotericsoftware/kryonet

66.0 7.0 8.0 2.51 MB

A fork of the KryoNet client/server library for Java.

License: BSD 3-Clause "New" or "Revised" License

Java 100.00%
kryonet java networking

kryonet's Introduction

KryoNet

Release Build

A fork of KryoNet, a Java library that provides a clean and simple API for efficient network communication.

This fork was specifically made for ProjektGG, but also adds the most demanded features on KryoNet's issue tracker. If you have a pull request for KryoNet also consider adding it here, as KryoNet had its last release in 2013.

Key Changes

  • Kryo 5 is used for the serialization (for a list of changes and new features since Kryo 3 see here; takes care of #77 and #123)
  • Listener is now an interface (#39) with empty default methods, which helps to reduce boilerplate code
  • Includes a fix for the common Android 5.0 crash (#106, #111, #120), toggleable by UdpConnection#androidFixDisabled
  • The LAN Host Discovery was improved and is now available to Non-Kryo-Serializations (#127)
  • A TypeListener was added for easier message handling (takes care of #130)
  • Various improvements to the javadoc (especially #35, #44, #124, #137); helps to reduce the most common developer mistakes
  • KryoNet now uses a gradle build setup
  • Java 8+ is supported
  • All the commits made on the main repo since the last release in 2013 (#52, #98, #105) and a lot more minor fixes and changes (#109 amongst others); the documentation (see below) was updated as well

A more in-depth changelog is available on the releases page.

Documentation

KryoNet is ideal for any client/server application. It is very efficient, so is especially good for games. KryoNet can also be useful for inter-process communication.

Click to expand the whole documentation

Running a server

This code starts a server on TCP port 54555 and UDP port 54777:

    Server server = new Server();
    server.bind(54555, 54777);
    server.start();

The start() method starts a thread to handle incoming connections, reading/writing to the socket and notifying listeners.

The following code adds a listener to handle receiving objects. Listeners should be added before binding and starting the server.

    server.addListener(new Listener() {
       public void received (Connection connection, Object object) {
          if (object instanceof SomeRequest) {
             SomeRequest request = (SomeRequest)object;
             System.out.println(request.text);
    
             SomeResponse response = new SomeResponse();
             response.text = "Thanks";
             connection.sendTCP(response);
          }
       }
    });
Click to expand the class definitions of 'SomeRequest' and 'SomeResponse'
    public class SomeRequest {
       public String text;
    }
    public class SomeResponse {
       public String text;
    }

Typically a listener has a series of instanceof checks to decide what to do with the object received. In this example, it prints out a string and sends a response over TCP.

Note the Listener class also has connected(Connection) and disconnected(Connection) methods that can be overridden.


TypeListeners

Type listeners takes care of distributing received messages to previously specified handlers. This replaces the long instanceof checks and allows for rather concise code, especially with lambdas:

TypeListener typeListener = new TypeListener();

// add a type handler for SomeRequest.class   
typeListener.addTypeHandler(SomeRequest.class,
   (con, msg) -> {
      System.out.println(msg.getSomeData());
   });
// add another one for SomeOtherRequest.class
typeListener.addTypeHandler(SomeOtherRequest.class,
   (con, msg) -> {
      con.sendTCP(new SomeResponse());
});

server.addListener(typeListener);

In the above example con is the connection to the client and msg is the received object - already cast to the right type.


Connecting a client

This code connects to a server running on TCP port 54555 and UDP port 54777:

    Client client = new Client();
    client.start();
    client.connect(5000, "192.168.0.4", 54555, 54777);
    
    SomeRequest request = new SomeRequest();
    request.text = "Here is the request";
    client.sendTCP(request);

The start() method starts a thread to handle the outgoing connection, reading/writing to the socket, and notifying listeners. Note that this method must be called before connect(...), otherwise the outgoing connection will fail.

In the above example, the connect(...) method blocks for a maximum of 5000 milliseconds. If it times-out or the connecting otherwise fails, an exception is thrown (handling not shown in the example above). After the connection is made, the example sends a SomeRequest object to the server over TCP.

This code adds a listener to print out the response:

    client.addListener(new Listener() {
       public void received (Connection connection, Object object) {
          if (object instanceof SomeResponse) {
             SomeResponse response = (SomeResponse)object;
             System.out.println(response.text);
          }
       }
    });

Registering classes

For the above examples to work, the classes that are going to be sent over the network must be registered with Kryo. Kryo takes care of serializing the objects to and from bytes.

    Kryo kryo = server.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);
    Kryo kryo = client.getKryo();
    kryo.register(SomeRequest.class);
    kryo.register(SomeResponse.class);

This must be done on both the client and server, before any network communication occurs. It is very important that the exact same classes are registered on both the client and server and that they are registered in the exact same order. Because of this, typically the code that registers classes is placed in a method on a class available to both the client and server.

Alternatively, Kryo can be configured to allow serialization without registering classes up front (kryo.setRegistrationRequired(false)). While this is useful for testing purposes, it can pose a security risk and leads to larger packet sizes. In addition, kryo.setWarnUnregisteredClasses(true) can be used to log whenever an unregistered class is serialized.

For further information on how objects are serialized for network transfer, please take a look at the Kryo serialization library. Kryo can serialize any object and supports data compression (e.g. deflate compression).


TCP and UDP

KryoNet always uses a TCP port. This allows the framework to easily perform reliable communication and have a stateful connection. KryoNet can optionally use a UDP port in addition to the TCP one. While both ports can be used simultaneously, it is not recommended to send an huge amount of data on both at the same time because the two protocols can affect each other.

TCP is reliable, meaning objects sent are sure to arrive at their destination eventually. UDP is faster, but unreliable, meaning an object sent may never be delivered. Because it is faster, UDP is typically used, when many updates are being sent and it does not matter if one update is missed.

Note that KryoNet does not currently implement any extra features for UDP, such as reliability or flow control. It is left to the application to make proper use of the UDP connection. See here for an example of a delta-snapshot-protocol.


Buffer sizes

KryoNet uses a few buffers for serialization and deserialization that must be sized appropriately for a specific application. See the Client and Server constructors for customizing the buffer sizes. There are two types of buffers, a write buffer and an object buffer.

To receive an object graph, the bytes are stored in the object buffer until all of the bytes for the object are received, then the object is deserialized. The object buffer should be sized at least as large as the largest object that will be received.

To send an object graph, it is serialized to the write buffer where it is queued until it can be written to the network socket. Typically it is written immediately, but when sending a lot of data or when the network is slow, it may remain queued in the write buffer for a short time. The write buffer should be sized at least as large as the largest object that will be sent, plus some head room to allow for some serialized objects to be queued. The amount of head room needed is dependent upon the size of objects being sent and how often they are sent.

To avoid very large buffer sizes, object graphs can be split into smaller pieces and sent separately. Collecting the pieces and reassembling the larger object graph, or writing them to disk, etc is left to the application code. If a large number of small object graphs are queued to be written at once, it may exceed the write buffer size. TcpIdleSender and InputStreamSender can be used to queue more data only when the connection is idle. Also see the setIdleThreshold method on the Connection class.


Threading

KryoNet imposes no restrictions on how threading is handled. The Server and Client classes have an update() method that accepts connections and reads or writes any pending data for the current connections. The update method should be called periodically to process network events. Both the Client and Server classes implement Runnable and the run() method continually calls update until the stop() method is called.

Handing a client or server to a java.lang.Thread is a convenient way to have a dedicated update thread, and this is what the start method does. If this does not fit your needs, call update() manually from the thread of your choice.

Listeners are notified from the update thread, so should not block for long. To change this behavior, take a look at ThreadedListener and QueuedListener.

The update thread should never be blocked to wait for an incoming network message, as this will cause a deadlock.


LAN server discovery

KryoNet can broadcast a UDP message on the LAN to discover any servers running:

    InetAddress address = client.discoverHost(54777, 5000);
    System.out.println(address);

This will print the address of the first server found running on UDP port 54777. The call will block for up to 5000 milliseconds, waiting for a response. A more in-depth example (where additional data is sent) can be found in the DiscoverHostTest.


Logging

KryoNet makes use of the low overhead, lightweight MinLog logging library. The logging level can be set in this way:

    Log.set(LEVEL_TRACE);

KryoNet does minimal logging at INFO and above levels. DEBUG is good to use during development and indicates the total number of bytes for each object sent. TRACE is good to use when debugging a specific problem, but outputs too much information to leave on all the time.

MinLog supports a fixed logging level, which will remove logging statements below that level. For efficiency, KryoNet can be compiled with a fixed logging level MinLog JAR. See here for more information.


Pluggable Serialization

Serialization can be customized by providing a Serialization instance to the Client and Server constructors. By default KryoNet uses Kryo (hence the name of KryoNet) for serialization. Kryo uses a binary format and is very efficient, highly configurable and does automatic serialization for most object graphs.

Additionally, JSON serialization is provided which uses JsonBeans. JSON is human readable so is convenient for use during development to monitor the data being sent and received.


Remote Method Invocation

KryoNet has an easy to use mechanism for invoking methods on remote objects (RMI). This has a small amount of overhead versus explicitly sending objects. RMI can hide that methods are being marshaled and executed remotely, but in practice the code using such methods will need to be aware of the network communication to handle errors and methods that block. KryoNet's RMI is not related to the java.rmi package.

RMI is done by first calling registerClasses, creating an ObjectSpace and registering objects with an ID on one side of the connection:

    ObjectSpace.registerClasses(endPoint.getKryo());
    ObjectSpace objectSpace = new ObjectSpace();
    objectSpace.register(42, someObject);
    // ...
    objectSpace.addConnection(connection);

Multiple ObjectSpaces can be created for both the client or server side. Once registered, objects can be used on the other side of the registered connections:

    SomeObject someObject = ObjectSpace.getRemoteObject(connection, 42, SomeObject.class);
    SomeResult result = someObject.doSomething();

The getRemoteObject(...) method returns a proxy object that represents the specified class. When a method on the class is called, a message is sent over the connection and on the remote side the method is invoked on the registered object. The method blocks until the return value is sent back over the connection.

Exactly how the remote method invocation is performed can be customized by casting the proxy object to a RemoteObject.

    SomeObject someObject = ObjectSpace.getRemoteObject(connection, 42, SomeObject.class);
    ((RemoteObject)someObject).setNonBlocking(true);
    someObject.doSomething();

Note that the SomeObject class does not need to implement RemoteObject, this is handled automatically.

The method setNonBlocking(true) causes remote method invocations to be non-blocking, i.e. when doSomething is invoked, it will not block and wait for the return value. Instead the method will just return null. The return value or any thrown exception for a non-blocking method invocation can still be retrieved if they are being transmitted (see #setTransmitReturnValue and #setTransmitExceptions):

    RemoteObject remoteObject = (RemoteObject)someObject;
    remoteObject.setNonBlocking(true);
    someObject.doSomething();
    // ...
    SomeResult result = remoteObject.waitForLastResponse();

    RemoteObject remoteObject = (RemoteObject)someObject;
    remoteObject.setNonBlocking(true);
    someObject.doSomething();
    byte responseID = remoteObject.getLastResponseID();
    // ...
    SomeResult result = remoteObject.waitForResponse(responseID);

KryoNet versus ?

Because KryoNet solves a specific problem (a simple networking API with a powerful serialization solution), the KryoNet API can do so very elegantly. However, KryoNet makes the assumptions that it will only be used for client/server architectures and that it will be used on both sides of the network. For you to make an informed decision, here are some common alternatives to KryoNet:

  • Netty is another popular networking framework. However, it does not offer a robust out-of-the-box serializing solution like KryoNet and seems to have a steeper learning curve. It also does not intrinsically support RMI.

  • The Apache MINA project is similar to KryoNet. MINA's API is lower level and a great deal more complicated. Even the simplest client/server will require a lot more code to be written. Furthermore, MINA is not integrated with a robust serialization framework and doesn't support RMI out-of-the-box.

  • JRakNet is a java port of the C++ networking engine RakNet and based on Netty. However, it has some shortcomings featurewise (no RMI, no serialization framework) and is a lot less flexible than other networking frameworks.

  • [Discontinued] The PyroNet project is a minimal layer over NIO. It provides TCP networking similar to KryoNet, but without the higher level features. Priobit requires all network communication to occur on a single thread.

  • [Disconitnued] The Java Game Networking project is a higher level library similar to KryoNet. JGN does not have as simple of an API.


Download

It is recommended to use jitpack to import this lib.

An example for the use with gradle (code snippets for other build tools can be found here):

allprojects {
   repositories {
      // ...
      maven { url 'https://jitpack.io' }
   }
}
	
dependencies {
   compile 'com.github.crykn:kryonet:2.22.8'
}

Further reading

Beyond this documentation page, you may find the following links useful:

kryonet's People

Contributors

anuken avatar badlogic avatar codetaylor avatar crykn avatar desertkun avatar ekeitho avatar geolykt avatar georgeto avatar joaquinodz avatar jrenner avatar magro avatar murray-andrew-r avatar nathansweet avatar payne911 avatar tudalex 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

kryonet's Issues

package does not exist: gradle

I am trying to migrate from the main repository to here in my project.
I have changed my build.gradle to the following and already had jitpack/maven in my repos:

dependencies {
...
    implementation 'com.github.crykn:kryonet:2.22.8'
}

This is the error I get when trying to run:

/Users/asher/code/computer_science/2023/chess/src/main/java/network/KryoRegister.java:5: error: package com.esotericsoftware.kryo does not exist
import com.esotericsoftware.kryo.Kryo;
                                ^
/Users/asher/code/computer_science/2023/chess/src/main/java/network/KryoRegister.java:23: error: cannot find symbol
    private static void registerWithKryo (Kryo kryo) {
                                          ^
  symbol:   class Kryo
  location: class KryoRegister
/Users/asher/code/computer_science/2023/chess/src/main/java/network/KryoRegister.java:49: error: cannot find symbol
        Kryo kryo = client.getKryo();
        ^
  symbol:   class Kryo
  location: class KryoRegister
/Users/asher/code/computer_science/2023/chess/src/main/java/network/KryoRegister.java:54: error: cannot find symbol
        Kryo kryo = server.getKryo();
        ^
  symbol:   class Kryo
  location: class KryoRegister
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
4 errors

My imports in the class KryoRegister look like so:

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Server;

Should my imports look different?
Thanks.

Thread hanging on Client.dispose()/ Client.close()

I noticed this issue over at EsotericSoftware /
kryonet : EsotericSoftware#142

and remembered that I spent quite a bit of time a few months ago trying to diagnose this, unsuccessfully. I'm curious if that's something that could be addressed on this forked repo in light of some of the info that was posted recently?

Unable to build project

I'm trying to build the latest release KryoNet 2.22.7 to give this project a try, however I can't get it to build. I am trying to create a jar file to include with my project. This is the output the gradlew.bat gives me when issuing "gradlew build":

E:\Eclipse\workspace\Supergalactix\Supergalactix-core\jars\kryonet-2.22.7>gradlew build
Configuration on demand is an incubating feature.
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/kryo/5.0.0/kryo-5.0.0.pom
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/reflectasm/1.11.9/reflectasm-1.11.9.pom
Download https://repo.maven.apache.org/maven2/org/sonatype/oss/oss-parent/7/oss-parent-7.pom
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/kryo-parent/5.0.0/kryo-parent-5.0.0.pom
Download https://jitpack.io/com/github/esotericsoftware/jsonbeans/0.9/jsonbeans-0.9.pom
Download https://repo.maven.apache.org/maven2/org/objenesis/objenesis/3.0.1/objenesis-3.0.1.pom
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/minlog/1.3.1/minlog-1.3.1.pom
Download https://repo.maven.apache.org/maven2/org/objenesis/objenesis-parent/3.0.1/objenesis-parent-3.0.1.pom
Download https://repo.maven.apache.org/maven2/org/objenesis/objenesis/3.0.1/objenesis-3.0.1.jar
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/minlog/1.3.1/minlog-1.3.1.jar
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/reflectasm/1.11.9/reflectasm-1.11.9.jar
Download https://repo.maven.apache.org/maven2/com/esotericsoftware/kryo/5.0.0/kryo-5.0.0.jar
Download https://jitpack.io/com/github/esotericsoftware/jsonbeans/0.9/jsonbeans-0.9.jar

Task :compileJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Download https://repo.maven.apache.org/maven2/junit/junit/4.12/junit-4.12.pom
Download https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.pom
Download https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom
Download https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
Download https://repo.maven.apache.org/maven2/junit/junit/4.12/junit-4.12.jar

Task :compileTestJava
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Task :test

com.esotericsoftware.kryonet.JsonTest > testJson FAILED
junit.framework.AssertionFailedError: TCP data is not equal on client. (expected: Data, actual: Data)
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.TestCase.fail(TestCase.java:227)
at com.esotericsoftware.kryonet.JsonTest.testJson(JsonTest.java:115)

23 tests completed, 1 failed

Task :test FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':test'.

There were failing tests. See the report at: file:///E:/Eclipse/workspace/Supergalactix/Supergalactix-core/jars/kryonet-2.22.7/build/reports/tests/test/index.html

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.9/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 2m 9s
4 actionable tasks: 4 executed

Kyro 5.3

Hey, thank you very much for your effort in updating the library! Are you going to update the dependencies to version kyro 5.3 any time soon?
I am thinking about doing it myself and creating a PR if that is alright with you :)

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.