ros-industrial / swri-ros-pkg

Automatically exported from code.google.com/p/swri-ros-pkg
7 stars 9 forks source link

simple_socket may fail if packets are split #23

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The SimpleSocket class uses a single call to RECV() to read data from a socket. 
 In some cases, a single call may not be sufficient to retrieve all requested 
data.  The socket communications are likely to fail in undefined ways if the 
full set of expected message data is not read.

Technically, the recv() call will return when *some* data is available on the 
socket input buffer.  The call specifies a maximum amount of data to read from 
the socket, but the call may return with fewer bytes read.  

Usually, these short messages will arrive in a single packet and should not 
cause issues.  But there is no guarantee.  A message could be split into 
multiple packets by the OS or intermediary equipment, potentially leading to 
the issue described above.

An easy solution would be to patch SimpleSocket::receiveBytes to retry the 
rawReceive bytes call until the expected number of bytes were read.  Repeat 
calls should decrement the number of requested bytes to account for bytes 
already received.  The receiveBytes function should also include a 
timeout/error mechanism, in case the full quantity of bytes is never received.  
Note that motoplus does not support the SO_RCVTIMEO socket option, so a 
select-based mechanism may be required.

See the link below for more discussion on split socket packets:
http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1108255660&id=1044780608

Original issue reported on code.google.com by jz...@swri.org on 16 Oct 2012 at 3:09

GoogleCodeExporter commented 9 years ago
See here: https://github.com/ros-industrial/industrial_core/pull/33

Original comment by shaun.ed...@gmail.com on 6 Sep 2013 at 2:45