commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 49/148: Added read_packet with timeout meth


From: git
Subject: [Commit-gnuradio] [gnuradio] 49/148: Added read_packet with timeout method to ethernet. Now the control recv can timeout and immediately recv.
Date: Mon, 15 Aug 2016 00:47:23 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

nwest pushed a commit to annotated tag old_usrp_devel_udp
in repository gnuradio.

commit 93431af11a6a34bff8e8f05493cf955c1a9b4815
Author: Josh Blum <address@hidden>
Date:   Fri Nov 20 19:16:40 2009 -0800

    Added read_packet with timeout method to ethernet.
    Now the control recv can timeout and immediately recv.
---
 usrp2/host/lib/eth_ctrl_transport.cc | 16 ++++++++++------
 usrp2/host/lib/eth_ctrl_transport.h  | 10 +++++++---
 usrp2/host/lib/ethernet.cc           | 16 +++++++++++++++-
 usrp2/host/lib/ethernet.h            | 15 ++++++++++++++-
 usrp2/host/lib/find.cc               |  7 ++-----
 usrp2/host/lib/transport.cc          |  2 +-
 6 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/usrp2/host/lib/eth_ctrl_transport.cc 
b/usrp2/host/lib/eth_ctrl_transport.cc
index 58fa58d..2a622cc 100644
--- a/usrp2/host/lib/eth_ctrl_transport.cc
+++ b/usrp2/host/lib/eth_ctrl_transport.cc
@@ -18,8 +18,8 @@
 
 #include "eth_ctrl_transport.h"
 
-usrp2::eth_ctrl_transport::eth_ctrl_transport(const std::string &ifc, 
u2_mac_addr_t mac, double timeout, bool target)
- : transport("ethernet control"), d_mac(mac), d_buff(NULL), d_timeout(timeout){
+usrp2::eth_ctrl_transport::eth_ctrl_transport(const std::string &ifc, 
u2_mac_addr_t mac, bool target)
+ : transport("ethernet control"), d_mac(mac), d_buff(NULL){
 
     //create raw ethernet device
     d_eth_ctrl = new ethernet();
@@ -79,9 +79,15 @@ static void delete_array(uint8_t *array){delete[] array;}
 usrp2::transport::sbuff_vec_t usrp2::eth_ctrl_transport::recv(){
     sbuff_vec_t sbs;
     for (size_t i = 0; i < max_buffs(); i++){
-        //allocate a new buffer and recv
+        //conditionally allocate a new buffer
         if (d_buff == NULL) d_buff = new uint8_t[ethernet::MAX_PKTLEN];
-        int recv_len = d_eth_ctrl->read_packet_dont_block(d_buff, 
ethernet::MAX_PKTLEN);
+        // This method must return immediately after getting a packet.
+        // Therefore, only the first call to read_packet (when size==0)
+        // may have a timeout and further calls must return immediately.
+        // This way, we return once all available packets have been read.
+        int recv_len = sbs.size()?
+            d_eth_ctrl->read_packet_dont_block(d_buff, ethernet::MAX_PKTLEN):
+            d_eth_ctrl->read_packet_timeout(d_buff, ethernet::MAX_PKTLEN, 
100); // FIXME magic timeout
         //strip the ethernet headers from the buffer
         if (recv_len > (signed)sizeof(u2_eth_packet_t)){
             sbs.push_back(sbuff::make(
@@ -91,7 +97,5 @@ usrp2::transport::sbuff_vec_t 
usrp2::eth_ctrl_transport::recv(){
             d_buff = NULL; //set to null to flag for a new allocation
         } else break;
     }
-    //if nothing was received, busy sleep to save cpu
-    if (sbs.size() == 0) 
boost::this_thread::sleep(gruel::get_new_timeout(d_timeout));
     return sbs;
 }
diff --git a/usrp2/host/lib/eth_ctrl_transport.h 
b/usrp2/host/lib/eth_ctrl_transport.h
index 3814cae..80fb10e 100644
--- a/usrp2/host/lib/eth_ctrl_transport.h
+++ b/usrp2/host/lib/eth_ctrl_transport.h
@@ -42,14 +42,18 @@ namespace usrp2{
          * When the target is false, the packet filter is setup to ignore our 
mac address.
          * \param ifc the ethernet device name
          * \param mac the destination mac address
-         * \param timeout the timeout in seconds (default 50ms)
          * \param target true for an inbound target
          */
-        eth_ctrl_transport(const std::string &ifc, u2_mac_addr_t mac, double 
timeout=0.05, bool target = true);
+        eth_ctrl_transport(const std::string &ifc, u2_mac_addr_t mac, bool 
target = true);
         ~eth_ctrl_transport();
         bool sendv(const iovec *iov, size_t iovlen);
         sbuff_vec_t recv();
-        size_t max_buffs(){return 3;}
+        /*!
+         * \brief Controls the maximum size returned by recv
+         * Any integer larger than 0 would work here.
+         * \return the max size of sbuffs recv vector
+         */
+        size_t max_buffs(){return 7;} 
 };
 
 
diff --git a/usrp2/host/lib/ethernet.cc b/usrp2/host/lib/ethernet.cc
index e51680b..9cbf291 100644
--- a/usrp2/host/lib/ethernet.cc
+++ b/usrp2/host/lib/ethernet.cc
@@ -37,6 +37,7 @@
 #include <sys/socket.h>
 #include <net/ethernet.h>
 #include <netinet/in.h>
+#include <sys/poll.h>
 
 #include <linux/types.h>
 #include <netpacket/packet.h>
@@ -178,7 +179,20 @@ namespace usrp2 {
     
     return len;
   }
-  
+
+  int
+  ethernet::read_packet_timeout (void *buf, int buflen, int timeout_in_ms)
+  {
+      struct pollfd pfd;
+      pfd.fd = d_fd;
+      pfd.revents = 0;
+      pfd.events = POLLIN;
+
+      poll(&pfd, 1, timeout_in_ms);
+
+      return read_packet_dont_block(buf, buflen);
+  }
+
   int
   ethernet::write_packet (const void *buf, int buflen)
   {
diff --git a/usrp2/host/lib/ethernet.h b/usrp2/host/lib/ethernet.h
index 24624f4..7fcb0eb 100644
--- a/usrp2/host/lib/ethernet.h
+++ b/usrp2/host/lib/ethernet.h
@@ -92,7 +92,20 @@ namespace usrp2 {
      * Returned packet includes 14-byte ethhdr
      */
     int read_packet_dont_block (void *buf, int buflen);
-    
+
+    /*!
+     * \brief Read packet from interface, but with timeout
+     *
+     * \param buf              where to put the packet
+     * \param buflen   maximum length of packet in bytes (should be >= 1528)
+     * \param timeout_in_ms   the timeout in milliseconds
+     *
+     * \returns number of bytes read, -1 if trouble or 0 if nothing available.
+     *
+     * Returned packet includes 14-byte ethhdr
+     */
+    int read_packet_timeout (void *buf, int buflen, int timeout_in_ms);
+
     /*
      * \brief Write ethernet packet to interface.
      *
diff --git a/usrp2/host/lib/find.cc b/usrp2/host/lib/find.cc
index a1c8a35..f8661d1 100644
--- a/usrp2/host/lib/find.cc
+++ b/usrp2/host/lib/find.cc
@@ -32,9 +32,6 @@
 
 #define FIND_DEBUG false
 
-#define TRANSPORT_RECV_TIMEOUT 0.01 //10ms
-#define FIND_RESPONSE_TIMEOUT 0.05 //50ms
-
 static const u2_mac_addr_t broadcast_mac_addr =
       {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }};
 
@@ -49,7 +46,7 @@ namespace usrp2{
         typedef boost::shared_ptr<find_helper> sptr;
 
         find_helper(const std::string &ifc, const std::string &addr): 
d_target_addr(addr){
-            d_ctrl_transport = transport::sptr(new eth_ctrl_transport(ifc, 
broadcast_mac_addr, TRANSPORT_RECV_TIMEOUT, false));
+            d_ctrl_transport = transport::sptr(new eth_ctrl_transport(ifc, 
broadcast_mac_addr, false));
             
d_ctrl_transport->set_callback(boost::bind(&find_helper::handle_control_packet, 
this, _1));
         }
 
@@ -67,7 +64,7 @@ namespace usrp2{
             d_ctrl_transport->sendv(&iov, 1);
             //allow responses to gather
             d_ctrl_transport->start();
-            
boost::this_thread::sleep(gruel::get_new_timeout(FIND_RESPONSE_TIMEOUT));
+            boost::this_thread::sleep(gruel::get_new_timeout(0.05)); //50ms
             d_ctrl_transport->stop();
             return d_result;
         }
diff --git a/usrp2/host/lib/transport.cc b/usrp2/host/lib/transport.cc
index b410f2a..028f892 100644
--- a/usrp2/host/lib/transport.cc
+++ b/usrp2/host/lib/transport.cc
@@ -61,7 +61,7 @@ void usrp2::transport::run(){
             // call recv to get a new sbuffer
             // pass the buffer into the callback
             sbuff_vec_t sbs = recv();
-            if (d_running and sbs.size()) d_cb(sbs);
+            if (sbs.size()) d_cb(sbs);
         //catch thread interrupts, possibly from stop
         //the running condition will be re-checked
         }catch(boost::thread_interrupted const &){}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]