- Notifications
You must be signed in to change notification settings - Fork0
TCP/UDP client/server library for Java, based on Kryo
License
JasonPEP/kryonet
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
KryoNet can be downloaded on thereleases page. Please use theKryoNet discussion group for support.
KryoNet is a Java library that provides a clean and simple API for efficient TCP and UDP client/server network communication using NIO. KryoNet uses theKryo serialization library to automatically and efficiently transfer object graphs across the network.
KryoNet runs on both the desktop and onAndroid.
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.
- Running a server
- Connecting a client
- Registering classes
- TCP and UDP
- Buffer sizes
- Threading
- LAN server discovery
- Pluggable Serialization
- Logging
- Remote Method Invocation (RMI)
- KryoNet versus ?
- Maven Build
- Further reading
This code starts a server on TCP port 54555 and UDP port 54777:
Serverserver =newServer();server.start();server.bind(54555,54777);
Thestart
method starts a thread to handle incoming connections, reading/writing to the socket, and notifying listeners.
This code adds a listener to handle receiving objects:
server.addListener(newListener() {publicvoidreceived (Connectionconnection,Objectobject) {if (objectinstanceofSomeRequest) {SomeRequestrequest = (SomeRequest)object;System.out.println(request.text);SomeResponseresponse =newSomeResponse();response.text ="Thanks";connection.sendTCP(response); } } });
Note the Listener class has other notification methods that can be overridden.
Typically a listener has a series ofinstanceof
checks to decide what to do with the object received. In this example, it prints out a string and sends a response over TCP.
The SomeRequest and SomeResponse classes are defined like this:
publicclassSomeRequest {publicStringtext; }publicclassSomeResponse {publicStringtext; }
Kryo automatically serializes the objects to and from bytes.
This code connects to a server running on TCP port 54555 and UDP port 54777:
Clientclient =newClient();client.start();client.connect(5000,"192.168.0.4",54555,54777);SomeRequestrequest =newSomeRequest();request.text ="Here is the request";client.sendTCP(request);
Thestart
method starts a thread to handle the outgoing connection, reading/writing to the socket, and notifying listeners. Note that the thread must be started beforeconnect
is called, else the outgoing connection will fail.
In this example, theconnect
method blocks for a maximum of 5000 milliseconds. If it times out or connecting otherwise fails, an exception is thrown (handling not shown). 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(newListener() {publicvoidreceived (Connectionconnection,Objectobject) {if (objectinstanceofSomeResponse) {SomeResponseresponse = (SomeResponse)object;System.out.println(response.text); } } });
For the above examples to work, the classes that are going to be sent over the network must be registered with the following code:
Kryokryo =server.getKryo();kryo.register(SomeRequest.class);kryo.register(SomeResponse.class);Kryokryo =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.
Please see theKryo serialization library for more information on how objects are serialized for network transfer. Kryo can serialize any object and supports data compression (eg, deflate compression).
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 port. 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 canaffect 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 doesn't matter if an 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.
KryoNet uses a few buffers for serialization and deserialization that must be sized appropriately for a specific application. See theClient
andServer
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
andInputStreamSender
can be used to queue more data only when the connection is idle. Also see thesetIdleThreshold
method on the Connection class.
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 therun
method continually calls update until thestop
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 thestart
method does. If this doesn't fit your needs, callupdate
manually from the thread of your choice.
Listeners are notified from the update thread, so should not block for long. Static wrapper classes are provided on the Listener class to change how a listener is notified, such as ThreadedListener.
The update thread should never be blocked to wait for an incoming network message, as this will cause a deadlock.
KryoNet can broadcast a UDP message on the LAN to discover any servers running:
InetAddressaddress =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.
KryoNet makes use of the low overhead, lightweightMinLog 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. SeeMinLog for more information.
Serialization can be customized by providing a Serialization instance to the Client and Server constructors. By default KryoNet usesKryo for serialization. Kryo uses a binary format and is very efficient, highly configurable, and does automatic serialization for most object graphs.
JsonSerialization is provided which usesJsonBeans to do serialization using JSON. JSON is human readable so is convenient for use during development to monitor the data being sent and received.
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 callingregisterClasses
, creating an ObjectSpace and registering objects with an ID:
ObjectSpace.registerClasses(endPoint.getKryo());ObjectSpaceobjectSpace =newObjectSpace();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:
SomeObjectsomeObject =ObjectSpace.getRemoteObject(connection,42,SomeObject.class);SomeResultresult =someObject.doSomething();
ThegetRemoteObject
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.
SomeObjectsomeObject =ObjectSpace.getRemoteObject(connection,42,SomeObject.class); ((RemoteObject)someObject).setNonBlocking(true,true);someObject.doSomething();
Note that the SomeObject class does not need to implement RemoteObject, this is handled automatically.
The firsttrue
passed tosetNonBlocking
causes remote method invocations to be non-blocking. WhendoSomething
is invoked, it will not block and wait for the return value. Instead the method will just return null.
The secondtrue
passed tosetNonBlocking
indicates that the return value of remote method invocations are to be ignored. This means the server will not waste time or bandwidth sending the result of the remote method invocation.
If the second parameter forsetNonBlocking
is false, the server will send back the remote method invocation return value. There are two ways to access a return value for a non-blocking method invocation:
RemoteObjectremoteObject = (RemoteObject)someObject;remoteObject.setNonBlocking(true,false);someObject.doSomething();// ...SomeResultresult =remoteObject.waitForLastResponse();RemoteObjectremoteObject = (RemoteObject)someObject;remoteObject.setNonBlocking(true,false);someObject.doSomething();byteresponseID =remoteObject.getLastResponseID();// ...SomeResultresult =remoteObject.waitForResponse(responseID);
KryoNet makes the assumptions that it will only be used for client/server architectures and that KryoNet will be used on both sides of the network. Because KryoNet solves a specific problem, the KryoNet API can do so very elegantly.
TheApache 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. MINA also is not integrated with a robust serialization framework and doesn't intrinsically support RMI.
ThePyroNet 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.
TheJava Game Networking project is a higher level library similar to KryoNet. JGN does not have as simple of an API.
<repositories> <repository> <id>clojars</id> <url>http://clojars.org/repo/</url> </repository></repositories><dependencies> <dependency> <groupId>kryonet</groupId> <artifactId>kryonet</artifactId> <version>2.21</version> </dependency></dependencies>
Beyond this documentation page, you may find the following links useful:
- Kryo (used to do the serialization in KryoNet)
- Example code
- Unit tests
About
TCP/UDP client/server library for Java, based on Kryo
Resources
License
Stars
Watchers
Forks
Packages0
Languages
- Java100.0%