Created
March 16, 2015 15:59
-
-
Save redboltz/b5e432a0ff54897562dc to your computer and use it in GitHub Desktop.
asio and Boost.Coroutine
This file contains 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
#include <iostream> | |
#include <functional> | |
#include <boost/array.hpp> | |
#include <boost/asio.hpp> | |
#include <boost/coroutine/all.hpp> | |
using boost::asio::ip::tcp; | |
char g_data[] = {0}; | |
struct wrap { | |
wrap(boost::asio::io_service& ios, | |
tcp::resolver::query const& query) | |
:ios(ios), | |
resolver(ios), | |
it(resolver.resolve(query)), | |
socket(ios) {} | |
void connect() { | |
boost::asio::async_connect( | |
socket, it, | |
[&] | |
(boost::system::error_code const& ec, boost::asio::ip::tcp::resolver::iterator){ | |
connect_hdl(); | |
}); | |
} | |
void close() { | |
socket.async_write_some( | |
boost::asio::buffer(g_data, sizeof(g_data)), | |
[=](boost::system::error_code const& ec, | |
std::size_t bytes_transferred) { | |
socket.close(); | |
close_hdl(); | |
}); | |
} | |
boost::asio::io_service& ios; | |
tcp::resolver resolver; | |
tcp::resolver::iterator it; | |
tcp::socket socket; | |
std::function<void(void)> connect_hdl; | |
std::function<void(void)> close_hdl; | |
}; | |
int main() { | |
tcp::resolver::query query("localhost", "12345"); | |
boost::asio::io_service ios1; | |
wrap w1(ios1, query); | |
boost::asio::io_service ios2; | |
wrap w2(ios2, query); | |
boost::coroutines::asymmetric_coroutine<void>::pull_type source1( | |
[&](boost::coroutines::asymmetric_coroutine<void>::push_type& sink){ | |
w1.connect_hdl = [&sink]{ | |
std::cout << "connected1" << std::endl; | |
sink(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
}; | |
w1.close_hdl = [&sink]{ | |
std::cout << "closed1" << std::endl; | |
sink(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
}; | |
w1.connect(); | |
std::cout << __FILE__ << ":" << __LINE__ << " ios1.run()" << std::endl; | |
ios1.run(); | |
std::cout << __FILE__ << ":" << __LINE__ << " ios1.run() finished" << std::endl; | |
}); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
boost::coroutines::asymmetric_coroutine<void>::pull_type source2( | |
[&](boost::coroutines::asymmetric_coroutine<void>::push_type& sink){ | |
w2.connect_hdl = [&sink]{ | |
std::cout << "connected2" << std::endl; | |
sink(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
}; | |
w2.close_hdl = [&sink]{ | |
std::cout << "closed2" << std::endl; | |
sink(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
}; | |
w2.connect(); | |
std::cout << __FILE__ << ":" << __LINE__ << " ios2.run()" << std::endl; | |
ios2.run(); | |
std::cout << __FILE__ << ":" << __LINE__ << " ios2.run() finished" << std::endl; | |
}); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
w1.close(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
w2.close(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
source1(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
source1(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
source2(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
source2(); | |
std::cout << __FILE__ << ":" << __LINE__ << std::endl; | |
} |
This file contains 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
#include <iostream> | |
#include <string> | |
#include <boost/asio.hpp> | |
using boost::asio::ip::tcp; | |
int main(){ | |
try { | |
boost::asio::io_service ios; | |
tcp::acceptor acceptor(ios, tcp::endpoint(tcp::v4(), 12345)); | |
boost::asio::socket_base::reuse_address option(true); | |
acceptor.set_option(option); | |
for (;;) { | |
tcp::socket socket(ios); | |
acceptor.accept(socket); | |
} | |
} | |
catch (std::exception const& e) { | |
std::cerr << e.what() << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment