Skip to content

Instantly share code, notes, and snippets.

@mbohun
Created October 13, 2012 09:02
Show Gist options
  • Save mbohun/3883894 to your computer and use it in GitHub Desktop.
Save mbohun/3883894 to your computer and use it in GitHub Desktop.
UDP blocking receive loop (C++, boost::asio)
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