1

我正在编写基于 Poco 框架的服务器应用程序。它使用反应器模式和请求处理程序类是 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;
}
4

0 回答 0