UDP communication with SIM!

This commit is contained in:
locklainn.linden
2008-08-07 20:02:45 +00:00
committed by Salad Dais
parent 4c3229031e
commit 4b83cf89f5
3 changed files with 90 additions and 81 deletions

View File

@@ -4,61 +4,63 @@ from pyogp.lib.base.message.message_types import MsgType, EndianType
class DataPacker(object):
def __init__(self):
self.endian = EndianType.BIG
self.packer = {}
self.packer[MsgType.MVT_VARIABLE] = self.__pack_string
self.packer[MsgType.MVT_U8] = 'B'
self.packer[MsgType.MVT_U16] = 'H'
self.packer[MsgType.MVT_U32] = 'I'
self.packer[MsgType.MVT_U64] = 'Q'
self.packer[MsgType.MVT_S8] = 'b'
self.packer[MsgType.MVT_S16] = 'h'
self.packer[MsgType.MVT_S32] = 'i'
self.packer[MsgType.MVT_S64] = 'q'
self.packer[MsgType.MVT_F32] = 'f'
self.packer[MsgType.MVT_F64] = 'd'
self.packer[MsgType.MVT_LLVector3] = self.__pack_vector3
self.packer[MsgType.MVT_LLVector3d] = self.__pack_vector3d
self.packer[MsgType.MVT_LLVector4] = self.__pack_vector4
self.packer[MsgType.MVT_LLQuaternion] = self.__pack_quat
self.packer[MsgType.MVT_LLUUID] = self.__pack_uuid
self.packer[MsgType.MVT_BOOL] = 'B'
self.packer[MsgType.MVT_IP_ADDR] = self.__pack_string
self.packer[MsgType.MVT_IP_PORT] = 'H'
self.packer[MsgType.MVT_VARIABLE] = ('>', self.__pack_string)
self.packer[MsgType.MVT_S8] = ('>', 'b')
self.packer[MsgType.MVT_U8] = ('>','B')
self.packer[MsgType.MVT_BOOL] = ('>','B')
self.packer[MsgType.MVT_LLUUID] = ('>',self.__pack_uuid)
self.packer[MsgType.MVT_IP_ADDR] = ('>',self.__pack_string)
self.packer[MsgType.MVT_IP_PORT] = ('>','H')
self.packer[MsgType.MVT_U16] = ('<','H')
self.packer[MsgType.MVT_U32] = ('<','I')
self.packer[MsgType.MVT_U64] = ('<','Q')
self.packer[MsgType.MVT_S16] = ('<','h')
self.packer[MsgType.MVT_S32] = ('<','i')
self.packer[MsgType.MVT_S64] = ('<','q')
self.packer[MsgType.MVT_F32] = ('<','f')
self.packer[MsgType.MVT_F64] = ('<','d')
self.packer[MsgType.MVT_LLVector3] = ('<',self.__pack_vector3)
self.packer[MsgType.MVT_LLVector3d] = ('<',self.__pack_vector3d)
self.packer[MsgType.MVT_LLVector4] = ('<',self.__pack_vector4)
self.packer[MsgType.MVT_LLQuaternion] = ('<',self.__pack_quat)
def pack_data(self, data, data_type, endian_type=EndianType.NONE):
if endian_type != EndianType.NONE:
self.endian = endian_type
if data_type in self.packer:
pack = self.packer[data_type]
pack_tup = self.packer[data_type]
endian = pack_tup[0]
#override endian
if endian_type != EndianType.NONE:
endian = endian_type
pack = pack_tup[1]
if callable(pack):
return pack(data)
return pack(endian, data)
else:
return struct.pack(self.endian + pack, data)
return struct.pack(endian + pack, data)
return None
def __pack_tuple(self, tup, tp):
def __pack_tuple(self, endian, tup, tp):
size = len(tup)
return struct.pack(self.endian + str(size) + tp, *tup)
return struct.pack(endian + str(size) + tp, *tup)
def __pack_vector3(self, vec):
return __pack_tuple(vec, 'f')
def __pack_vector3(self, endian, vec):
return __pack_tuple(endian, vec, 'f')
def __pack_vector3d(self, vec):
return __pack_tuple(vec, 'd')
def __pack_vector3d(self, endian, vec):
return __pack_tuple(endian,vec, 'd')
def __pack_vector4(self, vec):
return __pack_tuple(vec, 'f')
def __pack_vector4(self, endian, vec):
return __pack_tuple(endian, vec, 'f')
def __pack_quat(self, quat):
def __pack_quat(self, endian, quat):
#first, pack to vector3
vec = quat_to_vec3(quat)
return __pack_vector3(vec)
return __pack_vector3(vendian, ec)
def __pack_uuid(self, uuid):
def __pack_uuid(self, endian, uuid):
return uuid.bytes
def __pack_string(self, pack_string):
def __pack_string(self, endian, pack_string):
return pack_string

View File

@@ -5,33 +5,29 @@ from pyogp.lib.base.message.message_types import MsgType, EndianType, sizeof
class DataUnpacker(object):
def __init__(self):
self.endian = EndianType.BIG
self.unpacker = {}
self.unpacker[MsgType.MVT_VARIABLE] = self.__unpack_string
self.unpacker[MsgType.MVT_U8] = 'B'
self.unpacker[MsgType.MVT_U16] = 'H'
self.unpacker[MsgType.MVT_U32] = 'I'
self.unpacker[MsgType.MVT_U64] = 'Q'
self.unpacker[MsgType.MVT_S8] = 'b'
self.unpacker[MsgType.MVT_S16] = 'h'
self.unpacker[MsgType.MVT_S32] = 'i'
self.unpacker[MsgType.MVT_S64] = 'q'
self.unpacker[MsgType.MVT_F32] = 'f'
self.unpacker[MsgType.MVT_F64] = 'd'
self.unpacker[MsgType.MVT_LLVector3] = self.__unpack_vector3
self.unpacker[MsgType.MVT_LLVector3d] = self.__unpack_vector3d
self.unpacker[MsgType.MVT_LLVector4] = self.__unpack_vector4
self.unpacker[MsgType.MVT_LLQuaternion] = self.__unpack_quat
self.unpacker[MsgType.MVT_LLUUID] = self.__unpack_uuid
self.unpacker[MsgType.MVT_BOOL] = 'B'
self.unpacker[MsgType.MVT_IP_ADDR] = self.__unpack_string
self.unpacker[MsgType.MVT_IP_PORT] = 'H'
self.unpacker[MsgType.MVT_VARIABLE] = ('>', self.__unpack_string)
self.unpacker[MsgType.MVT_S8] = ('>', 'b')
self.unpacker[MsgType.MVT_U8] = ('>','B')
self.unpacker[MsgType.MVT_BOOL] = ('>','B')
self.unpacker[MsgType.MVT_LLUUID] = ('>',self.__unpack_uuid)
self.unpacker[MsgType.MVT_IP_ADDR] = ('>',self.__unpack_string)
self.unpacker[MsgType.MVT_IP_PORT] = ('>','H')
self.unpacker[MsgType.MVT_U16] = ('<','H')
self.unpacker[MsgType.MVT_U32] = ('<','I')
self.unpacker[MsgType.MVT_U64] = ('<','Q')
self.unpacker[MsgType.MVT_S16] = ('<','h')
self.unpacker[MsgType.MVT_S32] = ('<','i')
self.unpacker[MsgType.MVT_S64] = ('<','q')
self.unpacker[MsgType.MVT_F32] = ('<','f')
self.unpacker[MsgType.MVT_F64] = ('<','d')
self.unpacker[MsgType.MVT_LLVector3] = ('<',self.__unpack_vector3)
self.unpacker[MsgType.MVT_LLVector3d] = ('<',self.__unpack_vector3d)
self.unpacker[MsgType.MVT_LLVector4] = ('<',self.__unpack_vector4)
self.unpacker[MsgType.MVT_LLQuaternion] = ('<',self.__unpack_quat)
def unpack_data(self, data, data_type, start_index=0, \
var_size=0, endian_type=EndianType.NONE):
if endian_type != EndianType.NONE:
self.endian = endian_type
if start_index != 0:
if var_size != 0:
data = data[start_index:start_index+var_size]
@@ -39,34 +35,41 @@ class DataUnpacker(object):
data = data[start_index:start_index+sizeof(data_type)]
if data_type in self.unpacker:
unpack = self.unpacker[data_type]
unpack_tup = self.unpacker[data_type]
endian = unpack_tup[0]
#override endian
if endian_type != EndianType.NONE:
endian = endian_type
unpack = unpack_tup[1]
if callable(unpack):
return unpack(data)
return unpack(endian, data)
else:
return struct.unpack(self.endian + unpack, data)[0]
return struct.unpack(endian + unpack, data)[0]
return None
def __unpack_tuple(self, tup, tp):
def __unpack_tuple(self, endian, tup, tp):
size = len(tup) / struct.calcsize(tp)
return struct.unpack(self.endian + str(size) + tp, tup)
return struct.unpack(endian + str(size) + tp, tup)
def __unpack_vector3(self, vec):
return __unpack_tuple(vec, 'f')
def __unpack_vector3(self, endian, vec):
return self.__unpack_tuple(endian, vec, 'f')
def __unpack_vector3d(self, vec):
return __unpack_tuple(vec, 'd')
def __unpack_vector3d(self, endian, vec):
return self.__unpack_tuple(endian, vec, 'd')
def __unpack_vector4(self, vec):
return __unpack_tuple(vec, 'f')
def __unpack_vector4(self, endian, vec):
return self.__unpack_tuple(endian, vec, 'f')
def __unpack_quat(self, quat):
def __unpack_quat(self, endian, quat):
#first, pack to vector3
print "WARNING: UNPACKING A QUAT...."
vec = quat_to_vec3(quat)
return __unpack_vector3(vec)
return self.__unpack_vector3(endian, vec)
def __unpack_uuid(self, uuid_data):
def __unpack_uuid(self, endian, uuid_data):
return UUID(bytes=uuid_data)
def __unpack_string(self, pack_string):
def __unpack_string(self, endian, pack_string):
return pack_string

View File

@@ -10,10 +10,10 @@ class NetUDPClient(object):
implements(IUDPClient)
def __init__(self):
self.sender = None
self.sender = Host(None, None)
def get_sender(self):
return Host(self.sender.host, self.sender.port)
return self.sender
def send_packet(self, sock, send_buffer, host):
print 'Sending to: ' + str(host.ip) + ":" + str(host.port)
@@ -21,9 +21,13 @@ class NetUDPClient(object):
def receive_packet(self, sock):
buf = 10000
data, addr = sock.recvfrom(buf)
self.sender.ip_addr = addr
self.sender.port()
try:
data, addr = sock.recvfrom(buf)
print "Received data: " + repr(data)
except:
return '', 0
self.sender.ip = addr[0]
self.sender.port = addr[1]
return data, len(data)
def start_udp_connection(self, port):