关系
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