mbohun
10/13/2012 - 9:02 AM

UDP blocking receive loop (C++, boost::asio)

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