POCO库中文编程参考指南(11)如何使用Reactor框架?

POCO库中文编程参考指南(11)如何使用Reactor框架?

  • Author: 柳大·Poechant(钟超)
  • Email: zhongchao.ustc#gmail.com (#->@)
  • Blog: Blog.CSDN.net/Poechant
  • Date: April 21th, 2012

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 类定义如下。起重机把onReadableonShutdown的声音带来很大的麻烦。

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

-

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

钟超

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值