Boost Asio总结(15)class basic_stream_socket

1.

. 多种构造函数重载
一个参数:I/O服务
两个参数:1.I/O服务和协议;2.I/O服务和socket端点

. send()/receive()和write_some()/read_some()区别
  相同:
    功能完全相同;写数据和读数据。
  不同:
    send()/receive()有多种重载

. 异步读写函数

1.1

template <typename Protocol, typename Executor>
class basic_stream_socket
  : public basic_socket<Protocol, Executor>
{
    
    
public:
  /// The type of the executor associated with the object.
  typedef Executor executor_type;

    //构造函数
  explicit basic_stream_socket(const executor_type& ex)
    : basic_socket<Protocol, Executor>(ex)
  {
    
    
  }

  template <typename ExecutionContext>
  explicit basic_stream_socket(ExecutionContext& context,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)
    : basic_socket<Protocol, Executor>(context)
  {
    
    
  }

 basic_stream_socket(const executor_type& ex, const protocol_type& protocol)
    : basic_socket<Protocol, Executor>(ex, protocol)
  {
    
    
  }

  template <typename ExecutionContext>
  basic_stream_socket(ExecutionContext& context, const protocol_type& protocol,
      typename enable_if<
        is_convertible<ExecutionContext&, execution_context&>::value
      >::type* = 0)
    : basic_socket<Protocol, Executor>(context, protocol)
  {
    
    
  }

  basic_stream_socket(const executor_type& ex, const endpoint_type& endpoint)
    : basic_socket<Protocol, Executor>(ex, endpoint)
  {
    
    
  }



//    send
  template <typename ConstBufferSequence>
  std::size_t send(const ConstBufferSequence& buffers)
  {
    
    
    boost::system::error_code ec;
    std::size_t s = this->impl_.get_service().send(
        this->impl_.get_implementation(), buffers, 0, ec);
    boost::asio::detail::throw_error(ec, "send");
    return s;
  }

  template <typename ConstBufferSequence>
  std::size_t send(const ConstBufferSequence& buffers,
      socket_base::message_flags flags)
  {
    
    
    boost::system::error_code ec;
    std::size_t s = this->impl_.get_service().send(
        this->impl_.get_implementation(), buffers, flags, ec);
    boost::asio::detail::throw_error(ec, "send");
    return s;
  }

  template <typename ConstBufferSequence>
  std::size_t send(const ConstBufferSequence& buffers,
      socket_base::message_flags flags, boost::system::error_code& ec)
  {
    
    
    return this->impl_.get_service().send(
        this->impl_.get_implementation(), buffers, flags, ec);
  }



  template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send(const ConstBufferSequence& buffers,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  {
    
    
    return async_initiate<WriteHandler,
      void (boost::system::error_code, std::size_t)>(
        initiate_async_send(), handler, this,
        buffers, socket_base::message_flags(0));
  }

  template <typename ConstBufferSequence, typename WriteHandler>
  BOOST_ASIO_INITFN_RESULT_TYPE(WriteHandler,
      void (boost::system::error_code, std::size_t))
  async_send(const ConstBufferSequence& buffers,
      socket_base::message_flags flags,
      BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  {
    
    
    return async_initiate<WriteHandler,
      void (boost::system::error_code, std::size_t)>(
        initiate_async_send(), handler, this, buffers, flags);
  }


  template <typename MutableBufferSequence>
  std::size_t receive(const MutableBufferSequence& buffers)
  {
    
    
    boost::system::error_code ec;
    std::size_t s = this->impl_.get_service().receive(
        this->impl_.get_implementation(), buffers, 0, ec);
    boost::asio::detail::throw_error(ec, "receive");
    return s;
  }

...
}

猜你喜欢

转载自blog.csdn.net/thefist11cc/article/details/123565484