giantsd/dpnet/packet.py
2019-02-15 01:49:34 +01:00

51 lines
1.2 KiB
Python

import io
import struct
class Packet(io.BytesIO):
def putByte(self, val):
self.write(struct.pack('<B', val % 256))
def getByte(self):
return struct.unpack('<B', self.read(1))[0]
def putBytes(self, bytes):
for byte in bytes:
self.write(struct.pack('<B', byte))
def getBytes(self, num):
return bytes(struct.unpack('<'+str(num)+'B', self.read(num)))
def putShort(self, val):
self.write(struct.pack('<H', val))
def getShort(self):
return struct.unpack('<H', self.read(2))[0]
def putLong(self, val):
self.write(struct.pack('<l', val))
def putULong(self, val):
self.write(struct.pack('<L', val))
def getLong(self):
return struct.unpack('<l', self.read(4))[0]
def getULong(self):
return struct.unpack('<L', self.read(4))[0]
def putLongLong(self, val):
self.write(struct.pack('<Q', val))
def getLongLong(self):
return struct.unpack('<Q', self.read(8))[0]
def putFloat(self, val):
self.write(struct.pack('<f', val))
def getFloat(self):
return struct.unpack('<f', self.read(4))[0]
def putString(self, val):
self.write(val + '\x00')