In my single-threaded app I use
SDLNet_CheckSockets -> SDLNet_SocketReady -> SDLNet_TCP_Recv
chain in order to receive packets without blocking. I use 512 as the buffer size when calling SDLNet_TCP_Recv. And most of the time packets fit well into that. But what if I receive several packets at once? Or large packets?
- I can't blindly call
SDLNet_TCP_Recvmultiple times because I don't know if it will block or not. If there isn't any more data it blocks. If I receive one packet which is exactly the size of my buffer then I will have no idea if there are more of them waiting or not. - Calling
SDLNet_SocketReadyafter eachSDLNet_TCP_Recvcall doesn't work. It only returnstruethe first time. Then it returnsfalseeven when there is actually more data to read (tested it by blindly callingSDLNet_TCP_RecvwhenSDLNet_SocketReadyreturnedfalse). - Calling
SDLNet_CheckSocketsafter everySDLNet_TCP_Recvcall looks to me as a very bizarre option. Surely this is not the way? Why do I have to consider ALL sockets each time I read a few more bytes from one socket? That ruins class structure of my program and requires dirty hacks to work.
All tutorials and examples that I see don't concern themselves with the possibility that a socket can receive more data than its buffer size (which is typically small). As if it doesn't happen. But surely it does happen, websockets can express their size with a 64-bit integer. I don't want to allocate that much unless I know I have to. And, again, several small packets can simply stack up.
How should I approach this?
Using UDP is not an option since I deal with websockets.