POCO库中文编程参考指南(11)如何使用Reactor框架?
2012-10-26 16:30
316 查看
转载:http://blog.csdn.net/poechant/article/details/7485760
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 测试用例。
相关文章推荐
- POCO库中文编程参考指南(11)如何使用Reactor框架?
- POCO库中文编程参考指南(11)如何使用Reactor框架?
- POCO库中文编程参考指南(11)如何使用Reactor框架?
- 如何让Linux下的Java程序使用中文
- 如何使用PHP中ci框架实现验证码?
- QT编程中如何给按钮等控件加载图标以及QT编程中使用中文的问题
- laravel框架中前端如何使用csrf
- 如何在 Domino 上使用 Ajax 框架 Dojo
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- Android 框架学习1:EventBus 3.0 的特点与如何使用
- Django框架如何使用ajax的post方法
- ADO.NET Entity Framework如何:使用实体数据模型向导(实体框架)
- 如何使用开源技术产品、框架?
- apache shiro框架如何使用nginx负载
- 如何使用IEEE_cs_Latex写论文, 并加入中文
- 如何在sublime中使用中文
- 如何使用三层框架开发 JDBC 连接 Mysql 数据库
- Tiles框架如何使用介绍
- javaweb动态网站-----如何在javaweb项目中使用bootstrap框架
- 如何在页面上同时使用 jQuery 和其他框架?