• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
rigel

Tcp + iocp handling data receive and reusable sockets

12 posts in this topic

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
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

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
0

Share this post


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

Share this post


Link to post
Share on other sites

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)

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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 ?

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
Great, That sounds like the classic event-driven single-threaded networking model, which usually works very well!
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0