Java efficient Networking

@ashes987, check out this wicked api:

Create any class which extends Node. Add a few Leaf fields. Edit/modify the leaf fields. Sync the client and server with a simple trigger! Easy DX (as opposed to UX).

class Player extends Node {
    final LeafValue<Name> name = new LeafValue<>();
    final LeafValue<Position> position = new LeafValue<>();
    final LeafValue<Stats> stats = new LeafValue<>();


// Let's register a networked instance of this class.

// Server-side
register.add("player", new Player());

// Client-side
register.add("player", new Player());


// Now let the magic begin!

// Server-side
player.position.set(1, 2);

// Client-side
... after a few milliseconds
// printing player.position after update = 1, 2

Using a networking library like KryoNet and writing my own serializers will make this even more efficient inshaAllah. Currently however, it just sends delta updates. Only data which has changed since the last sync() call will be sent.

Yesterday I fixed one of the final bugs I was having with this. So glad it’s all good now. Now the API isn’t exactly like above, but I wanted to share the idea.

not flexible enough. This might work for movements, but you want to minimize the times less common data is sent. for example some ability activation or such should not be sent every time a sync is called.

Also, serialization is easy… just use java ogle protobufs serialization with the ObjectOutput/InputStream. Could also use google protobufs.

Also… why wrap in LeafValue?

lol the whole point of this was to prevent common data being sent every sync()! Currently, there are wrappers for values (LeafValue) and arrays (LeafArray) which track changes to the values. It’s also possible to nest these, for example, an array of arrays of arrays of integers.

Although I would’ve prefered not to have these wrappers, it was easy for me to implement it this way. The final result was worth it.

I use Kryo to do serialization. I’m not sure how it compares to the alternatives you mentioned (I’ve seen the term protobuf in so many places now lol). Although Kryo is wonderful, I’m still going to have to write my own serializer for the sync packets - not because it does a bad job but because I could save a byte or two per packet just due to the content of the packet.

EDIT: this is what I mean by my own serializer.

Whaooo!! I just found out about the Nashorn JavaScript engine inside Java!

That’s the coolest Java 8 feature ever! How did I miss this?!