Created
October 13, 2012 09:02
-
-
Save mbohun/3883894 to your computer and use it in GitHub Desktop.
UDP blocking receive loop (C++, boost::asio)
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
void receive(udp::socket* sock_in, fifo<packet*> *fifo_in, fifo<packet*> *fifo_out) { | |
try { | |
for (;;) { | |
packet *p = fifo_in->pop(); | |
p->len = sock_in->receive_from(boost::asio::buffer(p->data, UDP_DATA_SIZE_MAX), | |
p->src); | |
// TODO: this is a little overhead - all we need is save start time before entering this loop, and then \ | |
record the ns/us diff | |
const boost::chrono::nanoseconds ns = | |
boost::chrono::duration_cast<boost::chrono::nanoseconds>(boost::chrono::system_clock::now().time\ | |
_since_epoch()); | |
p->time_stamp = ns.count(); | |
fifo_out->push(p); | |
} | |
} catch (const std::exception& e) { | |
std::cerr << "receive exception:" << e.what() << std::endl; | |
return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment