Browse Source
* removed connection established message, apps can send that themselves (yam util does)master

7 changed files with 77 additions and 20 deletions
@ -1,19 +1,33 @@
|
||||
from base64 import b85encode, b85decode |
||||
from time import sleep |
||||
import sys |
||||
from typing import Union |
||||
|
||||
from youandme.commands import terminator |
||||
|
||||
_b85alphabet = (b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
_b85alphabet = bytearray(b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
||||
b"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~") |
||||
|
||||
|
||||
def decoded_recv_stream(raw_stream: bytearray, delay_seconds: int, max_buffer_size=) -> bytes: |
||||
def decoded_recv_stream(raw_stream: bytearray, delay_seconds: int) -> bytes: |
||||
while True: |
||||
for byte in raw_stream: |
||||
if byte == terminator: |
||||
yield b85decode(raw_stream[:len(raw_stream) - 1]) |
||||
raw_stream.pop() |
||||
yield b85decode(raw_stream) |
||||
raw_stream.clear() |
||||
continue |
||||
if byte not in _b85alphabet: |
||||
raise ValueError('Not valid base85 encoding') |
||||
sleep(delay_seconds) |
||||
|
||||
|
||||
def encode_and_send(send_stream: bytearray, data: Union[bytes, str]): |
||||
try: |
||||
data = data.encode('utf-8') |
||||
except AttributeError: |
||||
pass |
||||
encoded = b85encode(data) |
||||
for c in encoded: |
||||
send_stream.append(c) |
||||
|
||||
send_stream.append(terminator) |
||||
|
||||
|
Loading…
Reference in new issue