我目前在使用 Poco Net & Reactor Pattern 的服务器上工作。我想做类 CSConnection unique_ptr 因为类对象被引用到工作线程池。
构造函数:
CSConnection::CSConnection(StreamSocket& socket, SocketReactor& reactor) : _socket(socket), _reactor(reactor)
{
std::unique_ptr<CSConnection> autoptr(this);
app.logger().information("Connection : " + _socket.peerAddress().toString());
_reactor.addEventHandler(_socket, NObserver<CSConnection, ReadableNotification>(*this, &CSConnection::onReadable));
_reactor.addEventHandler(_socket, NObserver<CSConnection, ShutdownNotification>(*this, &CSConnection::onShutdown));
_reactor.addEventHandler(_socket, NObserver<CSConnection, ErrorNotification>(*this, &CSConnection::onError));
_socket.setBlocking(false);
sendSync();
}
析构函数:
CSConnection::~CSConnection()
{
app.logger().information("Disconnect : " + _socket.peerAddress().toString());
_reactor.removeEventHandler(_socket, NObserver<CSConnection, ReadableNotification>(*this, &CSConnection::onReadable));
_reactor.removeEventHandler(_socket, NObserver<CSConnection, ShutdownNotification>(*this, &CSConnection::onShutdown));
_reactor.removeEventHandler(_socket, NObserver<CSConnection, ErrorNotification>(*this, &CSConnection::onError));
if(player)
{
player->relase();
if(player->hasActiveCharacter())
{
player->getActiveCharacter()->leaveGameWorld();
player->nullActive();
}
}
}
并且在该应用程序立即释放该类之后(可以通过服务器日志中的“断开连接”来观察)。
有什么问题?谢谢。