def write_data(self, data): """ Write raw data to transport @raise TransportInitialiationError: could not be initialized @raise TransportTerminated: no longer open for publishing """ if not self.socket: raise TransportInitError( "TCPROS transport was not successfully initialized") if self.done: raise TransportTerminated("connection closed") try: #TODO: get rid of sendalls and replace with async-style publishing self.socket.sendall(data.encode()) self.stat_bytes += len(data) self.stat_num_msg += 1 except IOError as xxx_todo_changeme: #for now, just document common errno's in code (errno, msg) = xxx_todo_changeme.args #for now, just document common errno's in code if errno == 32: #broken pipe logdebug("ERROR: Broken Pipe") self.close() raise TransportTerminated(str(errno) + msg) raise #re-raise except socket.error as xxx_todo_changeme1: #for now, just document common errno's in code (errno, msg) = xxx_todo_changeme1.args #for now, just document common errno's in code if errno == 32: #broken pipe logdebug("[%s]: Closing connection [%s] due to broken pipe", self.name, self.endpoint_id) self.close() raise TransportTerminated(msg) elif errno == 104: #connection reset by peer logdebug("[%s]: Peer [%s] has closed connection", self.name, self.endpoint_id) self.close() raise TransportTerminated(msg) else: rospydebug("unknown socket error writing data: %s", traceback.format_exc()) logdebug( "[%s]: closing connection [%s] due to unknown socket error: %s", self.name, self.endpoint_id, msg) self.close() raise TransportTerminated(str(errno) + ' ' + msg) #except: #TODO: try to figure out common errors here #raise return True
def write_data(self, data): """ Write raw data to transport @raise TransportInitialiationError: could not be initialized @raise TransportTerminated: no longer open for publishing """ if not self.socket: raise TransportInitError( "TCPROS transport was not successfully initialized") if self.done: raise TransportTerminated("connection closed") try: #TODO: get rid of sendalls and replace with async-style publishing self.socket.sendall(data) self.stat_bytes += len(data) self.stat_num_msg += 1 except IOError, (errno, msg): #for now, just document common errno's in code if errno == 32: #broken pipe logdebug("ERROR: Broken Pipe") self.close() raise TransportTerminated(str(errno) + msg) raise #re-raise
def recv_buff(sock, b, buff_size): """ Read data from socket into buffer. @param sock: socket to read from @type sock: socket.socket @param b: buffer to receive into @type b: StringIO @param buff_size: recv read size @type buff_size: int @return: number of bytes read @rtype: int """ try: d = sock.recv(buff_size) if d: b.write(d) return len(d) else: #bomb out raise TransportTerminated("unable to receive data from sender, check sender's logs for details") except socket.error as ex: if ex.errno not in (EAGAIN, EWOULDBLOCK): raise TransportTerminated("unable to receive data from sender, check sender's logs for details") else: return 0
self.stat_bytes += len(data) self.stat_num_msg += 1 except IOError, (errno, msg): #for now, just document common errno's in code if errno == 32: #broken pipe logdebug("ERROR: Broken Pipe") self.close() raise TransportTerminated(str(errno) + msg) raise #re-raise except socket.error, (errno, msg): #for now, just document common errno's in code if errno == 32: #broken pipe logdebug("[%s]: Closing connection [%s] due to broken pipe", self.name, self.endpoint_id) self.close() raise TransportTerminated(msg) elif errno == 104: #connection reset by peer logdebug("[%s]: Peer [%s] has closed connection", self.name, self.endpoint_id) self.close() raise TransportTerminated(msg) else: rospydebug("unknown socket error writing data: %s", traceback.format_exc()) logdebug( "[%s]: closing connection [%s] due to unknown socket error: %s", self.name, self.endpoint_id, msg) self.close() raise TransportTerminated(str(errno) + ' ' + msg) except: #TODO: try to figure out common errors here