简介
主要分析linux平台下的,即reactive_socket_service_base和reactive_socket_service
发起
由basic_socket_acceptor调用async_accept,前提是需要调用open创建socket添加到reactor中。其定义为
template <typename SocketService, typename AcceptHandler>
BOOST_ASIO_INITFN_RESULT_TYPE(AcceptHandler,
void (boost::system::error_code))
async_accept(basic_socket<protocol_type, SocketService>& peer,
endpoint_type& peer_endpoint, BOOST_ASIO_MOVE_ARG(AcceptHandler) handler)
{
// If you get an error on the following line it means that your handler does
// not meet the documented type requirements for a AcceptHandler.
BOOST_ASIO_ACCEPT_HANDLER_CHECK(AcceptHandler, handler) type_check;
return this->get_service().async_accept(this->get_implementation(), peer,
&peer_endpoint, BOOST_ASIO_MOVE_CAST(AcceptHandler)(handler));
}
peer:为连接后的socket
peer_endpoint:对端的端口信息
handler:连接成功后的处理器
其底层是调用reactive_socket_service的async_accept
template <typename Socket, typename Handler>
void async_accept(implementation_type& impl, Socket& peer,
endpoint_type* peer_endpoint, Handler& handler)
{
bool is_continuation =
boost_asio_handler_cont_helpers::is_continuation(handler);
// Allocate and construct an operation to wrap the handler.
typedef reactive_socket_accept_op<Socket, Protocol, Handler> op;
typename op::ptr p = {
boost::asio::detail::addressof(handler),
boost_asio_handler_alloc_helpers::allocate(
sizeof(op), handler), 0 };
p.p = new (p.v) op(impl.socket_, impl.state_, peer,
impl.protocol_, peer_endpoint, handler);
BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept"));
start_accept_op(impl, p.p, is_continuation, peer.is_open());
p.v = p.p = 0;
}
首先创建reactive_socket_accept_op,其会动态分配内存handler,如果开启了小内存回收,分配策略是使用thread_info::allocate,否则是使用new
void* asio_handler_allocate(std::size_t size, ...)
{
#if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
# if defined(BOOST_ASIO_HAS_IOCP)
typedef detail::win_iocp_io_service io_service_i

450

被折叠的 条评论
为什么被折叠?



