void thread_fun() { for (;;) { error_code ec; tcp::endpoint from; tcp::socket socket(m_ios); condition_variable cond; bool done = false; m_acceptor.async_accept(socket, from, boost::bind(&new_connection, _1, &ec, &done)); while (!done) { m_ios.run_one(); m_ios.reset(); } if (ec == boost::asio::error::operation_aborted || ec == boost::asio::error::bad_descriptor) return; if (ec) { fprintf(stderr, "Error accepting connection on peer socket: %s\n", ec.message().c_str()); return; } fprintf(stderr, "%s: incoming peer connection\n", time_now_string()); ++m_peer_requests; socket.close(ec); } }
void CPluginTransportTCP::handleAsyncConnect(const boost::system::error_code & err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator) { delete m_Resolver; m_Resolver = NULL; if (!err) { m_bConnected = true; m_Socket->async_read_some(boost::asio::buffer(m_Buffer, sizeof m_Buffer), boost::bind(&CPluginTransportTCP::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); if (ios.stopped()) // make sure that there is a boost thread to service i/o operations { ios.reset(); _log.Log(LOG_NORM, "PluginSystem: Starting I/O service thread."); boost::thread bt(boost::bind(&boost::asio::io_service::run, &ios)); } } else { delete m_Socket; m_Socket = NULL; // _log.Log(LOG_ERROR, "Plugin: Connection Exception: '%s' connecting to '%s:%s'", err.message().c_str(), m_IP.c_str(), m_Port.c_str()); } ConnectedMessage* Message = new ConnectedMessage(m_HwdID, err.value(), err.message()); boost::lock_guard<boost::mutex> l(PluginMutex); PluginMessageQueue.push(Message); }
/** * @brief Funkcia sa snazi co najbezpecnejsim sposobom ukoncit server v pripade nudze * * zastavuje celu sietovu vrstvu programu a uvolnuje pamat */ static void sighandler(int signum) { (void)signum; io_service.reset(); io_service.stop(); Manager::instance().Shutdown(); }
void redux::runThreadsAndWait(boost::asio::io_service& service, uint16_t nThreads) { boost::thread_group pool; for(uint16_t t = 0; t < nThreads; ++t) { pool.create_thread(boost::bind(&boost::asio::io_service::run, &service)); } pool.join_all(); service.reset(); // reset service so that next call will not fail }
void run_event_loop_until_connect() { while (!last_error && !is_connected) { service.poll(); service.reset(); check_if_timed_out(); } }
void pump_and_run() { assert_false(comp.complete_); int n = 0; while (n < 50 && !comp.complete_) { n += 1; svc.reset(); svc.poll_one(); } assert_true(comp.complete_); }
//****************************************************************************** void startLogThread() { if (!LogThreadWork.get()) { LogThreadWork.reset(new boost::asio::io_service::work(LogIoService)); LogIoService.reset(); LogThread = boost::thread([](){ LogIoService.run(); }); } }
void run_event_loop_until_frame_received() { using boost::bind; have_frame = false; buffer.consume(buffer.size()); last_error = boost::system::error_code(); boost::asio::async_read_until(socket, buffer, "END\r\n", bind(&TimedSessionBase::handle_frame_reception, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); service.reset(); while (!last_error && !have_frame) { service.poll(); service.reset(); check_if_timed_out(); } }
void connection_t::connect(boost::asio::io_service& ioservice, const endpoint_t& endpoint, unsigned int connect_timeout) { ioservice.reset(); if(endpoint.is_unix()) { auto s = std::make_shared<boost::asio::local::stream_protocol::socket>(ioservice); boost::system::error_code error; s->async_connect(boost::asio::local::stream_protocol::endpoint(endpoint.get_path()), std::bind(&local_connection_handler, std::placeholders::_1, std::ref(error), std::ref(ioservice))); if(run_with_timeout(ioservice, connect_timeout)) { throw std::runtime_error("Connection timed out"); } else if(error) { throw boost::system::system_error(error); } m_socket = s; } else { boost::asio::ip::tcp::resolver resolver(ioservice); tcp_connector conn = { ioservice, std::shared_ptr<boost::asio::ip::tcp::socket>(), boost::system::error_code(), std::vector<boost::asio::ip::tcp::endpoint>() }; resolver.async_resolve( boost::asio::ip::tcp::resolver::query( endpoint.get_host(), boost::lexical_cast<std::string>(endpoint.get_port()) ), std::bind(&tcp_connector::resolve_handler, &conn, std::placeholders::_1, std::placeholders::_2) ); if(run_with_timeout(ioservice, connect_timeout)) { throw std::runtime_error("Connection timed out"); } else if(conn.socket) { m_socket = conn.socket; } else { throw boost::system::system_error(conn.error); } } }
void RunIoService(int numThreads = 1) { ioService.reset(); boost::thread_group threads; for (int i = 0; i < numThreads - 1; ++i) { threads.create_thread([this]{ioService.run();}); } ioService.run(); threads.join_all(); }
void ThreadPool::run(boost::asio::io_service& service, ThreadInit fct) { if (fct) { fct(); } ++running_threads_; LOG(thread_logger, debug) << "Start thread"; service.reset(); service.run(); --running_threads_; LOG(thread_logger, debug) << "Thread stopped"; }
void Run(int id, int count) { for (size_t i = 0; i < count; ++i) { if (isRun) { std::cout << "id = " << id << "\n"; Sleep(1000); } else { break; } } timer.cancel(); io.stop(); io.reset(); }
void io_service_work( boost::asio::io_service& ios ) { while( !boost::this_thread::interruption_requested() ){ try{ ios.run(); }catch( boost::system::error_code &e ){ shot<connect_error>( boost::ref(e) ); }catch( boost::system::system_error & e ){ shot<system_error>( boost::ref(e) ); }catch( std::exception& e ){ shot<exception>( boost::ref(e) ); }catch(...){ assert(0); } if( ios.stopped() ){ boost::this_thread::sleep( boost::posix_time::milliseconds(10) ); ios.reset(); } } }
void thread_fun() { char buffer[2000]; for (;;) { error_code ec; udp::endpoint from; size_t bytes_transferred; bool done = false; m_socket.async_receive_from( asio::buffer(buffer, sizeof(buffer)), from, 0 , boost::bind(&incoming_packet, _1, _2, &bytes_transferred, &ec, &done)); while (!done) { m_ios.run_one(); m_ios.reset(); } if (ec == boost::asio::error::operation_aborted || ec == boost::asio::error::bad_descriptor) return; if (ec) { fprintf(stderr, "Error receiving on DHT socket: %s\n", ec.message().c_str()); return; } try { entry msg = bdecode(buffer, buffer + bytes_transferred); #if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM std::cerr << msg << std::endl; #endif ++m_dht_requests; } catch (std::exception& e) { fprintf(stderr, "failed to decode DHT message: %s\n", e.what()); } } }
bool CPluginTransportTCP::handleConnect() { try { if (!m_Socket) { m_bConnected = false; m_Resolver = new boost::asio::ip::tcp::resolver(ios); m_Socket = new boost::asio::ip::tcp::socket(ios); boost::system::error_code ec; boost::asio::ip::tcp::resolver::query query(m_IP, m_Port); boost::asio::ip::tcp::resolver::iterator iter = m_Resolver->resolve(query); boost::asio::ip::tcp::endpoint endpoint = *iter; // // Async resolve/connect based on http://www.boost.org/doc/libs/1_45_0/doc/html/boost_asio/example/http/client/async_client.cpp // m_Resolver->async_resolve(query, boost::bind(&CPluginTransportTCP::handleAsyncResolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator)); if (ios.stopped()) // make sure that there is a boost thread to service i/o operations { ios.reset(); _log.Log(LOG_NORM, "PluginSystem: Starting I/O service thread."); boost::thread bt(boost::bind(&boost::asio::io_service::run, &ios)); } } } catch (std::exception& e) { // _log.Log(LOG_ERROR, "Plugin: Connection Exception: '%s' connecting to '%s:%s'", e.what(), m_IP.c_str(), m_Port.c_str()); ConnectedMessage* Message = new ConnectedMessage(m_HwdID, -1, std::string(e.what())); boost::lock_guard<boost::mutex> l(PluginMutex); PluginMessageQueue.push(Message); return false; } return true; }
int main() { for (size_t i = 0; i < 5; ++i) { std::cout << "Iteraton = " << i << "\n"; timer.cancel(); io.stop(); io.reset(); timer.expires_from_now(boost::posix_time::seconds(200)); isRun = true; boost::thread th(Timing); Run(i, 5); th.join(); } return 0; }
Connection::~Connection() { sock.close(); netservice.poll(); netservice.reset(); }
void Connection::Run() { netservice.run(); netservice.reset(); }
void run() { while (should_run) { ios.run(); ios.reset(); } }