奇怪的波科反应堆的通知
Strange poco reactor's notifications
我有实现Poco SocketReactor的代码,如Echo示例中所述。
class TSPConnectionHandler
{
public:
TSPConnectionHandler(const StreamSocket& socket, SocketReactor& reactor) : _socket(socket),
_reactor(reactor)
{
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ReadableNotification>(*this, &TSPConnectionHandler::onSocketReadable));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, WritableNotification>(*this, &TSPConnectionHandler::onSocketWritable));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ShutdownNotification>(*this, &TSPConnectionHandler::onSocketShutdown));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ErrorNotification>(*this, &TSPConnectionHandler::onSocketError));
_reactor.addEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, TimeoutNotification>(*this, &TSPConnectionHandler::onSocketTimeout));
}
virtual ~TSPConnectionHandler()
{
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ReadableNotification>(*this, &TSPConnectionHandler::onSocketReadable));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, WritableNotification>(*this, &TSPConnectionHandler::onSocketWritable));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ShutdownNotification>(*this, &TSPConnectionHandler::onSocketShutdown));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, ErrorNotification>(*this, &TSPConnectionHandler::onSocketError));
_reactor.removeEventHandler(_socket, Poco::NObserver<TSPConnectionHandler, TimeoutNotification>(*this, &TSPConnectionHandler::onSocketTimeout));
}
void onSocketReadable(const Poco::AutoPtr<ReadableNotification>& pNf)
{
cout << "READable !!" << endl;
try
{
vector<char> m_buffer;
m_buffer.resize(1024, ' ');
LONG32 m_buflen = _socket.receiveBytes(&m_buffer[0], 1024);
if (m_buflen == 0)
{
cout << "Connection reset by peer normally" << endl;
delete this;
}
_socket.sendBytes(&m_buffer[0], m_buflen);
}
catch(Poco::Net::NetException& e)
{
cout << "socket read exception: " << e.displayText() << endl;
delete this;
}
}
void onSocketWritable(const Poco::AutoPtr<WritableNotification>& pNf)
{
cout << "WRITEable !!" << endl;
}
void onSocketShutdown(const Poco::AutoPtr<ShutdownNotification>& pNf)
{
cout << "SHUTDOWN!!!!!!!!!!!!" << endl;
delete(this);
}
void onSocketError(const Poco::AutoPtr<ErrorNotification>& pNf)
{
cout << "Error!!" << endl;
}
void onSocketTimeout(const Poco::AutoPtr<TimeoutNotification>& pNf)
{
cout << "Timeout!!" << endl;
}
private:
StreamSocket _socket;
SocketReactor& _reactor;
};
它使用以下代码在程序的其他地方正常启动:
Poco::Net::ServerSocket tcpsock("9495");
Poco::Net::SocketReactor reactor;
Poco::Net::SocketAcceptor<TSPConnectionHandler> acceptor(tcpsock, reactor);
Poco::Thread thread;
thread.start(reactor);
waitForTerminationRequest();
reactor.stop();
thread.join();
return Application::EXIT_OK;
我还有python客户端来测试它:
import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 9495))
data = raw_input ( "Something:" )
client_socket.send(data)
data = client_socket.recv(1024)
print "RECIEVED:" , data
client_socket.close()
我收到了一份我不知道如何解决的问题清单:
- python字符串"client_socket.connect(('localhost',9495))"完成后,服务器开始垃圾邮件"onSocketWritable",如何阻止它?我知道如果套接字变为可写的,我必须得到通知,但为什么它在套接字的整个生命周期中都继续这样做呢?如果这是正常情况,那么设计WritableNotification是为了什么
- 如果我将在调试模式下启动python,并在"client_socket.close()"之前关闭它,则服务器将获得异常并将被删除,抛出"socket-read-exception:"。但不会发送错误通知和关闭通知。为什么?我从来没有见过他们被派去
- 如果我让"client_socket.close()"完成,在服务器端我将收到ReadableNotification,并将抛出"连接正常重置"。但仍然不会有关闭通知。为什么
-
一旦您连接到服务器,就会实例化一个套接字,如果您注册了一个可写的通知,您将收到有关此状态的通知。这意味着只有在必须发送数据时才添加此事件处理程序。套接字将发送数据,完成后,将再次调用此处理程序(异步套接字…)。如果不必为此事件发送更多块的调用removeEventHandler。
-
套接字反应器使用select命令(socket::select(可读,可写,except,_timeout))名称错误通知有点误导(会得到一些错误条件,但也会得到越界数据)。如果套接字正常关闭,Readable将在没有可用字符的情况下调用,否则将引发Poco::Net::ConnectionResetException。
-
当SocketReactor停止时,会发送关闭通知。
你应该看看SocketRactor.cpp,然后一切都会更清楚。
相关文章:
- 函数如何通知用户它基于函数原型抛出异常?
- 如何在ECS框架中更新组件数据和通知系统
- 当 I2C 值在C++中发生变化时收到通知
- 如何设计具有不同类型的通知和观察器的观察者模式?
- 当可输入框在窗口中处于活动状态时获得通知的任何方法
- 是否有必要获取锁并在不需要唤醒线程时通知condition_variable?
- 如何在点击通知时捕获用户的点击操作
- 在通知提升间处理条件变量时未按住锁会导致问题
- 通知条件变量后使用互斥锁
- 如何在不设置值的情况下通知 c++ 未来?
- 使用 ISdigit 通知任何检测到的非法字符
- Qt:如何通知对象已建立涉及它的信号槽连接
- 提升进程间named_condition_any不通知
- Windows Toast 通知不适用于 MS CPP 示例代码
- C++ Poco - 如何创建通知队列的向量?
- 当通知迭代器参数初始化为空列表的开头时,list::insert 行为是什么?
- C++ Windows 通知:使用依赖模板名称
- C++ Poco - 如何向特定线程发送通知/消息?
- 通知 Windows 系统应用程序字体已被卸载/删除
- 奇怪的波科反应堆的通知