1 Reactor Framework Overview
The reactor framework in POCO is designed based on the reactor design pattern. In which the event generated by a Socket is sent by Handler to the method of the specified object, as a callback.
2 Light said not practice fake
Poechantreactorserver class, Basic and Poechanttcpserver:
Class Poechantreactorserver:public serverapplication
{public
:
poechantserver () {}//: _helprequested ( False) {}
~poechantserver () {}
protected:
void Initialize (application& self)
{
Loadconfiguration ();
Serverapplication::initialize (self);
}
void Uninitialize ()
{
serverapplication::uninitialize ();
}
int main (const std::vector<std::string>& args)
{
//...
Return APPLICATION::EXIT_OK
}
}
The Poechantservicehandler class is defined as follows. The crane brought onreadable and OnShutdown's voice into great trouble.
Class Poechantservicehandler
{public
:
Poechantservicehandler (streamsocket& socket, socketreactor & reactor);
~poechantservicehandler ()
; void onreadable (const autoptr<readablenotification>& pNf);
void OnShutdown (const autoptr<shutdownnotification>& pNf);
Private:
enum
{
buffer_size = 1024
};
Streamsocket _socket;
socketreactor& _reactor;
char *_pbuffer;
} ;
Poechantservicehandler implementation:
Poechantservicehandler::P Oechantservicehandler (streamsocket& socket, socketreactor& reactor): _socket (sock ET), _reactor (reactor), _pbuffer (new Char[buffer_size]) {application& app = Application::in
Stance ();
App.logger (). Information ("Connection from" + socket.peeraddress (). toString ()); _reactor.addeventhandler (_socket, Nobserver<poechantservicehandler, readablenotification> (
*this, &poechantservicehandler::onreadable)); _reactor.addeventhandler (_socket, Nobserver<poechantservicehandler, shutdownnotification> (
*this, &poechantservicehandler::onshutdown));
} ~poechantservicehandler () {application& app = Application::instance ();
App.logger (). Information ("Disconnecting" + _socket.peeraddress (). toString ()); _reactor.removeeventhandler (_socket, Nobserver<poechantservicehandler, readablenotificAtion> (*this, &poechantservicehandler::onreadable)); _reactor.removeeventhandler (_socket, Nobserver<poechantservicehandler, SHUTDOWNNOTIFICATION&G
t; (*this, &poechantservicehandler::onshutdown));
delete [] _pbuffer;
} void Onreadable (const autoptr<readablenotification>& pNf) {//Receive data from Streamsocket
int n = _socket.receivebytes (_pbuffer, buffer_size);
Send data back the client if (n > 0) _socket.sendbytes (_pbuffer, N);
else delete this;
This method would be invoked if shutdownnotification is detected.
void OnShutdown (const autoptr<shutdownnotification>& pNf) {Delete this; }
Start:
int main (const std::vector<std::string>& args)
{
unsigned short port = (unsigned short) config (). GetInt ("Poechantreactor.port", 12345);
ServerSocket ServerSocket (port);
Socketreactor reactor;
Socketacceptor<poechantservicehandler> acceptor (ServerSocket, reactor);
Reactor.run ();
Waitforterminationrequest ();
Reactor.stop ();
return APPLICATION::EXIT_OK;
}
int main (int argc, char **argv)
{return
poechantserver (). Run (argc, argv);
}
3 Clinet Test Code
How do I use the TCPServer framework with the POCO Library Chinese Programming Reference Guide (10)? The Client test case in ".
CSDN blog from Liuda: blog.csdn.net/poechant
View a full set of articles: Http://www.bianceng.cn/Programming/cplus/201301/35022.htm