C + + Poco library Chinese Programming Reference Guide (11) How to use the reactor framework?

Source: Internet
Author: User
Tags socket tostring

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

Related Article

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.