自杀是否正确的方式来释放请求处理程序类?

时间:2013-11-10 08:10:14

标签: c++ class threadpool poco

我正在编写基于Poco框架的服务器应用程序。 它使用reactor模式和请求处理程序类是CSConnection。 我从模板(自杀)做了类结构。 现在我想问一下,这种方法是否可以将它与线程池一起使用?

班级:

class CSConnection
{
    public:
        CSConnection(StreamSocket& socket, SocketReactor& reactor);
        ~CSConnection();

        Player & getPlayer();

        bool isLogged();

        void sendBlocking(Packet * p);
        void sendNonblocking(Packet * p);

        void onReadable(const AutoPtr<ReadableNotification>& pNf);

        void onShutdown(const AutoPtr<ShutdownNotification>& pNf);

        void onError(const AutoPtr<ErrorNotification>& pNf);

        StreamSocket & getSocket()
        {
            return _socket;
        }

        void shutdownConnection();
    private:
        void sendSync();
        StreamSocket _socket;
        SocketReactor& _reactor;

        Player player = Player(_socket);
        Application & app = Application::instance();
};

自杀:

void CSConnection::onShutdown(const AutoPtr<ShutdownNotification>& pNf)
{
    delete this;
}

void CSConnection::onReadable(const AutoPtr<ReadableNotification>& pNf)
{
    try
    {
        char * buffer = new char[128](); //allocate incoming packet memory
        int n = _socket.receiveBytes(buffer, 128);
        if(n > 7)
        {
            Worker::getInstance().start(*new LogicHandler(_socket, player, buffer));
        }
        else
        {
            delete this;
            delete buffer;
        }
    }
    catch(Poco::Exception& exc)
    {
        delete this;
        //app.logger().log(exc);
    }
}

void CSConnection::onError(const AutoPtr<ErrorNotification>& pNf)
{
    _socket.shutdownReceive();
    delete this;
}

void CSConnection::shutdownConnection()
{
    _socket.shutdownReceive();
    delete this;
}

0 个答案:

没有答案