POCO库中文编程参考指南(11)如何使用Reactor框架?
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库中文编程参考指南(8)丰富的Socket编程
POCO库中文编程参考指南(8)丰富的Socket编程 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.com (# ...
- POCO库中文编程参考指南(4)Poco::Net::IPAddress
POCO库中文编程参考指南(4)Poco::Net::IPAddress 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmai ...
- POCO库中文编程参考指南(3)Poco::Net::Socket
POCO库中文编程参考指南(3)Poco::Net::Socket 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.c ...
- POCO库中文编程参考指南(2)基本数据类型(Poco/Types.h)
POCO库中文编程参考指南(2)基本数据类型 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.com (# -> ...
- POCO库中文编程参考指南(1)总览
POCO库中文编程参考指南(1)总览 作者:柳大·Poechant 博客:Blog.CSDN.net/Poechant 邮箱:zhongchao.ustc#gmail.com (# -> @) ...
- POCO库中文编程参考指南(10)如何使用TCPServer框架?
1 TCPServer 框架概述 POCO 库提供TCPServer框架,用以搭建自定义的 TCP 服务器.TCPServer维护一个连接队列.一个连接线程池.连接线程用于处理连接,连接线程只要一空闲 ...
- POCO库中文编程参考指南(5)Poco::Net::SocketAddress
1 枚举 最大地址长度,这个与Poco::Net::IPAddress中的定义可以类比,不过这里指的是`struct sockaddr_in6 enum { MAX_ADDRESS_LENGTH = ...
- POCO库中文编程参考指南(6)Poco::Timestamp
1 类型别名 三个时间戳相关的类型别名,TimeDiff表示两个时间戳的差,第二个是以微秒为单位的时间戳,第三个是以 100 纳秒(0.1 微妙)为单位的时间戳: typedef Int64 Time ...
- POCO库中文编程参考指南(9)Poco::Net::DNS
1 Poco::Net::DNS namespace Poco { namespace Net { class Net_API DNS { public: static HostEntry hostB ...
随机推荐
- 过滤XSS的HTMLPurifier使用
什么是HTMLPurifier? 在php里解决XSS最简单的方法是使用htmlspecialchars转义xml实体,但对于需要使用xml的时候就搏手无策了. HTML Purifier是基于php ...
- 【Scala】Scala的Predef对象
隐式引用(Implicit Import) Scala会自己主动为每一个程序加上几个隐式引用,就像Java程序会自己主动加上java.lang包一样. Scala中.下面三个包的内容会隐式引用到每一个 ...
- 关于mongodb创建索引的一些经验总结(转)
查看语句执行计划: explain() 在mongodb3+版本后输出格式发生改变: 详情参见:https://docs.mongodb.com/v3.0/reference/method/curso ...
- android菜鸟学习笔记4----android项目结构
src: 应用程序源代码存放目录 gen: 自动生成的目录,目录中存放所有由Android开发工具自动生成的文件. 目录中最重要的就是R.java文件. 这个文件由Android开发工具自动产生的.A ...
- 九度OJ 1039:Zero-complexity Transposition(逆置) (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3093 解决:1255 题目描述: You are given a sequence of integer numbers. Zero-co ...
- 【题解】P3162CQOI2012组装
[题解][CQOI2012]组装 考虑化为代数的形式,序列\(\left[a_i \right]\)表示选取的\(i\)种类仓库的坐标. \(ans=\Sigma(a_i-x)^2,(*)\),展开: ...
- 一起来学linux:SAMBA服务器搭建
前面介绍的NFS服务器的用来linux和linux系统之间共享文件和目录的,那如果是linux和windows之间需要共享修改文件该如何操作呢.这据需要用到SAMBA系统.我们首先来看下SAMBA系统 ...
- crm高速开发之EntityCollection
/* 创建者:菜刀居士的博客 * 创建日期:2014年07月07号 */ namespace Net.CRM.OrganizationService { using System; ...
- 关于SAP的编码范围
[转自 http://blog.sina.com.cn/s/blog_6466e5f70100ithw.html ] 1.Number Range的通用Tcode:SNRO 2.Number Rang ...
- angularJs 购物车模型
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel= ...