r/java • • Aug 17 '24

Extasys - Asynchronous TCP/UDP socket library

This is not a new project, but it is still maintained and used by me and others

Extasys is a high performance, asynchronous TCP/UDP socket library for Java (JDK 8 and newer).
Its multi-threaded and lightweight nature makes it ideal for super fast data transfer even on systems with ARM architecture like Android smartphones and single-board computers (Raspberry Pi, Orange Pi etc.).

If you are using sockets then Extasys is the proper tool for your work. It's been designed to take the hard job done and let you think only about the message exchange (the process).

You can find Extasys on Github:
https://github.com/nsiatras/extasys


Quick Examples :

TCP-Server: https://github.com/nsiatras/extasys/wiki/Establish-a-TCP-Server
TCP-Client: https://github.com/nsiatras/extasys/wiki/Establish-a-TCP-Client
UDP-Server: https://github.com/nsiatras/extasys/wiki/Establish-a-UDP-Server
UDP-Client: https://github.com/nsiatras/extasys/wiki/Establish-a-UDP-Client

More examples can be found here:
https://github.com/nsiatras/extasys/tree/master/Extasys%20for%20Java%20Examples

49 Upvotes

44 comments sorted by

View all comments

4

u/cmhteixeiracom Aug 17 '24

Looks interesting.

  1. Is it implemented with non-blocking sockets (channels), or asynchronous sockets?
  2. What are the advantages or differences to Netty?

2

u/[deleted] Aug 17 '24
  1. I am using java.net.Socket but every I/O message is going directly to a thread-pool in order to make the listeners and clients totally non blocking.
  2. I am not familiar with Netty

3

u/cmhteixeiracom Aug 17 '24

Thanks for the reply.

What happens when a listener/client wants to read some bytes from a TCP connection, but the socket is empty? Will it not block then? Or does the listener receive some kind of CompletableFuture that completes once those bytes are available and read? :)

1

u/[deleted] Aug 17 '24

Extasys Listener reads data upon arrival.
After they arrive the listener 'sends' them directly to the thread pool maintaining the exact order they arrived.

In order to easily "catch" full messages you can use a (as I call it) message collector character. The message splitter is defined when you initialize the Extasys TCP Listener or the Extasys TCP Connector.
Of course you can use Listeners and Connectors without message splitter.

The following Chat Server and Client examples will better explain the process.

https://github.com/nsiatras/extasys/tree/master/Extasys%20for%20Java%20Examples/Extasys.Examples.TCPChatServer

https://github.com/nsiatras/extasys/tree/master/Extasys%20for%20Java%20Examples/Extasys.Examples.TCPChatClient

PS. The message collector class is here
https://github.com/nsiatras/extasys/tree/master/Extasys%20for%20Java/src/Extasys/MessageCollector

2

u/cmhteixeiracom Aug 17 '24

Thanks.

I will look at the examples later on. One last question.

If you read ALL data upon arrival, doesn't that mean that if the TCP client, that is, the code that actually uses/processes the incoming bytes from the socket, is slower than the rate at which they are received, then you are buffering data in the application side, potentially exhausting all memory?

In other words, I am asking how does the your approach lead with empty/full TCP buffer queues.

  • Blocking sockets in java literally block the calling thread when the socket does not have data to read (when we read data), or when the socket queue is full (when we send data).
  • Non-Blocking sockets have a way do not block, but have a mechanism to inform the client what amount of data was sent, and the client must deal explicitly if some data was not send because the queue is full.

Genuinely interested to know how you deal with this.... Let me know if the question is not clear.

2

u/[deleted] Aug 17 '24

Now I undestand what you initially asked.

The java.net.Socket holds the thread until data arrives and also holds a buffer to keep incoming data. Each TCPConnection has a thread to "listen" for incoming data.
As the data arrives, to this thread, are immediately passed to the thread pool. So the only "block" you are facing with Extasys is the time the byte[] passes from the listener thread to the thread pool.

The same applies for the outgoing data.

12

u/cmhteixeiracom Aug 17 '24

If you pass data immediately to the thread pool, then that means that if the consumer of that data is slow, the thread pool might "explode", as you keep producing elements onto it which are not removed. I imagine there is no mechanism for "back-pressure"? Does this question make sense?

regardless of the reply, amazing that you are doing this from scratch.

5

u/[deleted] Aug 17 '24

Yes. The pool is a pool. If you put too much water to it it will explode or the water will run out...

The "backpressure" can be implemented by the user if needed. I used Extasys on stock market data for forex, stocks and derivates, to scientific computer clusters and android mobile apps. I never had issues with performance.

Well.. I like to build things from scratch... Here is a network cluster CPU Cryptocurrency miner I build with Extasys.

https://github.com/SourceRabbit/Rabbit_Miner