|
|
|
@ -18,28 +18,34 @@
|
|
|
|
|
|
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
|
|
import struct
|
|
|
|
|
from io import BytesIO
|
|
|
|
|
from . import messages as proto
|
|
|
|
|
import logging
|
|
|
|
|
import struct
|
|
|
|
|
from typing import Tuple
|
|
|
|
|
|
|
|
|
|
from . import mapping
|
|
|
|
|
from . import protobuf
|
|
|
|
|
from .transport import Transport
|
|
|
|
|
|
|
|
|
|
REPLEN = 64
|
|
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProtocolV2(object):
|
|
|
|
|
class ProtocolV2:
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self.session = None
|
|
|
|
|
|
|
|
|
|
def session_begin(self, transport):
|
|
|
|
|
def session_begin(self, transport: Transport) -> None:
|
|
|
|
|
chunk = struct.pack('>B', 0x03)
|
|
|
|
|
chunk = chunk.ljust(REPLEN, b'\x00')
|
|
|
|
|
transport.write_chunk(chunk)
|
|
|
|
|
resp = transport.read_chunk()
|
|
|
|
|
self.session = self.parse_session_open(resp)
|
|
|
|
|
LOG.debug("[session {}] session started".format(self.session))
|
|
|
|
|
|
|
|
|
|
def session_end(self, transport):
|
|
|
|
|
def session_end(self, transport: Transport) -> None:
|
|
|
|
|
if not self.session:
|
|
|
|
|
return
|
|
|
|
|
chunk = struct.pack('>BL', 0x04, self.session)
|
|
|
|
@ -49,12 +55,15 @@ class ProtocolV2(object):
|
|
|
|
|
(magic, ) = struct.unpack('>B', resp[:1])
|
|
|
|
|
if magic != 0x04:
|
|
|
|
|
raise RuntimeError('Expected session close')
|
|
|
|
|
LOG.debug("[session {}] session ended".format(self.session))
|
|
|
|
|
self.session = None
|
|
|
|
|
|
|
|
|
|
def write(self, transport, msg):
|
|
|
|
|
def write(self, transport: Transport, msg: protobuf.MessageType) -> None:
|
|
|
|
|
if not self.session:
|
|
|
|
|
raise RuntimeError('Missing session for v2 protocol')
|
|
|
|
|
|
|
|
|
|
LOG.debug("[session {}] sending message: {}".format(self.session, msg.__class__.__name__),
|
|
|
|
|
extra={'protobuf': msg})
|
|
|
|
|
# Serialize whole message
|
|
|
|
|
data = BytesIO()
|
|
|
|
|
protobuf.dump_message(data, msg)
|
|
|
|
@ -76,7 +85,7 @@ class ProtocolV2(object):
|
|
|
|
|
data = data[datalen:]
|
|
|
|
|
seq += 1
|
|
|
|
|
|
|
|
|
|
def read(self, transport):
|
|
|
|
|
def read(self, transport: Transport) -> protobuf.MessageType:
|
|
|
|
|
if not self.session:
|
|
|
|
|
raise RuntimeError('Missing session for v2 protocol')
|
|
|
|
|
|
|
|
|
@ -95,12 +104,14 @@ class ProtocolV2(object):
|
|
|
|
|
|
|
|
|
|
# Parse to protobuf
|
|
|
|
|
msg = protobuf.load_message(data, mapping.get_class(msg_type))
|
|
|
|
|
LOG.debug("[session {}] received message: {}".format(self.session, msg.__class__.__name__),
|
|
|
|
|
extra={'protobuf': msg})
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
def parse_first(self, chunk):
|
|
|
|
|
def parse_first(self, chunk: bytes) -> Tuple[int, int, bytes]:
|
|
|
|
|
try:
|
|
|
|
|
headerlen = struct.calcsize('>BLLL')
|
|
|
|
|
(magic, session, msg_type, datalen) = struct.unpack('>BLLL', chunk[:headerlen])
|
|
|
|
|
magic, session, msg_type, datalen = struct.unpack('>BLLL', chunk[:headerlen])
|
|
|
|
|
except:
|
|
|
|
|
raise RuntimeError('Cannot parse header')
|
|
|
|
|
if magic != 0x01:
|
|
|
|
@ -109,10 +120,10 @@ class ProtocolV2(object):
|
|
|
|
|
raise RuntimeError('Session id mismatch')
|
|
|
|
|
return msg_type, datalen, chunk[headerlen:]
|
|
|
|
|
|
|
|
|
|
def parse_next(self, chunk):
|
|
|
|
|
def parse_next(self, chunk: bytes) -> bytes:
|
|
|
|
|
try:
|
|
|
|
|
headerlen = struct.calcsize('>BLL')
|
|
|
|
|
(magic, session, sequence) = struct.unpack('>BLL', chunk[:headerlen])
|
|
|
|
|
magic, session, sequence = struct.unpack('>BLL', chunk[:headerlen])
|
|
|
|
|
except:
|
|
|
|
|
raise RuntimeError('Cannot parse header')
|
|
|
|
|
if magic != 0x02:
|
|
|
|
@ -121,10 +132,10 @@ class ProtocolV2(object):
|
|
|
|
|
raise RuntimeError('Session id mismatch')
|
|
|
|
|
return chunk[headerlen:]
|
|
|
|
|
|
|
|
|
|
def parse_session_open(self, chunk):
|
|
|
|
|
def parse_session_open(self, chunk: bytes) -> int:
|
|
|
|
|
try:
|
|
|
|
|
headerlen = struct.calcsize('>BL')
|
|
|
|
|
(magic, session) = struct.unpack('>BL', chunk[:headerlen])
|
|
|
|
|
magic, session = struct.unpack('>BL', chunk[:headerlen])
|
|
|
|
|
except:
|
|
|
|
|
raise RuntimeError('Cannot parse header')
|
|
|
|
|
if magic != 0x03:
|
|
|
|
|