Just get some free time for writing my blog. Let's do some thing planned long time ago -- to build a tcp server.
We all know server is necessary for online applications. It helps players communicate with each other. But existing servers can not always fulfill our various demands. So I just write this article to record my research and build my own customizable server. By the way, tcp is the base for http and websocket. So after buiding a raw tcp server, it's also on the half way for the others.
Just for clarifying my aim, the server should be fast, cross-platform, highly scalable, and easy to use. After doing some research on the Internet, I decided to use node.js's async io library --libuv(http://libuv.org/). Now it's much better documented than several years ago. The library is written in C, but I want to integrate it with some C++'s features. Just for saving my life.
Libuv uses a non-blocking event loop:
It's a single threaded loop, so should not be very hard for implementing a server.
After reading some examples including the simple tcp echo server provided in the official github thread, I was confused about how to distinguish the messages from different clients. Later I found the anwser in the document, the "data" variable in "uv_handle_t" is designed to be used for delivering user defined data. It seems I can use it to pass things to the callbacks.
After the only problem being solved, it is time to make a simple design for our server.