程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++ POCO庫中文編程參考指南(11) 如何使用Reactor框架?

C++ POCO庫中文編程參考指南(11) 如何使用Reactor框架?

編輯:關於C++

1 Reactor 框架概述

POCO 中的 Reactor 框架是基於 Reactor 設計模式進行設計的。其中由 Handler 將某 Socket 產生的事件,發送到指定的對象的方法上,作為回調。

2 光說不練假把式

PoechantReactorServer 類,基本與 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;   
    }   
}

PoechantServiceHandler 類定義如下。起重機把onReadable和onShutdown的聲音帶來很大的麻煩。

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 實現:

PoechantServiceHandler::PoechantServiceHandler(StreamSocket& socket, SocketReactor& reactor)   
    :_socket(socket),   
     _reactor(reactor),   
     _pBuffer(new char[BUFFER_SIZE])   
{   
    Application& app = Application::instance();   
    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>(*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;   
}   

// When ShutdownNotification is detected, this method will be invoked.   
void onShutdown(const AutoPtr<ShutdownNotification>& pNf)   
{   
    delete this;   
}

啟動:

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 測試代碼

同《POCO庫中文編程參考指南(10)如何使用TCPServer框架?》中的 Client 測試用例。

來自柳大的CSDN博客:Blog.CSDN.net/Poechant

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved