0. Some reference materials
(reference 1) a basic introduction to thrift can be found in:
Http://wenku.baidu.com/link?url= Lll5h3ql4hj3o6dfq0sbgztqtxyfr5vdyftwowknrmwiiq3t87mcu-gmzljxczvryxxhqna1hm4eu3f7aycmlc7ffy7ywl18iil6ny7jkca
(reference 2) Thrift IDL definitions can be referenced (that is, methods for defining structured data and services):
http://diwakergupta.github.io/thrift-missing-guide/#_defining_services
1. Basic Use Method
Tnonblockingserver is a multi-threaded non-blocking IO service provided by thrift.
The reason to read the code from here is because Tserver (Tnonblockingserver is a subclass of tserver implementation) is the most closely related to the user implementation part of the framework, the Division of the sections refer to (reference 1) ha.
First look at the basic use:
1 //fooserving is our own definition of thrift service2 //Fooservingprocessor is a class that is automatically generated by thrift3 //Fooservinghandler is our business implementation class, and 90% of the code is written in this class4Shared_ptr<fooservinghandler>5HandlerNewFooservinghandler ());6Shared_ptr<tprocessor>7ProcessorNewFooservingprocessor (handler));8 9Shared_ptr<tprotocolfactory>TenProtocolfactory (Newtbinaryprotocolfactory ()); One AShared_ptr<posixthreadfactory> Threadfactory (Newposixthreadfactory ()); -Shared_ptr<threadmanager> Threadmanager = - Threadmanager::newsimplethreadmanager (thread_count); theThreadmanager->threadfactory (threadfactory); -Threadmanager->start (); - - Tnonblockingserver Server (processor, + Protocolfactory, - Port, + Threadmanager); A Server.settaskexpiretime (expire_time); atServer.serve ();
Of course, there are many implementations of Tnonblockingserver's constructors, not confined to the above-mentioned methods. Handler is actually the business logic we implement, Processer is the processing class that is automatically generated based on our defined service (IDL file), it calls our handler, and in the server receives the client request and forwards it to processer processing.
2.serve function
The following from Tnonblockingserver's serve function, continue to see.
1 voidTnonblockingserver::serve () {2 3 if(Iothreads_.empty ())4 registerevents (NULL); Initialize listening ports, create Iothreads_, initialize Iothreadfactory_, initialize Eventhandler_, etc.5 6 //Run the primary (listener) IO thread loop in our main thread;7 //Only return if the server is shutting down.8iothreads_[0]->run (); This is the control thread9 Ten //ensure all threads is finished before exiting serve () One for(Uint32_t i =0; I < iothreads_.size (); ++i) { AIothreads_[i]->join (); -globaloutput.printf ("tnonblocking:join do for IO thread #%d", i); - } the}
The first thread in Iothreads_ is the control thread
Not to be continued ... Let's go home for New year.
Thrift0.9.3 C + + code reading essay--tnonblockingserver