Skip to content

Instantly share code, notes, and snippets.

@jamboree
Created April 13, 2017 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamboree/b0b8a81bb5d3acdca8b2b2557e9b7f1d to your computer and use it in GitHub Desktop.
Save jamboree/b0b8a81bb5d3acdca8b2b2557e9b7f1d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <co2/coroutine.hpp>
#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/ip/udp.hpp>
#include <act/timer.hpp>
#include <act/socket.hpp>
namespace asio = boost::asio;
auto session(asio::ip::udp::socket sock) CO2_BEG(void, (sock),
asio::ip::udp::endpoint endpoint;
asio::steady_timer timer{sock.get_io_service()};
char data[1024];
)
{
using namespace std::chrono_literals;
for ( ; ; )
{
CO2_TRY
{
CO2_AWAIT_LET(auto n, act::timeout(act::receive_from(sock, asio::buffer(data), endpoint), timer, 10s),
{
std::cout << "Received: ";
std::cout.write(data, n);
std::cout << "\n";
std::cout << "from: " << endpoint << "\n";
});
}
CO2_CATCH (std::exception const& e)
{
std::cout << "Receive error: " << e.what() << "\n";
}
}
} CO2_END
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cerr << "Usage: udp_timeout <listen_addr> <listen_port>\n";
return 1;
}
asio::ip::udp::endpoint listen_endpoint
(
boost::asio::ip::address::from_string(argv[1]),
std::atoi(argv[2])
);
asio::io_service io;
session(asio::ip::udp::socket{io, listen_endpoint});
io.run();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment