Created
December 18, 2014 23:48
-
-
Save jmorrill/238873b78fb8cad65b27 to your computer and use it in GitHub Desktop.
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 read_pass_counter = 0; | |
uint64_t total_bytes_read = 0; | |
string host_name = "10.1.30.151"; | |
int host_port = 80; | |
string http_command = "GET /movie.mp4 HTTP/1.0\r\n\r\n"; | |
void test_method() | |
{ | |
/* convenience struct to hold data for async calls. Also helps | |
* in decreasing atomic ops in create_iterative task*/ | |
struct socket_file_holder | |
{ | |
socket_file_holder() : io_buff(io::memory_buffer(512 * 1024)){ } | |
socket_file_holder(const socket_file_holder &) = delete; | |
socket_file_holder& operator=(const socket_file_holder &) = delete; | |
fs::file file; | |
net::stream_socket socket; | |
io::memory_buffer io_buff; | |
}; | |
auto async_objects = make_shared<socket_file_holder>(); | |
async_objects->socket.connect_async(host_name, host_port). | |
then([async_objects](task<void> connect_task) | |
{ | |
connect_task.get(); | |
printf("connected to %s port %d\n", host_name.c_str(), host_port); | |
printf("writing http GET cmd to socket\n", http_command.c_str()); | |
return async_objects->socket.write_async(http_command); | |
}). | |
then([async_objects](task<void> socket_write_task) | |
{ | |
socket_write_task.get(); | |
printf("opening file test.bin to write http data to...\n"); | |
return async_objects->file.open_async("dump.bin", std::ios_base::out); | |
}). | |
then([async_objects]() | |
{ | |
printf("begin downloading from socket\n"); | |
return create_iterative_task([async_objects]() | |
{ | |
return async_objects->socket.read_async(async_objects->io_buff, 0, async_objects->io_buff.length_get()). | |
then([async_objects](int bytes) | |
{ | |
int bytes_read = 0; | |
bytes_read = bytes; | |
read_pass_counter++; | |
if (read_pass_counter % 1000 == 0) | |
{ | |
printf("bytes read %llu\r\n", total_bytes_read); | |
} | |
total_bytes_read += bytes_read; | |
return async_objects->file.write_async(async_objects->io_buff, 0, bytes_read); | |
}); | |
}, task_continuation_context::use_current()); | |
}). | |
then([async_objects](task<void> iterative_task) | |
{ | |
cout << "finished downloading..." << endl; | |
try | |
{ | |
iterative_task.get(); | |
} | |
catch (end_of_file const& ex) | |
{ | |
cout << ex.what() << endl; | |
} | |
catch (exception const& ex) | |
{ | |
cout << ex.what() << endl; | |
} | |
return async_objects->socket.shutdown_async(); | |
}). | |
then([async_objects](task<void> socket_shutdown_task) | |
{ | |
try | |
{ | |
socket_shutdown_task.get(); | |
} | |
catch (exception const& ex) | |
{ | |
cout << ex.what() << endl; | |
throw; | |
} | |
return async_objects->file.close_async(); | |
}). | |
then([async_objects](task<void> file_close_task) | |
{ | |
try | |
{ | |
file_close_task.get(); | |
} | |
catch (exception const& ex) | |
{ | |
printf("exception - %s\n", ex.what()); | |
} | |
printf("done!\n"); | |
event_dispatcher::current_dispatcher().begin_shutdown(); | |
}); | |
} | |
int main(int argc, _TCHAR* argv[]) | |
{ | |
auto dispatcher = event_dispatcher::current_dispatcher(); | |
printf("starting libuvxx\n\n"); | |
/*uvxx::fs::directory::create_directory_async("c:\\users\\jeremiah\\desktop\\abc\\def\\ghi\\jkl"). | |
then([](task<void> t) | |
{ | |
printf("done making dir\n"); | |
}); | |
uvxx::fs::directory::get_files_async("C:\\Users\\Jeremiah\\", true). | |
then([](task<uvxx::fs::directory_entry_result_ptr> t) | |
{ | |
try | |
{ | |
auto files = t.get(); | |
printf("found %d files\n", files->size()); | |
} | |
catch (no_file_or_directory const& e) | |
{ | |
printf("%s\n", e.what()); | |
} | |
catch (std::exception& e) | |
{ | |
printf("%s\n", e.what()); | |
} | |
}); | |
*/ | |
test_method(); | |
event_dispatcher::run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment