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 <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