2017-07-29 09:54:03 +00:00
|
|
|
import logging
|
2017-05-25 10:47:01 +00:00
|
|
|
import struct
|
2017-07-30 12:08:18 +00:00
|
|
|
from asyncio import Event, Queue
|
2017-05-25 10:47:01 +00:00
|
|
|
from enum import Enum
|
2017-07-29 09:54:03 +00:00
|
|
|
from time import time
|
2018-05-06 12:55:00 +00:00
|
|
|
from math import ceil
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
import simplejson
|
2017-08-01 20:34:06 +00:00
|
|
|
|
|
|
|
from secret_handshake import SHSClient, SHSServer
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
logger = logging.getLogger('packet_stream')
|
|
|
|
|
|
|
|
|
2017-05-25 10:47:01 +00:00
|
|
|
class PSMessageType(Enum):
|
|
|
|
BUFFER = 0
|
|
|
|
TEXT = 1
|
|
|
|
JSON = 2
|
|
|
|
|
|
|
|
|
2017-07-29 09:54:03 +00:00
|
|
|
class PSStreamHandler(object):
|
|
|
|
def __init__(self, req):
|
|
|
|
super(PSStreamHandler).__init__()
|
|
|
|
self.req = req
|
|
|
|
self.queue = Queue()
|
|
|
|
|
|
|
|
async def process(self, msg):
|
|
|
|
await self.queue.put(msg)
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
await self.queue.put(None)
|
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
def __aiter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def __anext__(self):
|
|
|
|
elem = await self.queue.get()
|
|
|
|
|
|
|
|
if not elem:
|
|
|
|
raise StopAsyncIteration()
|
|
|
|
|
|
|
|
return elem
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
class PSRequestHandler(object):
|
|
|
|
def __init__(self, req):
|
|
|
|
super(PSRequestHandler).__init__()
|
|
|
|
self.req = req
|
2017-07-30 12:08:18 +00:00
|
|
|
self.event = Event()
|
2017-07-30 08:14:26 +00:00
|
|
|
self._msg = None
|
|
|
|
|
|
|
|
async def process(self, msg):
|
|
|
|
self._msg = msg
|
2017-07-30 12:08:18 +00:00
|
|
|
self.event.set()
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
async def stop(self):
|
2017-07-30 12:08:18 +00:00
|
|
|
if not self.event.is_set():
|
|
|
|
self.event.set()
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
def __await__(self):
|
2017-07-30 12:08:18 +00:00
|
|
|
# wait until 'process' is called
|
2018-10-25 16:39:39 +00:00
|
|
|
yield from self.event.wait().__await__()
|
2017-07-30 08:14:26 +00:00
|
|
|
return self._msg
|
|
|
|
|
|
|
|
|
2017-05-25 10:47:01 +00:00
|
|
|
class PSMessage(object):
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
@classmethod
|
2017-07-30 09:51:11 +00:00
|
|
|
def from_header_body(cls, flags, req, body):
|
2017-07-29 09:54:03 +00:00
|
|
|
type_ = PSMessageType(flags & 0x03)
|
|
|
|
|
|
|
|
if type_ == PSMessageType.TEXT:
|
|
|
|
body = body.decode('utf-8')
|
|
|
|
elif type_ == PSMessageType.JSON:
|
|
|
|
body = simplejson.loads(body)
|
|
|
|
|
|
|
|
return cls(type_, body, bool(flags & 0x08), bool(flags & 0x04), req=req)
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
if self.type == PSMessageType.TEXT:
|
2017-07-29 09:54:03 +00:00
|
|
|
return self.body.encode('utf-8')
|
2017-05-25 10:47:01 +00:00
|
|
|
elif self.type == PSMessageType.JSON:
|
2017-07-30 08:14:26 +00:00
|
|
|
return simplejson.dumps(self.body).encode('utf-8')
|
|
|
|
return self.body
|
2017-07-29 09:54:03 +00:00
|
|
|
|
|
|
|
def __init__(self, type_, body, stream, end_err, req=None):
|
|
|
|
self.stream = stream
|
|
|
|
self.end_err = end_err
|
|
|
|
self.type = type_
|
|
|
|
self.body = body
|
|
|
|
self.req = req
|
2017-05-25 10:47:01 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2017-07-30 09:51:11 +00:00
|
|
|
if self.type == PSMessageType.BUFFER:
|
|
|
|
body = '{} bytes'.format(len(self.body))
|
|
|
|
else:
|
|
|
|
body = self.body
|
|
|
|
return '<PSMessage ({}): {}{} {}{}>'.format(self.type.name, body,
|
2017-07-29 09:54:03 +00:00
|
|
|
'' if self.req is None else ' [{}]'.format(self.req),
|
|
|
|
'~' if self.stream else '', '!' if self.end_err else '')
|
|
|
|
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
class PacketStream(object):
|
|
|
|
def __init__(self, connection):
|
|
|
|
self.connection = connection
|
2017-07-29 09:54:03 +00:00
|
|
|
self.req_counter = 1
|
2018-02-04 21:18:36 +00:00
|
|
|
self._event_map = {}
|
2017-07-30 12:08:18 +00:00
|
|
|
|
2018-02-04 21:18:36 +00:00
|
|
|
def register_handler(self, handler):
|
|
|
|
self._event_map[handler.req] = (time(), handler)
|
2017-07-30 12:08:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_connected(self):
|
2018-02-04 21:18:36 +00:00
|
|
|
return self.connection.is_connected
|
|
|
|
|
2023-11-01 05:03:06 +00:00
|
|
|
def __aiter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
async def __anext__(self):
|
|
|
|
msg = await self.read()
|
|
|
|
|
|
|
|
if not msg:
|
|
|
|
raise StopAsyncIteration()
|
|
|
|
|
|
|
|
if msg.req >= 0:
|
|
|
|
return msg
|
|
|
|
|
|
|
|
return None
|
2018-02-04 21:18:36 +00:00
|
|
|
|
|
|
|
async def __await__(self):
|
|
|
|
async for data in self:
|
|
|
|
logger.info('RECV: %r', data)
|
|
|
|
if data is None:
|
|
|
|
return
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-07-30 20:28:42 +00:00
|
|
|
async def _read(self):
|
2017-06-04 22:27:44 +00:00
|
|
|
try:
|
|
|
|
header = await self.connection.read()
|
2018-05-15 19:40:42 +00:00
|
|
|
if not header or header == b'\x00' * 9:
|
2017-06-04 22:27:44 +00:00
|
|
|
return
|
2017-07-30 09:51:11 +00:00
|
|
|
flags, length, req = struct.unpack('>BIi', header)
|
|
|
|
|
2018-05-06 12:55:00 +00:00
|
|
|
n_packets = ceil(length / 4096)
|
2017-07-30 09:51:11 +00:00
|
|
|
|
|
|
|
body = b''
|
|
|
|
for n in range(n_packets):
|
|
|
|
body += await self.connection.read()
|
|
|
|
|
|
|
|
logger.debug('READ %s %s', header, len(body))
|
|
|
|
return PSMessage.from_header_body(flags, req, body)
|
2017-06-04 22:27:44 +00:00
|
|
|
except StopAsyncIteration:
|
2017-07-29 09:54:03 +00:00
|
|
|
logger.debug('DISCONNECT')
|
2017-07-31 21:15:52 +00:00
|
|
|
self.connection.disconnect()
|
2017-06-04 22:27:44 +00:00
|
|
|
return None
|
|
|
|
|
2017-07-30 20:28:42 +00:00
|
|
|
async def read(self):
|
|
|
|
msg = await self._read()
|
|
|
|
if not msg:
|
|
|
|
return None
|
|
|
|
# check whether it's a reply and handle accordingly
|
|
|
|
if msg.req < 0:
|
|
|
|
t, handler = self._event_map[-msg.req]
|
|
|
|
await handler.process(msg)
|
|
|
|
logger.info('RESPONSE [%d]: %r', -msg.req, msg)
|
|
|
|
if msg.end_err:
|
|
|
|
await handler.stop()
|
|
|
|
del self._event_map[-msg.req]
|
|
|
|
logger.info('RESPONSE [%d]: EOS', -msg.req)
|
|
|
|
return msg
|
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
def _write(self, msg):
|
|
|
|
logger.info('SEND [%d]: %r', msg.req, msg)
|
2017-07-29 09:54:03 +00:00
|
|
|
header = struct.pack('>BIi', (int(msg.stream) << 3) | (int(msg.end_err) << 2) | msg.type.value, len(msg.data),
|
|
|
|
msg.req)
|
2017-06-04 22:27:44 +00:00
|
|
|
self.connection.write(header)
|
2017-07-30 08:14:26 +00:00
|
|
|
self.connection.write(msg.data)
|
|
|
|
logger.debug('WRITE HDR: %s', header)
|
|
|
|
logger.debug('WRITE DATA: %s', msg.data)
|
2017-07-29 09:54:03 +00:00
|
|
|
|
2017-07-30 08:14:26 +00:00
|
|
|
def send(self, data, msg_type=PSMessageType.JSON, stream=False, end_err=False, req=None):
|
|
|
|
update_counter = False
|
|
|
|
if req is None:
|
|
|
|
update_counter = True
|
|
|
|
req = self.req_counter
|
|
|
|
|
|
|
|
msg = PSMessage(msg_type, data, stream=stream, end_err=end_err, req=req)
|
|
|
|
|
|
|
|
# send request
|
|
|
|
self._write(msg)
|
|
|
|
|
|
|
|
if stream:
|
|
|
|
handler = PSStreamHandler(self.req_counter)
|
|
|
|
else:
|
|
|
|
handler = PSRequestHandler(self.req_counter)
|
2017-07-29 09:54:03 +00:00
|
|
|
self.register_handler(handler)
|
2017-07-30 08:14:26 +00:00
|
|
|
|
|
|
|
if update_counter:
|
|
|
|
self.req_counter += 1
|
2017-07-29 09:54:03 +00:00
|
|
|
return handler
|
2017-05-25 10:47:01 +00:00
|
|
|
|
2017-07-30 12:08:18 +00:00
|
|
|
def disconnect(self):
|
|
|
|
self._connected = False
|
|
|
|
self.connection.disconnect()
|