Tcp + iocp handling data receive and reusable sockets

Started by
11 comments, last by Tribad 9 years, 9 months ago

I would try doing roughly like this:

Put your OVERLAPPED and the 4k receive buffer into one, like this:

struct recv_buf { OVERLAPPED overlapped; char data[4096]; };

Fire up a couple of threads (like, 5 or 10), and have them do an infinite loop around GetQueuedCompletionStatus, returning when some "special message" is posted to the completion port. The completion port will do the smart one-thread-per-core management for you, as per the documentation.

For each OVL_ACCEPT seen, pull one such recv_buf from an allocator (or list, or whatever). You have accepted a connection, so you want to receive. WSARecv into that structure, using &buf.data, sizeof(buf.data), and &buf.overlapped.

For each OVL_RECV seen, you get back an OVERLAPPED* from GetQueuedCompletionStatus, which is, as you know, really a pointer to a recv_buf (you were not 100% truthful when you told Windows that this was an OVERLAPPED, but you were truthful enough!). So you know where to find the received data, and you know that this buffer is used by you and only you. Nobody else could have gotten back this pointer at this time, so there is no need to lock anything, no need to do any memory management, or anything else. The structure was put into the IOCP's queue exactly once, and now it's no longer in there, you have it.

Process the buffer, and then WSARecv again using that same recv_buf structure. You still know for sure that nobody else could possibly be using it, so that's fine.

When a socket is closed, return the recv_buf to your allocator (or push it back to a list, or whatever). Someone else will eventually pull and reuse it.

Repeat forever.


When the server should exit, PostQueuedCompletionStatus your "special message" (for example code = 0, length = 0, handle = 0) and WaitForMultipleObjects(thread_handle_array, TRUE, INFINITE). Each thread receiving that message from the IOCP re-posts it (so all threads eventually get to see it) and then simply returns from the thread function.

The only thing that needs to be explicitly threadsafe is the allocator (list, whatever) from which you pull your recv_bufs, everything else is made threadsafe automatically by how the completion port works.

what about sending multiple packets ?

Advertisement

what about sending multiple packets ?
Sending works the same as receiving, only you'd use WSASend or TransmitPackets. The buffer must remain valid until the send has completed, which it does. You give it back to the allocator (so someone else can overwrite it) once you get the complete notification.

Using TransmitPackets, you can pack together several packets into one send, but I don't think you will gain an awful lot. First of all, they must all go to the same socket, so it isn't particularly useful to begin with. You usually want to send several packets to different computers, that is, to different sockets.

Second, collapsing an unknown number of packets into one send means you need to either manage a number of buffers somehow, which is painful (presumably you'd have to do something like above, and include an array of pointers in the structure that holds the OVERLAPPED -- otherwise you won't be able to find and recycle them once the send completes!). Or, you must allocate a single large block on the heap , which will stress-test the heap allocator and cause memory fragmentation (and likely destroy any gains you get from saving a few extra calls to TransmitPacket).

All in all, TransmitFile (or TransmitPackets) is by far less useful than it sounds at first. The same is true for e.g. sendfile under Linux which is great for a webserver, but not really all that useful otherwise (however, Linux does have a truly useful pair of functions if you use UDP: recvmmsg and sendmmsg -- these let you scatter/gather receive and send a whole bunch of datagrams from and to several different addresses in one call, sadly there exists no version that works with TCP).

The idea behind that is that you cannot receive data in parallel because they are passed through your networking line. Its serial. So it makes no sense to read the data in different threads.


For single packets (UDP datagrams less than 1.5 kB, say) this is true.
For network "streams" that are made up of multiple packets (such as TCP/HTTP) this is not true -- different clients will interleave packets on the wire.
In fact, if you read data only from a single connection, someone can lock up your server by opening a connection, and sending only a single byte, and then doing nothing (letting the connection stay open.)
enum Bool { True, False, FileNotFound };
Yes.

I use a signalfd where all socket IO gets signaled to. This way I can than use a blocking read from there to get a stream of events that each reads into buffers that are connected to the handle in nearly the same order as the packets come in from the wire.

This way I have a very simple loop the shuffles in data from the wire into a number of workers.
Great, That sounds like the classic event-driven single-threaded networking model, which usually works very well!
enum Bool { True, False, FileNotFound };
Its a bit better. I do not need to go through a signal handler. Using one of the real time signals instead of SIGIO gives me a signal queue. And the file descriptor is buffered itself.
What i do not implemented yet is the usage of different signals for http and https. But maybe comes later.

This topic is closed to new replies.

Advertisement