Server-side programming using the C++11 network library handy

Source: Internet
Author: User

Handy is a simple and elegant c++11 network library for Linux and Mac platforms. 10 lines of code to complete a complete network server.

The following is the code for the Echo server:

#Include>using namespace std;using namespace handy;IntMainint argc,Constchar* argv[]) {eventbase bases;Event DispatcherSignal::signal (SIGINT, [&]{bases.exit ();}); //register CTRL + C signal processor-Exit event distribution Loop TCPServer echo (&bases); //Create server int r = Echo. Bind ( "", 99); //bind port exitif (R,  "Bind failed %d %sstrerror (errno)); Echo. onconnread ([] (const tcpconnptr& con) {con->send (Con->getinput ()); //Echo read data}); Bases. loop (); //into event distribution loop}              

Where Eventbase is an event dispatcher that internally uses Epoll/kqueue for IO event distribution.

The Eventbase feature is rich and includes functions such as timed tasks.

In network programming, it is more difficult to process requests completely asynchronously, especially the business logic, involving the use of database. The most common model used by everyone is to process IO asynchronously, ensure large concurrency, and use multithreading to process business requests, simplifying the writing of business logic. This semi-synchronous, semi-asynchronous programming model is referred to as Hsha (half sync half async).

Handy can support Hsha, here is a complete server example:

#include 
using namespace Std;
using namespace handy;  
int main (int argc, const char* argv[]) {
Eventbase Base;
Hsha Hsha (&base, 4); Start 4 Threads for synchronous processing
int r = Hsha.bind ("", 99);
Exitif (R, "Bind failed");
Register the signal processing of CTRL + C
Signal::signal (SIGINT, [&]{base.exit (); Hsha.exit (); Signal (SIGINT, SIG_DFL);});
Message-handling functions
hsha.onmsg (New Linecodec, [] (const tcpconnptr& con, const string& input) {
int ms = rand ()% 1000;
info ("Processing a msg");
Usleep (MS * 1000);
return Util::format ("%s used%d MS", INPUT.C_STR (), MS);
    });
Base.loop ();
}

Where onmsg registers the message handler function, the first parameter of ONMSG is the message decoder, which decodes the input byte stream of the TCP connection into a message, and calls the second CB parameter for each full message onmsg incoming.

The CB parameter prototype is a string CB (const tcpconnptr& con, const string& input), the user only need to write this CB function, processing input, return processing results. The CB function is called in the thread pool, so operations such as sleep in the processing function do not clog network IO. In the above example, the user can use Telnet to log on to the Hsha example server, send a message to the server, the server side of the log can be found to output ' processing a msg ' thread is not an IO thread.

If the user needs more flexibility in handling, you can return an empty string indicating that it has not been processed and can directly manipulate the con connection.

Handy also has more features, such as timed processing, cleaning up idle connections, and more, see Https://github.com/yedf/handy

Server-side programming using the C++11 network library handy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.