9 from threading
import Thread, Event
13 super(SocketTransport, self).
__init__()
30 super(SocketTransport, self).
start()
31 rospy.loginfo(
"[%s] Started" %
type(self).__name__)
34 if self.
alive is not None:
39 super(SocketTransport, self).
join()
40 rospy.loginfo(
"[%s] Stopped" %
type(self).__name__)
44 while not self.alive.wait(self.
poll_rate):
46 self.socket.settimeout(1)
48 except socket.error
as e:
49 if e.message ==
"timed out":
53 parsed_data, parsed_length = self.
parse(recvbuf)
54 for data
in parsed_data:
57 if len(recvbuf) > parsed_length:
58 recvbuf = recvbuf[parsed_length:]
63 except RuntimeError
as e:
64 rospy.logerr(
"Failed to parse data: %s" % str(e))
70 self.socket.getpeername()
72 except socket.error
as e:
86 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
88 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
89 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
91 rospy.loginfo(
"[%s] Connected to server %s",
type(self).__name__, info)
94 except socket.error
as e:
100 raise IOError(
"[%s] Failed to connect to %s after %d retries: %s" % (
type(self).__name__, info, retry, str(err)))
105 rospy.loginfo(
"[%s] Disconnected",
type(self).__name__)
114 self.socket.sendall(data)
117 except AttributeError:
121 self.callbacks.append(cb)
124 raise NotImplementedError()
127 if __name__ ==
'__main__':
def __init__(self, host, port, max_retry)
def on_received_data(self, cb)