From: Erik Andresen Date: Thu, 17 Dec 2020 10:13:35 +0000 (+0100) Subject: renamed protocoll -> protocol X-Git-Url: https://defiant.homedns.org/gitweb/?p=pyshared.git;a=commitdiff_plain;h=64cbeb6ce456b28c0ddba30727e7f54cdc93b29d renamed protocoll -> protocol --- diff --git a/.gitignore b/.gitignore index c9b568f..ffffad6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc *.swp +pycrc diff --git a/net.py b/net.py index 9092936..61655fc 100755 --- a/net.py +++ b/net.py @@ -6,7 +6,7 @@ import logging import traceback from threading import Thread from time import sleep -from protocoll import * +from protocol import * logger = logging.getLogger(__name__) @@ -35,7 +35,7 @@ class NetServer(Thread): except: continue logger.debug("New Connection") - proto = Protocoll(NetWrapper(conn)) + proto = Protocol(NetWrapper(conn)) if self.handler_connect: self.handler_connect(proto) while True: @@ -85,7 +85,7 @@ class NetWrapper: def close(self): return self.sck.close() -class NetClient(Protocoll, Thread): +class NetClient(Protocol, Thread): def __init__(self, kTarget, handler=None): Thread.__init__(self) self.setDaemon(True) @@ -93,7 +93,7 @@ class NetClient(Protocoll, Thread): self.comm.connect(kTarget) self.comm.setblocking(0) self.comm.settimeout(0.01) - Protocoll.__init__(self, NetWrapper(self.comm)) + Protocol.__init__(self, NetWrapper(self.comm)) self.handler = handler self.bRun = False @@ -124,7 +124,7 @@ class NetClient(Protocoll, Thread): while True: i+=1 try: - return Protocoll.receive(self) + return Protocol.receive(self) except: if i > 300: raise diff --git a/protocol.py b/protocol.py new file mode 100755 index 0000000..eb31a52 --- /dev/null +++ b/protocol.py @@ -0,0 +1,161 @@ +#!/usr/bin/env python +# -*- coding: iso-8859-15 -*- + +import struct +import threading +import math +from pycrc.crc_algorithms import Crc + +class ByteError(Exception): + def __init__(self, value): + Exception.__init__(self) + self.value = value + def __str__(self): + return "Byte Error, got 0x%x" % self.value + +class CRCError(Exception): + def __str__(self): + return "CRC Error" + +class TimeoutException(Exception): + def __str__(self): + return "Timeout" + +class NAKReceived(Exception): + def __str__(self): + return "NAK received" + +class PackageTooBigException(Exception): + def __str__(self): + return "Package too long" + + +class Protocol: + ENQ = 0x5 + ACK = 0x6 + DC1 = 0x11 + NAK = 0x21 + MAX_LEN = 128 + STATE_DEFAULT = 0 + STATE_LEN = 1 + STATE_READ = 2 + + def __init__(self, conn): + self.conn = conn + self.lock = threading.Lock() + self.crc = Crc(width = 8, poly = 0x07, reflect_in = False, xor_in = 0x0, reflect_out = False, xor_out = 0x00) + + def __get_ack(self): + c = "" + for i in range(60): + try: + c = self.conn.read(1) + except: + continue + if c: + break + if not c: + self.conn.close() + raise TimeoutException() + c = ord(c) + if c not in (self.ACK, self.NAK): + raise ByteError(c) + return c == self.ACK + + def send(self, addr, msg, bSlitMsg=False): + msg_len = 3 + len(msg) + 1 + if bSlitMsg and msg_len > self.MAX_LEN: + num_per_packet = self.MAX_LEN - 3 - 1 + num_packets = math.ceil(len(msg)/float(num_per_packet)) + self.send(addr, "%cSplit %d" % (self.DC1, num_packets)) + for i in range(0, len(msg), num_per_packet): + msg_part = msg[i:i+num_per_packet] + self.send(addr, msg_part) + return + self.lock.acquire() + try: + if msg_len > self.MAX_LEN: + raise PackageTooBigException() + packet = struct.pack(" self.MAX_LEN: + self.__reply_nak() + raise PackageTooBigException() + state = self.STATE_READ + packet += c + num = ord(c)-2 + elif state == self.STATE_READ: + packet += c + num-=1 + if num == 0: + if self.crc.bit_by_bit_fast(packet) == 0: + self.__reply_ack() + msgtype, msglen, addr, msg, crc = struct.unpack(" 1: + lCmd = msg[1:].split() + if len(lCmd) == 2 and lCmd[0] == "Split": + num = int(lCmd[1]) + msg = "" + for i in range(num): + addr_part, msg_part = self.receive() + if addr_part == addr: + msg+=msg_part + return addr, msg + +if __name__ == "__main__": + import sys + if (len(sys.argv) > 2): + # sender + with open(sys.argv[1], "a+") as f: + p = Protocol(f) + p.write(int(sys.argv[2]), sys.argv[3]) + else: + # receiver + with open(sys.argv[1], "a+") as f: + p = Protocol(f) + print p.read() diff --git a/protocoll.py b/protocoll.py deleted file mode 100644 index 5beea6e..0000000 --- a/protocoll.py +++ /dev/null @@ -1,148 +0,0 @@ -#!/usr/bin/env python -# -*- coding: iso-8859-15 -*- - -import struct -import threading -import math -from pycrc.crc_algorithms import Crc - -class ByteError(Exception): - def __init__(self, value): - Exception.__init__(self) - self.value = value - def __str__(self): - return "Byte Error, got 0x%x" % self.value - -class CRCError(Exception): - def __str__(self): - return "CRC Error" - -class TimeoutException(Exception): - def __str__(self): - return "Timeout" - -class NAKReceived(Exception): - def __str__(self): - return "NAK received" - -class PackageTooBigException(Exception): - def __str__(self): - return "Package too long" - - -class Protocoll: - ENQ = 0x5 - ACK = 0x6 - DC1 = 0x11 - NAK = 0x21 - MAX_LEN = 128 - STATE_DEFAULT = 0 - STATE_LEN = 1 - STATE_READ = 2 - - def __init__(self, conn): - self.conn = conn - self.lock = threading.Lock() - self.crc = Crc(width = 8, poly = 0x07, reflect_in = False, xor_in = 0x0, reflect_out = False, xor_out = 0x00) - - def __get_ack(self): - c = "" - for i in range(60): - try: - c = self.conn.read(1) - except: - continue - if c: - break - if not c: - self.conn.close() - raise TimeoutException() - c = ord(c) - if c not in (self.ACK, self.NAK): - raise ByteError(c) - return c == self.ACK - - def send(self, addr, msg, bSlitMsg=False): - msg_len = 3 + len(msg) + 1 - if bSlitMsg and msg_len > self.MAX_LEN: - num_per_packet = self.MAX_LEN - 3 - 1 - num_packets = math.ceil(len(msg)/float(num_per_packet)) - self.send(addr, "%cSplit %d" % (self.DC1, num_packets)) - for i in range(0, len(msg), num_per_packet): - msg_part = msg[i:i+num_per_packet] - self.send(addr, msg_part) - return - self.lock.acquire() - try: - if msg_len > self.MAX_LEN: - raise PackageTooBigException() - packet = struct.pack(" self.MAX_LEN: - self.__reply_nak() - raise PackageTooBigException() - state = self.STATE_READ - packet += c - num = ord(c)-2 - elif state == self.STATE_READ: - packet += c - num-=1 - if num == 0: - if self.crc.bit_by_bit_fast(packet) == 0: - self.__reply_ack() - msgtype, msglen, addr, msg, crc = struct.unpack(" 1: - lCmd = msg[1:].split() - if len(lCmd) == 2 and lCmd[0] == "Split": - num = int(lCmd[1]) - msg = "" - for i in range(num): - addr_part, msg_part = self.receive() - if addr_part == addr: - msg+=msg_part - return addr, msg