-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
13 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,31 @@ | ||
#! /usr/local/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import struct | ||
import types | ||
|
||
|
||
class LenL2PacketRcv(): | ||
def __init__(self, name=''): | ||
self.name = name | ||
class LenPackets(): | ||
def __init__(self): | ||
self.data_rcv = b'' | ||
self.l2_packets = [] | ||
|
||
def __add_packet(self, value: bytes) -> None: | ||
def pck_in(self, data: types.GeneratorType) -> None: | ||
""" | ||
get packet from length header | ||
""" | ||
self.data_rcv = b''.join([self.data_rcv, b''.join(value)]) | ||
self.data_rcv = b''.join([self.data_rcv, b''.join(data)]) | ||
# esli razmer packeta dostatochen dlya dekodirovaniya | ||
while (len(self.data_rcv) > 2) and (len(self.data_rcv) >= (struct.unpack('<H',self.data_rcv[:2])[0])): | ||
# print('''struct.unpack('<H',self.data_rcv[:2])[0]) = ''',struct.unpack('<H',self.data_rcv[:2])[0]) | ||
while (len(self.data_rcv) > 2) and (len(self.data_rcv) >= \ | ||
(struct.unpack('<H',self.data_rcv[:2])[0])): | ||
# get packet header | ||
head = struct.unpack('<H',self.data_rcv[:2])[0] | ||
pck = self.data_rcv[:head] | ||
# remove packet from buffer | ||
self.data_rcv = self.data_rcv[head:] | ||
# remove header from packet | ||
pck = pck[2:] | ||
self.l2_packets.append(pck) | ||
|
||
def segmentation_packets(self, to_s_data: bytes) -> types.GeneratorType : | ||
self.__add_packet(to_s_data) | ||
while self.l2_packets: | ||
yield self.l2_packets.pop(0) | ||
|
||
|
||
class LenL2PacketSend(): | ||
def __init__(self, name=''): | ||
self.name = name | ||
self.data_send = b'' | ||
|
||
def pop_packet(self) -> bytes : | ||
yield self.data_send | ||
self.data_send = b'' | ||
yield pck[2:] | ||
|
||
def add_packets(self, value: types.GeneratorType) -> None : | ||
def pck_out(self, value: types.GeneratorType) -> bytes : | ||
""" | ||
add length header to packet | ||
""" | ||
for packet in value: | ||
head = struct.pack('<H',len(packet)+2) | ||
self.data_send = b''.join([self.data_send, head, packet]) | ||
yield b''.join([head, packet]) |