Search code examples
javaniobytebuffer

Alternatives to ByteBuffers for a low-level UDP messaging system


I'm working on a low-level UDP messaging layer for an encrypted P2P architecture, if interested you can read more about it on its github page.

I've built a neat serialization framework that turns POJOs into compact ByteBuffers, and also various libraries that make using symmetric and asymmetric crypto fairly painless.

I'm now working on the messaging framework, which makes use of dynamic proxies to achieve similar functionality to GWT's RPC mechanism.

My problem is that early-on I decided to make the serialization mechanism read to and write from ByteBuffers. I'm now finding that this has several problems:

  • You need to know the maximum bytebuffer size in advance of serializing an object
  • They are mutable which makes them error-prone
  • They aren't especially compatible with DatagramPacket, and DatagramChannels are confusing

Can anyone suggest alternative ways to implement serialization in this framework?


Solution

  • Might be worth having a look at KryoNet - it may fit your needs, or alternatively you can take a look at how they do things under the hood.

    Incidentally, they do use ByteBuffers for both TCP and UDP connections. I think the trick is to abstract away from ByteBuffers so that any client code can just work with POJOs. To do this, you clearly will need to have logic somewhere that can break a large object into multiple buffered writes.

    I think you actually still want ByteBuffers under the hood since they are very fast, support various native acceleration tricks and because like it or not, ultimately you need to deal with buffering in a high throughput IO environment....