开发者

boost::asio and boost::bind errors

开发者 https://www.devze.com 2023-03-19 03:00 出处:网络
This questions is a bit annoying, I can\'t get the following code to compile. You will have to compile the code below.

This questions is a bit annoying, I can't get the following code to compile. You will have to compile the code below.

I am having some trouble with boost asio, I am trying to abstract the logic of accepting connections into a uniform abstraction so that I can initiate connection for windows named-pipes and Unix domain sockets uniformly with regular TCP/IP.

There are 3 classes shown in the code below, the first 2 are the implementations of acceping TCP connections, and the third class below is a generic class that is implemented in terms of the first 2. I am having troubles with boost::bind calls. The trouble probably lies with my understanding of the semantics.

If I make TcpDestinationAcceptor::handle_accept a regular member function (--i.e., not a template member function) which results in me not passing the AcceptHandler parameter. The code compiles fine. Note: I do not remove the template function status from TcpDestinationAcceptor::StartAccepting.

Note: I have already started on a different design, still I would like to pursue this design if possible.

Self contained code:

#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>
#include <boost/asio/placeholders.hpp>

class TcpDestinationConnection
{
public:
  typedef boost::asio::ip::tcp::socket t_io_object;
  TcpDestinationConnection(boost::asio::io_service & io_s)
    : m_io_object(io_s) {} ;
  t_io_object & io_object() { return m_io_object; }
private:
  t_io_object m_io_object;
};

class TcpDestinationAcceptor
{
public:
  typedef boost::asio::ip::tcp::acceptor t_acceptor;
  typedef boost::shared_ptr<TcpDestinationConnection> t_connection_ptr;


  TcpDestinationAcceptor( boost::asio::io_service & io_s)
    : m_io_service(io_s),
      m_acceptor(io_s) 
  {
    m_acceptor.open(boost::asio::ip::tcp::v4());
  }

开发者_开发技巧  TcpDestinationAcceptor( boost::asio::io_service & io_s , 
                          const boost::asio::ip::tcp::endpoint & endpoint)
    : m_io_service(io_s),
      m_acceptor(io_s, endpoint) 
    {
      m_acceptor.open(boost::asio::ip::tcp::v4());
    }

  t_acceptor & acceptor() { return m_acceptor; } 

  template<typename AcceptHandler>
  void StartAccepting(AcceptHandler h)
  {   
    t_connection_ptr new_session(new TcpDestinationConnection(m_io_service));

    m_acceptor.async_accept(  new_session->io_object(),
                              boost::bind(  &TcpDestinationAcceptor::handle_accept<AcceptHandler>, this,
                                              boost::asio::placeholders::error, new_session, h));
  }

  template<typename AcceptHandler>
  void handle_accept(const boost::system::error_code & err, t_connection_ptr cur, AcceptHandler h)  {
  }
private:
  boost::asio::io_service & m_io_service;
  boost::asio::ip::tcp::acceptor m_acceptor;
};

template<typename t_acceptor>
class ConnectionOracle
{
public:
  ConnectionOracle()
    : m_io_service(),
      m_acceptor(m_io_service) {}

  typename t_acceptor::t_acceptor & native_acceptor() { return m_acceptor.acceptor(); }
  boost::asio::io_service & io_service() { return m_io_service; }

  void StartConnection( typename t_acceptor::t_connection_ptr connection,
                        boost::system::error_code & error)
  {

  }
  void Begin()
  {
    m_acceptor.StartAccepting(  boost::bind(  &ConnectionOracle::StartConnection,this,
                                              _1,
                                              boost::asio::placeholders::error));
    m_io_service.run();
  } 
private:
  boost::asio::io_service m_io_service;
  t_acceptor m_acceptor;
};

int main()
{
  ConnectionOracle<TcpDestinationAcceptor> ConOracle;
  ConOracle.native_acceptor().
    bind(boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),50000));
  ConOracle.Begin();

  return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消