The previous network library was marked with an ID as the session object, but many application server logic is complex and each session object needs to carry (save) a lot of state, then the developer will always build an object manager.
So I was previously encapsulated in a network library as a C + + object, as a callback to the net event and the parameters of the network interface.
Shown here is a pingpong protocol test program (not subcontracting, subcontracting does not affect the efficiency of this Protocol test)
Project (Requires VS2013): Http://files.cnblogs.com/files/irons/PingPangDemo.zip
The Tcpservice class is the network interface layer that is designed to be labeled with an ID object.
The Wrapserver is a network interface labeled Tcpservice with C + + objects, and its C + + object is tcpsession.
It usually uses its smart pointer.
For the blog Garden Rules (in fact, is dine), only paste code (specific code design, but also please take a moment to see the source code-I do not do more here to explain the HA, there are questions please advise)
Server:
#include <iostream>#include<mutex>#include".. /net/eventloop.h"#include".. /net/wraptcpservice.h"Std::mutex G_mutex;intTOTAL_RECV =0;intTotal_client_num =0;voidonsessionclose (tcpsession::P tr session) {G_mutex.Lock(); Total_client_num--; G_mutex.unlock ();}intOnsessionmsg (tcpsession::P TR Session,Const Char* Buffer,intLen) {Session-Send (buffer, Len); G_mutex.Lock(); Total_recv+=Len; G_mutex.unlock (); returnLen;}intMainintargcChar**argv) { intThread_num = Atoi (argv[1]); intPort_num = Atoi (argv[2]); Wrapserver::P TR Server= std::make_shared<wrapserver>(); Server-Setdefaultentercallback ([] (tcpsession::P tr session) {session-Setclosecallback (onsessionclose); Session-Setdatacallback (onsessionmsg); G_mutex.Lock(); Total_client_num++; G_mutex.unlock (); }); Server-Startlisten (Port_num); Server-Startworkthread (Thread_num); EventLoop Mainloop; Mainloop.restorethreadid (); while(true) {Mainloop.loop ( +); G_mutex.Lock(); Std::cout<<"Total recv:"<< (TOTAL_RECV/1024x768) /1024x768<<"m/s, of client num:"<< Total_client_num <<Std::endl; Total_recv=0; G_mutex.unlock (); }}
Client:
#include <iostream>#include<string>#include".. /net/socketlibfunction.h"#include".. /net/wraptcpservice.h"voidonsessionclose (tcpsession::P tr session) {}intOnsessionmsg (tcpsession::P TR Session,Const Char* Buffer,intLen) {Session-Send (buffer, Len); returnLen;}intMainintargcChar**argv) { intThread_num = Atoi (argv[1]); intPort_num = Atoi (argv[2]); intnum = atoi (argv[3]); intPacket_len = Atoi (argv[4]); STD::stringTMP (Packet_len,'a'); Wrapserver::P TR Server= std::make_shared<wrapserver>(); Server-Startworkthread (Thread_num); for(inti =0; i < num; i++) {sock FD= Ox_socket_connect ("127.0.0.1", Port_num); Server->addsession (FD, [&] (tcpsession::P tr session) {session-Setclosecallback (onsessionclose); Session-Setdatacallback (onsessionmsg); Session-Send (Tmp.c_str (), tmp.size ()); }); } std::cin.Get();}
Tks
Refactoring Network libraries