void initialize(shared_ptr<options> const& opts, F const& f, Id const& id) { opts_ = opts; parser_.initialize(opts_->mtu, BEX_IO_BIND(&tcp_packet_protocol::on_parse, this, _1, _2, _3, _4)); if (f) global_receiver_ = BEX_IO_BIND(f, id, _1, _2, _3, _4); }
// 握手 void async_handshake(socket_ptr const& sp, endpoint const& addr) { async_handshaking_.set(); auto handler = BEX_IO_BIND(&this_type::on_async_handshake, this, BEX_IO_PH_ERROR, sp, addr); if (opts_->ssl_opts) { auto timed_handler = timer_handler<allocator>(handler, ios_); timed_handler.expires_from_now(boost::posix_time::milliseconds(opts_->ssl_opts->handshake_overtime)); timed_handler.async_wait(BEX_IO_BIND(&this_type::on_async_handshake, this, make_error_code(errc::handshake_overtime), sp, addr)); protocol_traits_type::async_handshake(sp, ssl::stream_base::client, timed_handler); } else protocol_traits_type::async_handshake(sp, ssl::stream_base::client, handler); }
// 带超时的异步连接 bool async_connect_timed(endpoint const& addr, boost::posix_time::time_duration time) { if (is_running() || !async_connecting_.set()) return false; socket_ptr sp = protocol_type::alloc_socket(ios_, *opts_, ec_); if (ec_) return false; /// 连接超时计时器, 异步等待 auto timed_handler = timer_handler<allocator>(BEX_IO_BIND(&this_type::on_async_connect_timed, this, BEX_IO_PH_ERROR, sp, addr, time), ios_); timed_handler.expires_from_now(time); timed_handler.async_wait(BEX_IO_BIND(&this_type::on_overtime, this, BEX_IO_PH_ERROR, sp, errc::connect_overtime)); sp->lowest_layer().async_connect(addr, timed_handler); // 启动工作线程 mstrand_service_.startup(opts_->workthread_count); return true; }
// 发起接受连接请求 void async_accept(bool reply = false) { if (!reply) { BOOST_INTERLOCKED_INCREMENT(&accept_count_); } socket_ptr sp = protocol_type::alloc_socket(ios_, opts_->receive_buffer_size, opts_->send_buffer_size); acceptor_.async_accept(sp->lowest_layer(), BEX_IO_BIND(&this_type::on_async_accept, this, BEX_IO_PH_ERROR, sp)); }
// 异步连接 bool async_connect(endpoint const& addr) { if (is_running() || !async_connecting_.set()) return false; socket_ptr sp = protocol_type::alloc_socket(ios_, *opts_, ec_); if (ec_) return false; sp->lowest_layer().async_connect(addr, BEX_IO_BIND(&this_type::on_async_connect, this, BEX_IO_PH_ERROR , sp, addr)); // 启动工作线程 mstrand_service_.startup(opts_->workthread_count); return true; }
// 接受连接请求回调 void on_async_accept(error_code const& ec, socket_ptr sp) { if (ec) { if (BOOST_INTERLOCKED_DECREMENT(&accept_count_) == 1 && shutdowning_.is_set()) shutdown_sessions(); return ; } async_accept(true); /// create session session_type * session_p = allocate<session_type, allocator>(); session_ptr session(session_p, BEX_IO_BIND(&this_type::session_deleter, this, _1), allocator()); session_p->initialize(sp, opts_, callback_); session_mgr_.insert(session); }
// 握手回调 void on_async_handshake(error_code const& ec, socket_ptr sp, endpoint addr) { async_handshaking_.reset(); if (ec) { ec_ = ec; async_connecting_.reset(); if (on_handshake_error_) mstrand_service_.post(BEX_IO_BIND( on_handshake_error_, ec, sp->lowest_layer().remote_endpoint())); } else { ec_.clear(); running_.set(); async_connecting_.reset(); session_ = make_shared_ptr<session_type, allocator>(); session_->initialize(sp, opts_, callback_); } notify_onconnect(); }
// 通知连接结果 void notify_onconnect() { if (async_connect_callback_) mstrand_service_.post(BEX_IO_BIND(async_connect_callback_, ec_)); }
// 强制地关闭所有连接 void terminate_sessions() { session_mgr_.for_each(BEX_IO_BIND(&session_type::terminate, _1)); }
// 优雅地关闭所有连接 void shutdown_sessions() { session_mgr_.for_each(BEX_IO_BIND(&session_type::shutdown, _1)); }