Last active
August 29, 2015 14:09
-
-
Save jmorrill/dd7853983c47e6d2824e to your computer and use it in GitHub Desktop.
libuv C++ library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int _tmain(int argc, _TCHAR* argv[]) | |
| { | |
| cout << "initial thread id - " << this_thread::get_id() << endl; | |
| /* dns resolver */ | |
| auto dns = net::dns(); | |
| /* socket client */ | |
| auto socket = net::stream_socket(); | |
| /* Get dispatcher for this thread */ | |
| auto dispatcher = event_dispatcher::current_dispatcher(); | |
| /* our current dispatcher context */ | |
| auto task_context = task_continuation_context::use_current(); | |
| /* Buffer to read incoming data */ | |
| vector<char> read_buffer(1024 * 1024); | |
| /* Resolve host */ | |
| dns.resolve_host_async("localhost").then([&socket](std::string const & ipaddress) | |
| { | |
| cout << "got ip address - " << ipaddress << endl; | |
| return socket.connect_async(ipaddress, 80); | |
| }, task_context).then([&socket]( /* task<void>& t - if we want exception handling */) | |
| { | |
| cout << "connected !" << endl; | |
| /* Send http command */ | |
| return socket.write_async("GET /big_buck_bunny_480p_h264.mov\r\n\r\n"); | |
| }, task_context).then([&socket, &read_buffer, task_context] | |
| { | |
| return create_iterative_task([&socket, &read_buffer, task_context]() | |
| { | |
| return create_task([&socket] | |
| { | |
| /* task always comes back on thread pool for some reason */ | |
| }, task_context).then([&socket,&read_buffer] | |
| { | |
| return socket.read_async(read_buffer); | |
| },task_context).then([&read_buffer,task_context](task<int>& t) | |
| { | |
| int bytes_read = 0; | |
| try | |
| { | |
| bytes_read = t.get(); | |
| } | |
| catch (...) | |
| { | |
| /* exit iterative task */ | |
| return false; | |
| } | |
| /* continue iterative task */ | |
| return bytes_read > 0; | |
| }, task_context); | |
| }, task_context); | |
| }, task_context).then([dispatcher] | |
| { | |
| cout << "finished downloading" << endl; | |
| /* shutdown the current event dispatcher */ | |
| dispatcher->begin_shutdown(); | |
| },task_context); | |
| event_dispatcher::run(); | |
| cout << "exiting..." << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment