inline std::shared_ptr<detail::multiplexer> service::add(const endpoint_type& local_endpoint) { // FIXME: Thread-safety std::shared_ptr<detail::multiplexer> result; auto where = multiplexers.lower_bound(local_endpoint); if ((where == multiplexers.end()) || (multiplexers.key_comp()(local_endpoint, where->first))) { // Multiplexer for local endpoint does not exists result = detail::multiplexer::create(std::ref(get_io_service()), local_endpoint); where = multiplexers.insert(where, multiplexer_map::value_type(local_endpoint, result)); } else { result = where->second.lock(); if (!result) { // This can happen if an acceptor has failed // Reassign if empty result = detail::multiplexer::create(std::ref(get_io_service()), local_endpoint); where->second = result; } } return result; }
CFStringRef copy_hardware_platform() { io_service_t service; CFStringRef platform; char *platformPtr; service = get_io_service("IOPlatformExpertDevice"); if(!service) { printf("unable to find IOPlatformExpertDevice service\n"); return NULL; } platform= IORegistryEntryCreateCFProperty(service, CFSTR("platform-name"), kCFAllocatorDefault, 0); if(platform == NULL) { printf("platform-name not found in device tree\n"); IOObjectRelease(service); return NULL; } platformPtr = calloc(1, CFStringGetLength(platform)+1); if(!CFStringGetCString(platform, platformPtr, CFStringGetLength(platform)+1, kCFStringEncodingUTF8)) { printf("unable to obtain platform-name string\n"); IOObjectRelease(service); return NULL; } printf("platform-name = %s\n", platformPtr); free(platformPtr); return platform; }
bool console_signal_service::deliver_signal() { handler_list_binder::handler_list_owner_ptr handlers = detail::make_shared<handler_list_owner>(); lock_guard lock(mutex_); if (impl_list_.empty()) { return false; } if (!shutdown_) { // Take ownership of all still active handlers. for (impl_base* impl = impl_list_.front(); impl; impl = impl_list_.next(*impl)) { handlers->list.insert_front(impl->handlers_); } if (handlers->list.empty()) { if ((std::numeric_limits<queued_signals_counter>::max)() != queued_signals_) { ++queued_signals_; } } else { get_io_service().post(handler_list_binder(handlers)); } } return true; }
void tracker_connection::fail(error_code const& ec, int code , char const* msg, seconds32 const interval, seconds32 const min_interval) { // we need to post the error to avoid deadlock get_io_service().post(std::bind(&tracker_connection::fail_impl , shared_from_this(), ec, code, std::string(msg), interval, min_interval)); }
void basic_http_connection_impl::__init() { BOOST_ASSERT(connection_); sending_=false; is_recv_blocked_=true; is_header_recvd_=false; recv_state_=RECVED; state_=INIT; recv_buf_.consume(recv_buf_.size()); if(!socket_impl_) socket_impl_.reset(new ssl_stream_wrapper<tcp::socket>( connection_->get_io_service(), enable_ssl_)); while(!send_bufs_.empty()) { send_bufs_.pop(); } if (conn_timeout_timer_) { conn_timeout_timer_->cancel(); } else { conn_timeout_timer_=timer_type::create(get_io_service()); conn_timeout_timer_->set_obj_desc("basic_http_connection_impl::conn_timeout_timer_"); } if (is_passive_) state_=CONNECTED; send_bufs_len_=0; }
void deadline_timer::async_wait(event_handler const &h) { std::auto_ptr<waiter> wt(new waiter); wt->h=h; wt->self = this; event_id_ = get_io_service().set_timer_event(deadline_,wt); }
void avif::construct() { quitting = boost::make_shared< boost::atomic<bool> >(false); _write_queue = boost::make_shared< boost::async_coro_queue< std::queue< std::pair< auto_avPacketPtr, boost::function<void(boost::system::error_code)> > > > >( boost::ref(get_io_service()) ); }
void ResolverService::call_back( ResolveHandler * handler, boost::system::error_code const & ec, ResolverIterator const & iter) { get_io_service().post( boost::bind(&ResolveHandler::handle, handler, ec, iter)); }
unsigned agent_impl::connect(const char* ipaddr, unsigned short port, int32_t timeout_millis, unsigned short local_port) { connection_ptr new_conn(new connection(*this, get_io_service(), net_callback_)); add_connection(new_conn); new_conn->async_connect(ipaddr, port, timeout_millis, local_port); return new_conn->get_id(); }
std::function<void(int, const std::error_code&)> on_exit_handler(Executor & exec) { if (!pipe) pipe = std::make_shared<boost::process::async_pipe>(get_io_service(exec.seq)); auto pipe = this->pipe; return [pipe](int, const std::error_code& ec) { boost::asio::io_service & ios = pipe->get_io_service(); ios.post([pipe] { boost::system::error_code ec; pipe->close(ec); }); }; };
void deadline_timer::cancel() { if(event_id_!=-1) { int tmp_id = event_id_; event_id_ = -1; get_io_service().cancel_timer_event(tmp_id); } }
basic_timer(io_service& engine_svc) : basic_engine_object(engine_svc) , deadline_timer_(get_io_service()) , repeat_times_(0) , repeated_times_(0) , status_(NOT_WAITING) { set_obj_desc("basic_timer"); next_op_stamp(); }
int32_t agent_impl::start_server(unsigned short port) { stop_server(); acceptor_.reset(new acceptor(*this, get_io_service())); int32_t ret = acceptor_->start(port, net_callback_); if (ret != 0) { acceptor_.reset(); } return 0; }
void bind_and_listen(const std::string& port,bool reuse_addr) { boost::asio::ip::tcp::resolver resolver(get_io_service()); boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(),port); boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); open(endpoint.protocol()); if(reuse_addr) { set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); } bind(endpoint); listen(boost::asio::socket_base::max_connections); }
bool avjackif::handshake() { boost::mutex m; boost::condition_variable ready; boost::unique_lock<boost::mutex> l(m); boost::asio::spawn(get_io_service(), [&,this](boost::asio::yield_context ctx){ this->async_handshake(ctx); ready.notify_all(); }); ready.wait(l); return true; }
bool basic_io_device::dont_block(event_handler const &h) { if(nonblocking_was_set_) return true; system::error_code e; set_non_blocking(true,e); if(e) { get_io_service().post(h,e); return false; } nonblocking_was_set_ = true; return true; }
/// Get an io_service to use. if need then create one to use. boost::asio::io_service& get_io_service(std::size_t load) { // Calculate the required number of threads. std::size_t threads_number = load / pool_thread_load_; if ((threads_number > io_services_.size()) && (io_services_.size() < pool_high_watermark_) && !block_) { // Create new io_service and start it. io_service_ptr io_service(new boost::asio::io_service); io_services_.push_back(io_service); start_one(io_service); next_io_service_ = io_services_.size() - 1; } return get_io_service(); }
void stream_socket::async_connect(endpoint const &ep,event_handler const &h) { if(!dont_block(h)) return; system::error_code e; connect(ep,e); if(e && would_block(e)) { async_connector::pointer connector(new async_connector(h,this)); on_writeable(connector); } else { event_binder::pointer binder(new event_binder( h,e )); get_io_service().post(binder); } }
void on_setup(WindowsExecutor &exec) { if (!pipe) pipe = std::make_shared<boost::process::async_pipe>(get_io_service(exec.seq)); auto source_handle = pipe->native_source(); boost::detail::winapi::SetHandleInformation(source_handle, boost::detail::winapi::HANDLE_FLAG_INHERIT_, boost::detail::winapi::HANDLE_FLAG_INHERIT_); exec.startup_info.hStdInput = source_handle; exec.startup_info.dwFlags |= boost::detail::winapi::STARTF_USESTDHANDLES_; exec.inherit_handles = true; }
CFStringRef copy_device_serial_number() { io_service_t service; CFStringRef serialNumber; service = get_io_service("IOPlatformExpertDevice"); if(!service) { printf("unable to find IOPlatformExpertDevice service\n"); return NULL; } serialNumber = IORegistryEntryCreateCFProperty(service, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0); IOObjectRelease(service); return serialNumber; }
virtual void connect(std::wstring host, int port) { tcp::resolver resolver(get_io_service()); tcp::resolver::query query(to_string(host), to_string(port)); //tcp::resolver::query query("www.medin.name", "80"); //tcp::resolver::query query("test_server", "80"); tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); tcp::resolver::iterator end; boost::system::error_code error = boost::asio::error::host_not_found; while (error && endpoint_iterator != end) { tcp::resolver::endpoint_type ep = *endpoint_iterator; get_socket().close(); get_socket().lowest_layer().connect(*endpoint_iterator++, error); } if (error) throw boost::system::system_error(error); }
bool url_crack_processor::process(const uri& u, const http::request& req, const connection_sptr& sock) { std::map<std::string, std::string> qmap=u.query_map(); const std::string& cmd=qmap["cmd"]; if (cmd == "url_crack") { std::string url = qmap["uri"]; url = hex_to_string(url); crack_adapter_sptr cracker = crack_adapter::create(url, get_io_service()); crack_adapter_wptr cracker_wptr(cracker); connection_wptr sock_wsptr(sock); cracker->get_crack_urls()=boost::bind(&this_type::write_urls, this, _1, cracker_wptr); crack_adapters_[cracker] = sock; return true; } return false; }
void basic_http_connection_impl::keep_async_receiving() { BOOST_ASSERT(connection_); if (is_recv_blocked_&&recv_state_!=RECVING) { is_recv_blocked_=false; if (is_header_recvd_&&recv_buf_.size()>0) { get_io_service().post(make_alloc_handler( boost::bind(&this_type::__async_recv_handler, SHARED_OBJ_FROM_THIS,error_code(),recv_buf_.size(), op_stamp()) )); } else do_keep_receiving(op_stamp()); } }
void stream_socket::async_read_some(mutable_buffer const &buffer,io_handler const &h) { if(!dont_block(h)) return; #ifdef BOOSTER_AIO_FORCE_POLL reader_some::pointer reader(new reader_some(h,buffer,this)); on_readable(reader); #else system::error_code e; size_t n = read_some(buffer,e); if(e && would_block(e)) { reader_some::pointer reader(new reader_some(h,buffer,this)); on_readable(reader); } else { io_binder::pointer binder(new io_binder( h,n,e )); get_io_service().post(binder); } #endif }
async_write(const std::string &data, Handler &&handler) { BUNSAN_ASIO_INITFN_BEGIN(Handler, handler, void(boost::system::error_code), real_handler, result) std::ostringstream header_stream; const header size = data.size(); header_stream << std::setw(HEADER_SIZE) << std::hex << size; if (!header_stream || header_stream.str().size() != HEADER_SIZE) { boost::system::error_code error(boost::asio::error::invalid_argument); get_io_service().post(std::bind(real_handler, error)); } m_outbound_header = header_stream.str(); std::vector<boost::asio::const_buffer> buffers = { boost::asio::buffer(m_outbound_header), boost::asio::buffer(data)}; boost::asio::async_write( m_connection, buffers, [real_handler](const boost::system::error_code &ec, const std::size_t) mutable { real_handler(ec); }); BUNSAN_ASIO_INITFN_END(result) }
void async_move_order(TradingContext* ctx, IAsyncMarket* market, int64_t order_id, int64_t new_order_id, double price, int volume, const pt::time_duration& duration, Handler&& handler) { auto __handler = std::move(handler); auto _handler = [&, __handler] (system::error_code errc, int64_t order_id) mutable { get_io_service().post([errc, order_id, __handler]() mutable { __handler(errc, order_id); }); }; asio::spawn(m_ios, [=] (asio::yield_context yield) mutable { int64_t _new_order_id = new_order_id; if (!_new_order_id) { _new_order_id = generate_order_id(); } if (market) { market->move_order(order_id, _new_order_id, price, volume); } else { async2::move_order(m_ios, order_id, _new_order_id, price, volume); } try { async2::async_wait_cancel(m_ios, *ctx, order_id, duration, yield); async2::async_wait_order(m_ios, *ctx, _new_order_id, duration, yield); } catch (system::system_error& e) { _handler(system::error_code(e.code()), _new_order_id); return; } _handler(system::error_code(), _new_order_id); }); }
void basic_http_connection_impl::__retry_async_resolve_connect_coro(op_stamp_t stamp) { if(is_canceled_op(stamp)) return; if (resolve_timer_) { resolve_timer_->cancel(); resolve_timer_.reset(); } if (!conn_retry_timer_) { conn_retry_timer_=timer_type::create(get_io_service()); conn_retry_timer_->set_obj_desc("basic_http_connection_impl::conn_retry_timer_"); } conn_retry_timer_->cancel(); conn_retry_timer_->time_signal().clear(); conn_retry_timer_->time_signal().bind(&this_type::__async_resolve_connect_coro, this,stamp,coroutine(),error_code(),resolver_iterator() ); conn_retry_timer_->async_wait(milliseconds(100)); }
/// Get an io_service to use. if need then create one to use. boost::asio::io_service& get_io_service(size_t load) { // Calculate the required number of threads. size_t threads_number = load / pool_thread_load_; // Lock for synchronize access to data. scoped_lock_t lock(mutex_); size_t service_count = io_services_.size(); if (!blocked_ && \ !work_.empty() && \ !threads_.empty() && \ threads_number > service_count && \ service_count < pool_high_watermark_) { // Create new io_service and start it. io_service_ptr io_service(new boost::asio::io_service); io_services_.push_back(io_service); start_one(io_service); next_io_service_ = service_count; } return get_io_service(); }
void PptvMedia::set_response( MediaBase::response_type const & resp) { resp_ = resp; // it is safe to get user stat object now ppbox::demux::DemuxModule & demux = util::daemon::use_module<ppbox::demux::DemuxModule>(get_io_service()); owner_ = demux.find(MediaBase::url_); // 需要原始的URL if (owner_) { owner_type_ = ot_demuxer; ppbox::peer::PeerSource & peer = const_cast<ppbox::peer::PeerSource &>(static_cast<ppbox::peer::PeerSource const &>(demuxer().source().source())); peer.pptv_media(*this); demuxer().on<ppbox::demux::StatusChangeEvent>(boost::bind(&PptvMedia::on_event, this, _1)); return; } ppbox::merge::MergeModule & merge = util::daemon::use_module<ppbox::merge::MergeModule>(get_io_service()); owner_ = merge.find(MediaBase::url_); // 需要原始的URL if (owner_) { owner_type_ = ot_merger; ppbox::peer::PeerSource & peer = const_cast<ppbox::peer::PeerSource &>(static_cast<ppbox::peer::PeerSource const &>(merger().source().source())); peer.pptv_media(*this); //merger().on<ppbox::demux::StatusChangeEvent>(boost::bind(&PptvMedia::on_event, this, _1)); return; } }
void on_setup(Executor & exec) { pipe = std::make_shared<boost::process::async_pipe>(get_io_service(exec.seq)); }