2012-04-15 03:47:55 +00:00
|
|
|
# A class for communicating in Go Text Protocol
|
2012-04-15 03:42:56 +00:00
|
|
|
#
|
2012-04-15 03:47:55 +00:00
|
|
|
# An already-connected socket should be passed in, then this class
|
|
|
|
# can be used to handle GTP simply
|
2012-04-15 03:42:56 +00:00
|
|
|
|
|
|
|
class GTPSocket:
|
2012-04-15 03:47:55 +00:00
|
|
|
def __init__(self, socket):
|
2012-04-15 03:42:56 +00:00
|
|
|
self.socket = None
|
2012-04-15 03:47:55 +00:00
|
|
|
self.id = None
|
2012-04-15 03:42:56 +00:00
|
|
|
|
|
|
|
def get_gtp(self):
|
|
|
|
msg = None
|
|
|
|
|
|
|
|
while msg is None:
|
|
|
|
try:
|
|
|
|
msg = self.socket.recv(1024)
|
|
|
|
if not self._validate_gtp(msg):
|
|
|
|
print 'Error: Incoming data was not a valid GTP message'
|
|
|
|
msg = None
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def send_gtp(self, msg):
|
|
|
|
try:
|
|
|
|
if _validate_gtp(msg):
|
|
|
|
self.socket.send(msg)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
print 'Error: Outgoing data was not a valid GTP message'
|
|
|
|
return False
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _validate_gtp(self, gtp):
|
|
|
|
return True
|