So I wrote C++ library that provides a scalable TCP server using Windows I/O Completion Port (IOCP).
Couple weeks ago, I started studying IOCP to improve the scalability of a C++ application that may handle thousands of TCP/IP data stream.
It didn’t take long for me to realize why IOCP has the reputation of being difficult to learn. IOCP tutorial online usually fall into the category of difficult to read, overly simplified, or just plain wrong.
Worse yet, Winsock2 contains a mix of confusing APIs that perform very similar functions with subtle differences. I spent a few days just to decide whether I should use WSAAccept, accept or AcceptEx to accept a connection.
Eventually, I stumbled onto two books that helped me out a great deal – Windows Via C++ and Network Programming For Windows.
The Library
The library interface is rather simple. It follows the Proactor design pattern where user supplies a completion handler and event notifications flow through the completion handler asynchronously.
Everyone uses echo server as tutorial. So what the heck, here’s mine. 🙂
class CEchoHandler : public CIocpHandler { public: virtual void OnReceiveData( uint64_t clientId, std::vector<uint8_t> const &data) { // echo data back directly to the connected client std::vector<uint8_t> d(data); GetIocpServer().Send(clientId, d); } } void main() { // create a handler that echos data back boost::shared_ptr h(new CEchoHandler()); try { // bind to port 50000 with the server CIocpServer *echoServer = new CIocpServer(50000,h); char c; std::cin >> c; // enter a key to exit delete echoServer; } // RAII constructor that throws different exceptions upon failure catch(...) { } }
[10/27/2010 10:34AM EST]
Update: Moved “delete echoServer;” to within the try block per co-worker’s suggestion.
Focus
Of course, there are more to the IOCP server than the code snippet above.
Here are my area of focus when designing the library.
- Scalability – By ensuring that there are minimum number of critical section in the library.
- TCP Graceful shutdown – Allow user to perform TCP graceful shutdown and simplify the TCP half-closed state.
- RAII – A WYSIWYG constructor and a lenient destructor that allows you to do ungraceful shutdown.
Here is a screenshot of the CPU utilization of the echo server at 300 concurrent loopback file transfer.
License
IOCPServer is released under the Boost Software License 1.0.
Download
For latest version, please see the Projects page.
IOCPServer is tested under the following configurations.
OS: Window XP, Window 7.
Compiler: Visual Studio 2009 with Boost 1.40
Build Type: ANSI, Unicode.