Search code examples
libuv

How to wait for more data in libuv?


I am testing on libuv for server.

The problem is that the data is to arrive partially and I need to wait for more data. (this situation hasn't occurred yet but I think it is around the corner, in my experience)

But this libuv is dependent on event base, and next after_read() function doesn't guarantee the next packet would be the leftover.

I wonder how you work around. Thank you.


Added for more information. My concerned situation of splitted packet.

  • Assume Client A, Client B, Server S.

    1. A sends 100 bytes to server.
    2. B sends 100 bytes to server.
    3. S receives A's first 50 bytes. (in read callback)
    4. S receives B's 100 bytes. (in read callback)
    5. S receives A's rest 50 bytes. (in read callback)

After step 3, I expect the rest A's 50bytes to arrive , but unlike my expectation, B's 100 bytes arrive. my goal is to merge step 3 (50bytes) + step 5 (50bytes) for A's entire data.


Solution

  • In libuv you don't wait for data, you request that data is read by calling uv_read_start and then the read callback will be called when there is data to read. If your application needs to read complete packets of data, you typically want to use a state machine to know what state you're in, and keep buffering data until you have a full packet.