asio中socket的打开

关系

basic_socket_acceptor<Protocol, Service>
basic_stream_socket<Protocol, Service>
basic_raw_socket<Protocol, Service>
basic_seq_packet_socket<Protocol, Service>
socket_acceptor_service<Protocol>
stream_socket_service<Protocol>
raw_socket_service<Protocol>
seq_packet_socket_service<Protocol>
win_iocp_socket_service<Protocol>
reactive_socket_service<Protocol>

reactive_socket_service打开

其是创建socket,并且将socket注册到reactor中。其在操作socket时使用了socket_holder来管理fd

  // Open a new socket implementation.
  boost::system::error_code open(implementation_type& impl,
      const protocol_type& protocol, boost::system::error_code& ec)
  {
    if (!do_open(impl, protocol.family(),
          protocol.type(), protocol.protocol(), ec))
      impl.protocol_ = protocol;
    return ec;
  }

boost::system::error_code reactive_socket_service_base::do_open(
    reactive_socket_service_base::base_implementation_type& impl,
    int af, int type, int protocol, boost::system::error_code& ec)
{
  if (is_open(impl))
  {
    ec = boost::asio::error::already_open;
    return ec;
  }

  socket_holder sock(socket_ops::socket(af, type, protocol, ec));
  if (sock.get() == invalid_socket)
    return ec;

  if (int err = reactor_.register_descriptor(sock.get(), impl.reactor_data_))
  {
    ec = boost::system::error_code(err,
        boost::asio::error::get_system_category());
    return ec;
  }

  impl.socket_ = sock.release();
  switch (type)
  {
  case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
  case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
  default: impl.state_ = 0; break;
  }
  ec = boost::system::error_code();
  return ec;
}

win_iocp_socket_servicer打开

其是创建socket,并且将socket添加到iocp中。其在操作socket时使用了socket_holder来管理fd

  boost::system::error_code open(implementation_type& impl,
      const protocol_type& protocol, boost::system::error_code& ec)
  {
    if (!do_open(impl, protocol.family(),
          protocol.type(), protocol.protocol(), ec))
    {
      impl.protocol_ = protocol;
      impl.have_remote_endpoint_ = false;
      impl.remote_endpoint_ = endpoint_type();
    }
    return ec;
  }

boost::system::error_code win_iocp_socket_service_base::do_open(
    win_iocp_socket_service_base::base_implementation_type& impl,
    int family, int type, int protocol, boost::system::error_code& ec)
{
  if (is_open(impl))
  {
    ec = boost::asio::error::already_open;
    return ec;
  }

  socket_holder sock(socket_ops::socket(family, type, protocol, ec));
  if (sock.get() == invalid_socket)
    return ec;

  HANDLE sock_as_handle = reinterpret_cast<HANDLE>(sock.get());
  if (iocp_service_.register_handle(sock_as_handle, ec))
    return ec;

  impl.socket_ = sock.release();
  switch (type)
  {
  case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
  case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
  default: impl.state_ = 0; break;
  }
  impl.cancel_token_.reset(static_cast<void*>(0), socket_ops::noop_deleter());
  ec = boost::system::error_code();
  return ec;
}

socket_acceptor_service,stream_socket_service ,raw_socket_service和seq_packet_socket_service 打开

这四个服务的打开依赖于win_iocp_socket_servicer和reactive_socket_service,其直接调用win_iocp_socket_service或者reactive_socket_service的open方法

//socket_acceptor_service
  boost::system::error_code open(implementation_type& impl,
      const protocol_type& protocol, boost::system::error_code& ec)
  {
    return service_impl_.open(impl, protocol, ec);
  }
  //stream_socket_service 
   boost::system::error_code open(implementation_type& impl,
      const protocol_type& protocol, boost::system::error_code& ec)
  {
    if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_STREAM))
      service_impl_.open(impl, protocol, ec);
    else
      ec = boost::asio::error::invalid_argument;
    return ec;
  }
  //raw_socket_service
  boost::system::error_code open(implementation_type& impl,
      const protocol_type& protocol, boost::system::error_code& ec)
  {
    if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_RAW))
      service_impl_.open(impl, protocol, ec);
    else
      ec = boost::asio::error::invalid_argument;
    return ec;
  }
  //seq_packet_socket_service 
  boost::system::error_code open(implementation_type& impl,
      const protocol_type& protocol, boost::system::error_code& ec)
  {
    if (protocol.type() == BOOST_ASIO_OS_DEF(SOCK_SEQPACKET))
      service_impl_.open(impl, protocol, ec);
    else
      ec = boost::asio::error::invalid_argument;
    return ec;
  }

socket层打开

分为间接打开也就是指定对端信息时,直接打开就是直接调用服务层的open。basic_socket_acceptor basic_socket_acceptor(boost::asio::io_service& io_service, const endpoint_type& endpoint, bool reuse_addr = true) : basic_io_object<SocketAcceptorService>(io_service)其除了会调用open外,还会调用bind和listen方法。basic_stream_socketbasic_stream_socket(boost::asio::io_service& io_service, const endpoint_type& endpoint)调用basic_socket的构造函数,会调用open,connect

相关推荐

  1. asiosocket打开

    2024-04-07 05:48:03       68 阅读
  2. boost::asio::ip::tcp::socket

    2024-04-07 05:48:03       37 阅读
  3. C++socket编程常用接口

    2024-04-07 05:48:03       20 阅读
  4. Qt和Boost::asioemit冲突

    2024-04-07 05:48:03       40 阅读
  5. Axios 不同 responseType 选项

    2024-04-07 05:48:03       49 阅读
  6. Socket Shutdown

    2024-04-07 05:48:03       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-07 05:48:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-07 05:48:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-07 05:48:03       87 阅读
  4. Python语言-面向对象

    2024-04-07 05:48:03       96 阅读

热门阅读

  1. 计算机网络(目录)

    2024-04-07 05:48:03       41 阅读
  2. OJ题目分享2

    2024-04-07 05:48:03       83 阅读
  3. 彩虹易支付搭建教程

    2024-04-07 05:48:03       39 阅读
  4. .NET9 PreView2+.AOT ILC 的重大变化

    2024-04-07 05:48:03       175 阅读
  5. 排序算法-堆排序

    2024-04-07 05:48:03       38 阅读
  6. Nginx配置使用笔记

    2024-04-07 05:48:03       46 阅读
  7. nuxt3使用记录一:框架摸索

    2024-04-07 05:48:03       45 阅读
  8. 开发语言漫谈-C++

    2024-04-07 05:48:03       121 阅读
  9. 【Python面向对象编程】

    2024-04-07 05:48:03       40 阅读
  10. 深入理解Transformer架构的编码器-解码器结构

    2024-04-07 05:48:03       46 阅读