Initial work on implementing a network mode for communication with a server program.
This commit is contained in:
49
lib/gtpsocket.py
Normal file
49
lib/gtpsocket.py
Normal file
@ -0,0 +1,49 @@
|
||||
# A socket class for communicating in Go Text Protocol
|
||||
#
|
||||
# A simple connect method is defined, and everything else is
|
||||
# suffixed with _gtp, so that this can be subclassed easily,
|
||||
# e.g. for communicating with a server
|
||||
|
||||
import socket
|
||||
|
||||
class GTPSocket:
|
||||
def __init__(self, info):
|
||||
self.info = info
|
||||
self.socket = None
|
||||
|
||||
|
||||
def connect(self):
|
||||
try:
|
||||
self.socket = socket.create_connection(info)
|
||||
return True
|
||||
except:
|
||||
return False
|
||||
|
||||
|
||||
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
|
Reference in New Issue
Block a user