int main() { io_service::strand strand1(service), strand2(service); for (auto i = 0; i < 5; ++i) service.post(strand1.wrap(std::bind(func, i))); for (auto i = 5; i < 10; ++i) service.post(strand2.wrap(std::bind(func, i))); std::vector<std::thread> threads; for (auto i = 0; i < 3; ++i) threads.push_back(std::thread(worker_thread)); std::this_thread::sleep_for(std::chrono::milliseconds(500)); for (auto& th : threads) th.join(); }
///登录,发送登陆请求给服务器 void talk_to_svr::do_login(const std::string &username, const std::string &password) { _username = username; _password = password; std::string writeMsg("login " + username + " " + password + "\n"); service.post(MEM_FN1(do_write, writeMsg)); }
void tracker_manager::queue_request( io_service& ios , connection_queue& cc , tracker_request req , std::string const& auth , boost::weak_ptr<request_callback> c) { mutex_t::scoped_lock l(m_mutex); TORRENT_ASSERT(req.num_want >= 0); TORRENT_ASSERT(!m_abort || req.event == tracker_request::stopped); if (m_abort && req.event != tracker_request::stopped) return; if (req.event == tracker_request::stopped) req.num_want = 0; TORRENT_ASSERT(!m_abort || req.event == tracker_request::stopped); if (m_abort && req.event != tracker_request::stopped) return; std::string protocol = req.url.substr(0, req.url.find(':')); boost::intrusive_ptr<tracker_connection> con; #ifdef TORRENT_USE_OPENSSL if (protocol == "http" || protocol == "https") #else if (protocol == "http") #endif { con = new http_tracker_connection( ios, cc, *this, req, c , m_ses, m_proxy, auth #if TORRENT_USE_I2P , &m_ses.m_i2p_conn #endif ); } else if (protocol == "udp") { con = new udp_tracker_connection( ios, cc, *this, req , c, m_ses , m_proxy); } else { // we need to post the error to avoid deadlock if (boost::shared_ptr<request_callback> r = c.lock()) ios.post(boost::bind(&request_callback::tracker_request_error, r, req , -1, error_code(errors::unsupported_url_protocol) , "", 0)); return; } m_connections.push_back(con); boost::shared_ptr<request_callback> cb = con->requester(); if (cb) cb->m_manager = this; con->start(); }
void tracker_manager::queue_request( io_service& ios , tracker_request req , std::weak_ptr<request_callback> c) { TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(req.num_want >= 0); TORRENT_ASSERT(!m_abort || req.event == tracker_request::stopped); if (m_abort && req.event != tracker_request::stopped) return; if (req.event == tracker_request::stopped) req.num_want = 0; TORRENT_ASSERT(!m_abort || req.event == tracker_request::stopped); if (m_abort && req.event != tracker_request::stopped) return; std::string protocol = req.url.substr(0, req.url.find(':')); #ifdef TORRENT_USE_OPENSSL if (protocol == "http" || protocol == "https") #else if (protocol == "http") #endif { std::shared_ptr<http_tracker_connection> con = std::make_shared<http_tracker_connection>( std::ref(ios), std::ref(*this), std::cref(req), c); m_http_conns.push_back(con); con->start(); return; } else if (protocol == "udp") { std::shared_ptr<udp_tracker_connection> con = std::make_shared<udp_tracker_connection>( std::ref(ios), std::ref(*this), std::cref(req) , c); m_udp_conns[con->transaction_id()] = con; con->start(); return; } // we need to post the error to avoid deadlock if (std::shared_ptr<request_callback> r = c.lock()) ios.post(std::bind(&request_callback::tracker_request_error, r, req , -1, error_code(errors::unsupported_url_protocol) , "", 0)); }
void read_send( session_ptr session ) { string read; cin >> read; if( read == "quit" ) { // session_->close(); return ; } cout << "[SYS:send]" << read << endl; char send_buf[1024] = {0}; unsigned short message_len = static_cast<unsigned short>( read.length() ); ::memcpy( send_buf, &message_len, sizeof(message_len) ); ::memcpy( send_buf + sizeof(message_len), read.c_str(), message_len ); ios_.post( BOOST_BIND( &io_session::write, session, shared_ptr<io_buffer>( new io_buffer( send_buf, message_len + sizeof(message_len))) ) ); // session->write( shared_ptr<io_buffer>( new io_buffer( send_buf, message_len + sizeof(message_len))) ); }
void postNewValue (io_service & ios, newValFunc_t f, Tag & t, const string& nam, const any& val, Variable::Pointer var ) { ios.post( bind(f, std::ref(t), nam, val, var) ); }
///sendto 发送信息到目的用户端 void talk_to_svr::do_sendto(const std::string &username, const std::string &msg) { //username 和 msg 赋值 std::string writeMsg("send to " + username + " " + msg + "\n"); service.post(MEM_FN1(do_write, writeMsg)); }
///ask for clients 亲求客户端信息 void talk_to_svr::do_ask_clients() { service.post(MEM_FN1(do_write, "ask for clients\n")); }
///ping,发送ping请求给服务器 void talk_to_svr::do_ping() { service.post(MEM_FN1(do_write, "ping\n")); }