Tcp + iocp handling data receive and reusable sockets

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

I'm going to build mmorpg server with reusable sockets

Question 1

An accepted socket (via AcceptEx and posted one WSARecv()) is disconnected by peer (by zero byte notify receiving) in that case, could I call TransmitFile (TF_DISCONNECT | TF_REUSE_SOCKET) ? if its not possible, what should I do?

Question 2

If my server received an illegal packet or some hack attempts by peer, so I've to disconnect peer, how could I disconnect peer immediately (non-graceful) and make peer socket reusable?

Question 3

Some pseudo codes

1. Method

Enqueue received data and post new WSARecv() directly, then process received data (in order to, get received data(s) and enqueue them while processing received packets)

case OVL_RECV:

{

CircularBuffer->WriteData(recvBuffer, dwRecvBytes);

WSARecv(socket, recvBuffer, dwRecvBytes)

Lock();

char buf[4096];

int read = CircularBuffer->ReadData(buf, sizeof buf);

ProcessReceive(buf, read);

Unlock();

}

or

2. Method

Process received data and post new WSARecv()

case OVL_RECV

{

Lock();

ProcessReceive(recvBuffer, dwRecvBytes);

WSARecv(socket, recvBuffer, dwRecvBytes);

Unlock();

}

which method is better for performance ?

Question 4

DisconnectEx (TF_REUSE_SOCKET) versus TransmitFile (TF_DISCONNECT | TF_REUSE_SOCKET), are there any performance issues? which one should I use for make socket reusable?

Advertisement
What is your motivation for wanting to reuse sockets? I've never seen a need for it, and it seems to cause more headaches than it's worth for most people who try it.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

What is your motivation for wanting to reuse sockets? I've never seen a need for it, and it seems to cause more headaches than it's worth for most people who try it.

High performance, but i'm not sure going to use it or not, for now just collecting knowledge.

Socket reuse is not really going to affect your performance unless you plan on dropping connections at the rate of thousands a second ;-)

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Socket reuse is not really going to affect your performance unless you plan on dropping connections at the rate of thousands a second ;-)

If server got small ddos attacks (connect-close only), server should handle them until firewall block IPs. Otherwise its possibly to occur lags. However money care for actual ddos attacks (Using multiple machines and high-speed network)

If I would like to attack a server I would not close the socket. Only sending a syn-paket that will open the socket on your accept and let it starve. This way I send you 60k sync packets, that would be very quickly done at thats it for your server.

I think socket reuse is nothing that helps you anything against an attack.

I would think more about reusing your management for the sockets. If you are building the application with something object oriented language it would be a good thing to prevent the applicaion of constructing and deleting objects all the time.

If I would like to attack a server I would not close the socket. Only sending a syn-paket that will open the socket on your accept and let it starve. This way I send you 60k sync packets, that would be very quickly done at thats it for your server.

I think socket reuse is nothing that helps you anything against an attack.

I would think more about reusing your management for the sockets. If you are building the application with something object oriented language it would be a good thing to prevent the applicaion of constructing and deleting objects all the time.

You are right.

-- Waiting answer for question 3

Aja.

I wrote my own web-server some month ago. I do the reading from the socket in a single thread that handles all incoming data. 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.

If you use multiple network interfaces you may have multiple threads one for each interface.

The working may be done in a different thread that waits for data to be passed in.

Waiting answer for question 3
If there is any difference, it will be diminuitive. Starting the receive outside the critical section might arguably be 0.000001% faster, since it allows a kernel thread to do the copy from the receive buffer to your application while you are still inside the critical section. If there is something to receive, that is. The other version will do this memcpy the moment you call WSARecv.

All in all, this won't scale great. You are doing the processing in the completion hander while holding a lock. Which means you will always only process exactly one receive at a time, and you will at most have one in-flight receive. That is actually a much more serious performance thing to consider than whether to reuse sockets, or whether to use a flag in TransmitFile or some other call.

Why not post receives to a queue and have a worker pool process them? Or, since you already use a completion port anyway, the completion port can do that for you automatically anyway. But of course holding a lock while processing defeats the purpose.

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.

This topic is closed to new replies.

Advertisement