| 1 |
from __future__ import absolute_import |
| 2 |
import sys, os |
| 3 |
from struct import pack, unpack, calcsize |
| 4 |
from dotviewer.strunicode import tryencode, ord_byte_index |
| 5 |
|
| 6 |
MAGIC = -0x3b83728b |
| 7 |
|
| 8 |
CMSG_INIT = b'i' |
| 9 |
CMSG_START_GRAPH = b'[' |
| 10 |
CMSG_ADD_NODE = b'n' |
| 11 |
CMSG_ADD_EDGE = b'e' |
| 12 |
CMSG_ADD_LINK = b'l' |
| 13 |
CMSG_FIXED_FONT = b'f' |
| 14 |
CMSG_STOP_GRAPH = b']' |
| 15 |
CMSG_MISSING_LINK= b'm' |
| 16 |
CMSG_SAY = b's' |
| 17 |
|
| 18 |
MSG_OK = b'O' |
| 19 |
MSG_ERROR = b'E' |
| 20 |
MSG_RELOAD = b'R' |
| 21 |
MSG_FOLLOW_LINK = b'L' |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
long_min = -2147483648 |
| 26 |
long_max = 2147483647 |
| 27 |
|
| 28 |
|
| 29 |
def message(tp, *values): |
| 30 |
|
| 31 |
typecodes = [b''] |
| 32 |
values = list(map(tryencode, values)) |
| 33 |
for v in values: |
| 34 |
if type(v) is bytes: |
| 35 |
typecodes.append(b'%ds' % len(v)) |
| 36 |
elif 0 <= v < 256: |
| 37 |
typecodes.append(b'B') |
| 38 |
elif long_min <= v <= long_max: |
| 39 |
typecodes.append(b'l') |
| 40 |
else: |
| 41 |
typecodes.append(b'q') |
| 42 |
typecodes = b''.join(typecodes) |
| 43 |
if len(typecodes) < 256: |
| 44 |
return pack((b"!B%dsc" % len(typecodes)) + typecodes, |
| 45 |
len(typecodes), typecodes, tp, *values) |
| 46 |
else: |
| 47 |
|
| 48 |
return message('\x00', typecodes, pack("!c" + typecodes, tp, *values)) |
| 49 |
|
| 50 |
def decodemessage(data): |
| 51 |
if data: |
| 52 |
limit = ord_byte_index(data, 0) + 1 |
| 53 |
if len(data) >= limit: |
| 54 |
typecodes = b"!c" + data[1:limit] |
| 55 |
end = limit + calcsize(typecodes) |
| 56 |
if len(data) >= end: |
| 57 |
msg = unpack(typecodes, data[limit:end]) |
| 58 |
if msg[0] == b'\x00': |
| 59 |
msg = unpack(b"!c" + msg[1], msg[2]) |
| 60 |
return msg, data[end:] |
| 61 |
|
| 62 |
|
| 63 |
return None, data |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
class RemoteError(Exception): |
| 68 |
pass |
| 69 |
|
| 70 |
|
| 71 |
class IO(object): |
| 72 |
_buffer = b'' |
| 73 |
|
| 74 |
def sendmsg(self, tp, *values): |
| 75 |
self.sendall(message(tp, *values)) |
| 76 |
|
| 77 |
def recvmsg(self): |
| 78 |
while True: |
| 79 |
msg, self._buffer = decodemessage(self._buffer) |
| 80 |
if msg is not None: |
| 81 |
break |
| 82 |
self._buffer += self.recv() |
| 83 |
if msg[0] != MSG_ERROR: |
| 84 |
return msg |
| 85 |
raise RemoteError(*msg[1:]) |
| 86 |
|
| 87 |
|
| 88 |
class FileIO(IO): |
| 89 |
def __init__(self, f_in, f_out): |
| 90 |
self.f_in = f_in |
| 91 |
self.f_out = f_out |
| 92 |
|
| 93 |
def sendall(self, data): |
| 94 |
self.f_out.write(data) |
| 95 |
self.f_out.flush() |
| 96 |
|
| 97 |
def recv(self): |
| 98 |
fd = self.f_in.fileno() |
| 99 |
data = os.read(fd, 16384) |
| 100 |
if not data: |
| 101 |
raise EOFError |
| 102 |
return data |
| 103 |
|
| 104 |
def close_sending(self): |
| 105 |
self.f_out.close() |
| 106 |
|
| 107 |
def close(self): |
| 108 |
self.f_out.close() |
| 109 |
self.f_in.close() |
| 110 |
|
| 111 |
|
| 112 |
class SocketIO(IO): |
| 113 |
def __init__(self, s): |
| 114 |
self.s = s |
| 115 |
|
| 116 |
def sendall(self, data): |
| 117 |
self.s.sendall(data) |
| 118 |
|
| 119 |
def recv(self): |
| 120 |
data = self.s.recv(16384) |
| 121 |
if not data: |
| 122 |
raise EOFError |
| 123 |
return data |
| 124 |
|
| 125 |
def close_sending(self): |
| 126 |
self.s.shutdown(1) |
| 127 |
|
| 128 |
def close(self): |
| 129 |
self.s.close() |