Poco-自定义SocketAcceptor

时间:2018-08-16 08:25:54

标签: c++ templates poco-libraries

我需要拥有自己的SocketAcceptor,并且必须执行以下操作:

class OwnServiceHandler
{
public:
    OwnServiceHandler(StreamSocket& socket, SocketReactor& reactor,void * data):
        _socket(socket),
        _reactor(reactor),
        _data(data) {}

private:
    StreamSocket   _socket;
    SocketReactor& _reactor;
    void * _data;
};

class OwnSocketAcceptor : public SocketAcceptor<OwnServiceHandler> {
  public:
    OwnSocketAcceptor (ServerSocket& socket, SocketReactor& reactor,void * data)
    : SocketAcceptor(socket,reactor), _data(data){};

  protected:

    virtual OwnServiceHandler* createServiceHandler(StreamSocket& socket) override
    {
      return new OwnServiceHandler(socket, *reactor(),_data);
    }

  private:
    void * _data;
};

但是当我编译时,出现以下错误:

SocketAcceptor.h: In instantiation of 'ServiceHandler* Poco::Net::SocketAcceptor<ServiceHandler>::createServiceHandler(Poco::Net::StreamSocket&) [with ServiceHandler = OwnServiceHandler]':
SocketAcceptor.h:156:23:   required from 'void Poco::Net::SocketAcceptor<ServiceHandler>::onAccept(Poco::Net::ReadableNotification*) [with ServiceHandler = OwnServiceHandler]'
SocketAcceptor.h:85:33:   required from 'Poco::Net::SocketAcceptor<ServiceHandler>::SocketAcceptor(Poco::Net::ServerSocket&, Poco::Net::SocketReactor&) [with ServiceHandler = OwnServiceHandler]'
error: no matching function for call to 'OwnServiceHandler::OwnServiceHandler(Poco::Net::StreamSocket&, Poco::Net::SocketReactor&)'

我在以下位置指向SocketAcceptor.h文件:

    void onAccept(ReadableNotification* pNotification)
        /// Accepts connection and creates event handler.
    {
        pNotification->release();
        StreamSocket sock = _socket.acceptConnection();
        _pReactor->wakeUp();
        /* the fuction call in the Poco header */ createServiceHandler(sock);
    }

protected:
    virtual ServiceHandler* createServiceHandler(StreamSocket& socket)
        /// Create and initialize a new ServiceHandler instance.
        ///
        /// Subclasses can override this method.
    {
      /* Error --> */ return new ServiceHandler(socket, *_pReactor);
    }

编译器没有用我自己的函数覆盖虚拟函数。当我破解源代码并使函数成为纯虚拟

virtual ServiceHandler* createServiceHandler(StreamSocket& socket) = 0;

一切都很好,我的功能将被使用。那我的错是什么? 我不认为这与Poco有关。

编辑:

我尝试过这种结构,应该与上面的相同

class Helper {
  public:
  Helper(){};
};
template<class t> class TestBase {
  public:
  TestBase(){}
  void doIt(){
    test();
  }
  virtual t* test(){
    return new t();
  }
};

class Test : public TestBase<Helper> {
  public:
  Test() : TestBase() {}
  virtual Helper* test(){
    return new Helper();
  }
};

有效!很奇怪...

最诚挚的问候

0 个答案:

没有答案