Skip to content

Commit

Permalink
Dependency Update + Clean up (#8)
Browse files Browse the repository at this point in the history
* Fix README.md issue

* Update kryo 5.0.0-RC9 to 5.2.0

* Update netty 4.1.51 to 4.1.68

* Update test-units: Use normal endpoints instead of 'Threaded'

* Delete ThreadedEndpoints

* Update + Optimize KryoNetty

* Fix wrong Endpoint variable name

* Add Consumer<Kryo> for custom Kryo initialization

* Remove ThreadedEndpoint documentation

* Refactor Bootstrap variable in Client- and ServerEndpoint

* Update dependencies

* Change shutdown order

* Update gradle dependencies and plugins

* Update gradle wrapper to 7.2

* Update README.md
  • Loading branch information
Koboo authored Dec 16, 2022
1 parent c3609ee commit 678adb8
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 119 deletions.
49 changes: 7 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ KryoNetty is very similar to [KryoNet](https://github.com/EsotericSoftware/kryon
- [Build](#build-from-source)
- [KryoSerialization](#why-kryoserialization)


## How to KryoNetty works
`KryoNetty` passes the configuration of the different components to the classes & references behind it.

Expand Down Expand Up @@ -63,7 +62,6 @@ Or with index-ids:
.register(103, Random.class);
```


Every option returns the current instance of `KryoNetty`<br>
Current Options:
- `useLogging()`
Expand All @@ -85,9 +83,6 @@ Current Options:
- 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.
Expand All @@ -97,16 +92,6 @@ To start the `Server`, the following call `start(int port)` is all you need. The
server.start(56566);
```

If you want to let the `Server` run in another thread, use the `ThreadedServer`. This starts the server in a `Executors.newSingleThreadExecutor()`. Just replace `Server` with `ThreadedServer`:

```java
Server server = new ThreadedServer(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.
Expand All @@ -116,16 +101,6 @@ The `Client` configuration works quite similar to the `Server`. The only differe
client.connect("localhost", 56566);
```

Same way with the `ThreadedClient`

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




## How to register an Event

In contrast to [KryoNet](https://github.com/EsotericSoftware/kryonet), the event system works a little differently. First of all there are the three following events:
Expand Down Expand Up @@ -189,7 +164,7 @@ Here an example to process an object which is fired via a `ReceiveEvent`.

```

## Add as dependecy
## Add as dependency

First of all add `jitpack.io` as repository.

Expand All @@ -208,30 +183,20 @@ After that you can add it as dependency.

## Build from source

If you want to build `kryonetty` from source, clone this repository and run `./gradlew buildKryoNetty`. The output-file will be in the directory: `/build/libs/kryonetty-{version}-all.jar`
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.
If you are interested in the build task:

```java
task buildKryoNetty(type: Jar) {
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jar
}
```
- [ShadowJar Plugin](https://imperceptiblethoughts.com/shadow)

## 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:)
- https://github.com/EsotericSoftware/kryo#benchmarks
- https://github.com/eishay/jvm-serializers/wiki
- [Kryo Benchmarks](https://github.com/EsotericSoftware/kryo#benchmarks)
- [JVM Serializers](https://github.com/eishay/jvm-serializers/wiki)

Since we work with a `Kryo`, `Input` & `Output` in a `Pool<?>` from `kryo-5.0.0`, classes are passed to the `KryoSerialization` constructor for registration & initialization.
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](http://groups.google.com/group/kryonet-users) for [Kryo](https://github.com/EsotericSoftware/kryo)/[KryoNet](https://github.com/EsotericSoftware/kryonet)-specific support. <br>
26 changes: 9 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

group 'com.esotericsoftware'
version '0.9'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
Expand All @@ -20,23 +22,13 @@ repositories {


dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.carrotsearch', name: 'junit-benchmarks', version: '0.7.2'
testImplementation 'junit:junit:4.13.1'
testImplementation 'com.carrotsearch:junit-benchmarks:0.7.2'

compile group: 'io.netty', name: 'netty-buffer', version: '4.1.68.Final'
compile group: 'io.netty', name: 'netty-transport', version: '4.1.68.Final'
compile group: 'io.netty', name: 'netty-transport-native-epoll', version: '4.1.68.Final'
compile group: 'io.netty', name: 'netty-handler', version: '4.1.68.Final'
implementation 'io.netty:netty-buffer:4.1.86.Final'
implementation 'io.netty:netty-transport:4.1.86.Final'
implementation 'io.netty:netty-transport-native-epoll:4.1.86.Final'
implementation 'io.netty:netty-handler:4.1.86.Final'

compile group: 'com.esotericsoftware', name: 'kryo', version: '5.2.0'
}

task buildKryoNetty(type: Jar) {
baseName = project.name + '-all'
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jar
implementation 'com.esotericsoftware:kryo:5.3.0'
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class ClientEndpoint extends Endpoint {

private final EventLoopGroup group;
private Bootstrap bootstrap;
private final Bootstrap bootstrap;
private Channel channel;

public ClientEndpoint(KryoNetty kryoNetty) {
Expand Down Expand Up @@ -118,8 +118,8 @@ public void closeChannel(){
*/
public void close() {
getEventHandler().unregisterAll();
group.shutdownGracefully();
closeChannel();
group.shutdownGracefully();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

public class ServerEndpoint extends Endpoint {

private final ServerBootstrap bootstrap;
private final EventLoopGroup bossGroup;
private final EventLoopGroup workerGroup;
private final ServerBootstrap bootstrap;
private Channel channel;

public ServerEndpoint(KryoNetty kryoNetty) {
Expand Down Expand Up @@ -72,12 +72,12 @@ public void close() {
// unregister network-events
getEventHandler().unregisterAll();

// close server-channel
channel.close();

// shutdown eventloop-groups
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();

// close server-channel
channel.close();
}

/**
Expand Down
66 changes: 38 additions & 28 deletions src/main/java/com/esotericsoftware/kryonetty/kryo/KryoNetty.java
Original file line number Diff line number Diff line change
@@ -1,76 +1,86 @@
package com.esotericsoftware.kryonetty.kryo;

import com.esotericsoftware.kryo.Kryo;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class KryoNetty {

boolean useLogging;
boolean useExecution;
int executionThreadSize;
Consumer<Kryo> initializationConsumer;

HashMap<Integer, Class<?>> classesToRegister;
int inputBufferSize;
int outputBufferSize;
int maxOutputBufferSize;
boolean useLogging = false;
boolean useExecution = false;
int executionThreadSize = 1;

Map<Integer, Class<?>> classesToRegister = new ConcurrentHashMap<>();
int inputBufferSize = -1;
int outputBufferSize = -1;
int maxOutputBufferSize = -1;

public KryoNetty() {
this.useLogging = false;
this.useExecution = false;
this.executionThreadSize = 1;
}

this.classesToRegister = new HashMap<>();
this.inputBufferSize = -1;
this.outputBufferSize = -1;
this.maxOutputBufferSize = -1;
public KryoNetty initialization(Consumer<Kryo> consumer) {
initializationConsumer = consumer;
return this;
}

public KryoNetty useLogging() {
this.useLogging = true;
useLogging = true;
return this;
}

public KryoNetty useExecution() {
this.useExecution = true;
useExecution = true;
return this;
}

public KryoNetty threadSize(int size) {
this.executionThreadSize = size;
executionThreadSize = size;
return this;
}

public KryoNetty register(int index, Class<?> clazz) {
this.classesToRegister.put(index, clazz);
classesToRegister.put(index, clazz);
return this;
}

public KryoNetty register(Class<?> clazz) {
this.classesToRegister.put(this.classesToRegister.size() + 1, clazz);
classesToRegister.put(this.classesToRegister.size() + 1, clazz);
return this;
}

public KryoNetty register(Class<?>... clazzez) {
if(clazzez.length != 0)
Arrays.stream(clazzez).forEach(clazz -> this.classesToRegister.put(this.classesToRegister.size() + 1, clazz));
if(clazzez.length != 0) {
for(Class<?> clazz : clazzez) {
classesToRegister.put(classesToRegister.size() + 1, clazz);
}
}
return this;
}

public KryoNetty inputSize(int inputBufferSize) {
this.inputBufferSize = inputBufferSize;
public KryoNetty inputSize(int newInputSize) {
inputBufferSize = newInputSize;
return this;
}

public KryoNetty outputSize(int outputBufferSize) {
this.outputBufferSize = outputBufferSize;
public KryoNetty outputSize(int newOutputSize) {
outputBufferSize = newOutputSize;
return this;
}

public KryoNetty maxOutputSize(int maxOutputBufferSize) {
this.maxOutputBufferSize = maxOutputBufferSize;
public KryoNetty maxOutputSize(int newMaxOutputSize) {
maxOutputBufferSize = newMaxOutputSize;
return this;
}

protected Consumer<Kryo> getInitializationConsumer() {
return initializationConsumer;
}

public boolean isUseLogging() {
return useLogging;
}
Expand All @@ -83,7 +93,7 @@ public int getExecutionThreadSize() {
return executionThreadSize;
}

protected HashMap<Integer, Class<?>> getClassesToRegister() {
protected Map<Integer, Class<?>> getClassesToRegister() {
return classesToRegister;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ protected Kryo create() {
kryo.register(PriorityQueue.class, 45);
kryo.register(BitSet.class, 46);

if(kryoNetty.getInitializationConsumer() != null) {
kryoNetty.getInitializationConsumer().accept(kryo);
}

// Register KryoNetty Classes
if (!kryoNetty.getClassesToRegister().isEmpty())
kryoNetty.getClassesToRegister().forEach((key, value) -> kryo.register(value, (key + 100)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

public class KryoNettyDecoder extends ByteToMessageDecoder {

private final Endpoint IEndpoint;
private final Endpoint endpoint;

public KryoNettyDecoder(Endpoint IEndpoint) {
this.IEndpoint = IEndpoint;
public KryoNettyDecoder(Endpoint endpoint) {
this.endpoint = endpoint;
}

@Override
Expand Down Expand Up @@ -40,7 +40,7 @@ protected void decode (ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
in.readBytes(objectBytes);

// Get object-decoding methods from KryoSerialization
Object object = IEndpoint.getKryoSerialization().decodeObject(objectBytes);
Object object = endpoint.getKryoSerialization().decodeObject(objectBytes);

// add the object to the output-list
out.add(object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

public class KryoNettyEncoder extends MessageToByteEncoder<Object> {

private final Endpoint IEndpoint;
private final Endpoint endpoint;

public KryoNettyEncoder(Endpoint IEndpoint) {
this.IEndpoint = IEndpoint;
public KryoNettyEncoder(Endpoint endpoint) {
this.endpoint = endpoint;
}

protected void encode (ChannelHandlerContext ctx, Object object, ByteBuf out) {
// Get object-encoding method from KryoSerialization
byte[] objectBytes = IEndpoint.getKryoSerialization().encodeObject(object);
byte[] objectBytes = endpoint.getKryoSerialization().encodeObject(object);

// Write the length to the output-buffer
out.writeInt(objectBytes.length);
Expand Down
Loading

0 comments on commit 678adb8

Please sign in to comment.