Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


Tcp + iocp handling data receive and reusable sockets


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
15 replies to this topic

#1 rigel   Members   -  Reputation: 126

Like
0Likes
Like

Posted 08 July 2014 - 03:42 PM

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?


Edited by rigel, 08 July 2014 - 03:45 PM.


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15975

Like
0Likes
Like

Posted 08 July 2014 - 03:56 PM

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.

#3 rigel   Members   -  Reputation: 126

Like
0Likes
Like

Posted 08 July 2014 - 04:07 PM

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.


Edited by rigel, 08 July 2014 - 04:09 PM.


#4 ApochPiQ   Moderators   -  Reputation: 15975

Like
0Likes
Like

Posted 08 July 2014 - 07:17 PM

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

#5 rigel   Members   -  Reputation: 126

Like
0Likes
Like

Posted 09 July 2014 - 02:56 AM

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)



#6 Tribad   Members   -  Reputation: 872

Like
4Likes
Like

Posted 09 July 2014 - 03:28 AM

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.



#7 rigel   Members   -  Reputation: 126

Like
0Likes
Like

Posted 09 July 2014 - 05:19 AM

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


Edited by rigel, 09 July 2014 - 05:23 AM.


#8 Tribad   Members   -  Reputation: 872

Like
0Likes
Like

Posted 09 July 2014 - 05:37 AM

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.



#9 samoth   Crossbones+   -  Reputation: 4910

Like
2Likes
Like

Posted 09 July 2014 - 05:57 AM

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.



#10 samoth   Crossbones+   -  Reputation: 4910

Like
1Likes
Like

Posted 09 July 2014 - 06:48 AM

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.



#11 rigel   Members   -  Reputation: 126

Like
0Likes
Like

Posted 09 July 2014 - 08:18 AM

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 ?



#12 samoth   Crossbones+   -  Reputation: 4910

Like
1Likes
Like

Posted 09 July 2014 - 10:10 AM

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).



#13 hplus0603   Moderators   -  Reputation: 5507

Like
1Likes
Like

Posted 10 July 2014 - 10:30 AM

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 };

#14 Tribad   Members   -  Reputation: 872

Like
0Likes
Like

Posted 10 July 2014 - 11:39 AM

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.

#15 hplus0603   Moderators   -  Reputation: 5507

Like
0Likes
Like

Posted 10 July 2014 - 11:53 AM

Great, That sounds like the classic event-driven single-threaded networking model, which usually works very well!
enum Bool { True, False, FileNotFound };

#16 Tribad   Members   -  Reputation: 872

Like
0Likes
Like

Posted 10 July 2014 - 12:09 PM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS