Boost.Asio c++ 网络编程翻译(24)
int main() {
talk_to_client::ptr client = talk_to_client::new_();
acc.async_accept(client->sock(), boost::bind(handle_
accept,client,_1));
service.run();
}
boost::thread_group threads;
void listen_thread() {
service.run();
}
void start_listen(int thread_count) {
for ( int i = 0; i < thread_count; ++i)
threads.create_thread( listen_thread);
}
int main(int argc, char* argv[]) {
talk_to_client::ptr client = talk_to_client::new_();
acc.async_accept(client->sock(), boost::bind(handle_
accept,client,_1));
start_listen(100);
threads.join_all();
}
void do_read() {
async_read(sock_, buffer(read_buffer_),MEM_FN2(read_complete,_1,_2), MEM_FN2(on_read,_1,_2));
post_check_ping();
}
void post_check_ping() {
timer_.expires_from_now(boost::posix_time::millisec(5000));
timer_.async_wait( MEM_FN(on_check_ping));
}
void update_clients_changed() {
array copy;
{ boost::recursive_mutex::scoped_lock lk(clients_cs);
copy = clients; }
for( array::iterator b = copy.begin(), e = copy.end(); b != e;
++b)
(*b)->set_clients_changed();
}
void my_func() {
...
}
service.post(my_func);
void on_complete() {
...
}
void my_func() {
...
service.post(on_complete);
}
async_call(my_func);
struct async_op : boost::enable_shared_from_this<async_op>, ... {
typedef boost::function<void(boost::system::error_code)>
completion_func;
typedef boost::function<boost::system::error_code ()> op_func;
struct operation { ... };
void start() {
{ boost::recursive_mutex::scoped_lock lk(cs_);
if ( started_) return; started_ = true; }
boost::thread t( boost::bind(&async_op::run,this));
}
void add(op_func op, completion_func completion, io_service
&service) {
self_ = shared_from_this();
boost::recursive_mutex::scoped_lock lk(cs_);
ops_.push_back( operation(service, op, completion));
if ( !started_) start();
}
void stop() {
boost::recursive_mutex::scoped_lock lk(cs_);
started_ = false; ops_.clear();
} private:
boost::recursive_mutex cs_;
std::vector<operation> ops_; bool started_; ptr self_;
};
- 一个异步调用的方法
- 当第一个方法结束时被调用的一个完成处理handler
- 会运行完成处理handler的io_service实例。这也是完成时通知你的地方。参考下面的代码:
-
struct async_op : boost::enable_shared_from_this<async_op> , private boost::noncopyable {
struct operation { operation(io_service & service, op_func op, completion_
func completion) : service(&service), op(op), completion(completion) , work(new io_service::work(service))
{} operation() : service(0) {} io_service * service; op_func op; completion_func completion; typedef boost::shared_ptr<io_service::work> work_ptr; work_ptr work;
};
... };
struct async_op : ... {
typedef boost::shared_ptr<async_op> ptr;
static ptr new_() { return ptr(new async_op); }
...
void run() {
while ( true) {
{ boost::recursive_mutex::scoped_lock lk(cs_);
if ( !started_) break; }
boost::this_thread::sleep( boost::posix_
time::millisec(10));
operation cur;
));
}
{ boost::recursive_mutex::scoped_lock lk(cs_);
if ( !ops_.empty()) {
cur = ops_[0]; ops_.erase( ops_.begin());
}}
if ( cur.service)
cur.service->post(boost::bind(cur.completion, cur.op()
self_.reset();
}
};
size_t checksum = 0;
boost::system::error_code compute_file_checksum(std::string file_name)
{
HANDLE file = ::CreateFile(file_name.c_str(), GENERIC_READ, 0, 0,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 0);
windows::random_access_handle h(service, file);
long buff[1024];
checksum = 0;
size_t bytes = 0, at = 0;
boost::system::error_code ec;
while ( (bytes = read_at(h, at, buffer(buff), ec)) > 0) {
at += bytes; bytes /= sizeof(long);
for ( size_t i = 0; i < bytes; ++i)
checksum += buff[i];
}
return boost::system::error_code(0, boost::system::generic_
category());
}
void on_checksum(std::string file_name, boost::system::error_code) {
std::cout << "checksum for " << file_name << "=" << checksum <<
std::endl;
}
int main(int argc, char* argv[]) {
std::string fn = "readme.txt";
async_op::new_()->add( service, boost::bind(compute_file_
checksum,fn),
boost::bind(on_checksum,fn,_1));
service.run();
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。