Votes Internally!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

6 years ago
6 years ago
  1. import struct
  2. import socket
  3. from io import BytesIO as StringIO
  4. class Packet(object):
  5. def __init__(self, ival = None):
  6. if ival is not None:
  7. self.buffer = StringIO(ival)
  8. else:
  9. self.buffer = StringIO()
  10. # Staticmethod
  11. def _reader(fmt):
  12. return lambda self: struct.unpack(fmt, self.buffer.read(struct.calcsize(fmt)))[0]
  13. # Staticmethod
  14. def _writer(fmt):
  15. return lambda self, obj: self.buffer.write(struct.pack(fmt, obj))
  16. read_byte = _reader('!b')
  17. read_ubyte = _reader('!B')
  18. read_short = _reader('!h')
  19. read_ushort = _reader('!H')
  20. read_int = _reader('!i')
  21. read_uint = _reader('!I')
  22. read_long = _reader('!l')
  23. read_ulong = _reader('!L')
  24. read_float = _reader('!f')
  25. read_double = _reader('!d')
  26. write_byte = _writer('!b')
  27. write_ubyte = _writer('!B')
  28. write_short = _writer('!h')
  29. write_ushort = _writer('!H')
  30. write_int = _writer('!i')
  31. write_uint = _writer('!I')
  32. write_long = _writer('!l')
  33. write_ulong = _writer('!L')
  34. write_float = _writer('!f')
  35. write_double = _writer('!d')
  36. def __bytes__(self):
  37. return self.buffer.getvalue()
  38. class CMD:
  39. KEEPALIVE = 0
  40. MOTION = 1
  41. SOUND = 2