Skip to content

Commit

Permalink
refactoring l2/len_packet.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yv84 committed Jul 5, 2014
1 parent 5d509c9 commit f249839
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 36 deletions.
10 changes: 4 additions & 6 deletions src/l2/key_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools
import numpy

from .len_packet import LenL2PacketRcv, LenL2PacketSend
from .len_packet import LenPackets
from .xor import Xor
from .gs_l2_packet import PacketError

Expand All @@ -23,15 +23,14 @@ def key_packet_initialization_remover(to_s_data):
if to_s_d.startswith(b'\x19\x00.'):
for stack, obj in zip([self.packet.client.command_stack, self.packet.server.command_stack],
[self.packet.client, self.packet.server]):
stack.append(lambda data: obj.pck_rcv.segmentation_packets(data))
stack.append(lambda data: obj.pck_len.pck_in(data))
stack.append(lambda gen: obj.xor_in.xor(gen))
stack.append(lambda gen, manager=self.packet.manager, name=obj.name,
peername=self.packet.peername : self.packet.manager.set_manager_data(name, gen, peername))
stack.append(lambda gen, name=obj.name, gameapi=self.gameapi, peername=self.packet.peername: \
self.packet_print_dtype(name, gameapi, gen, peername))
stack.append(lambda gen: obj.xor_out.xor(gen))
stack.append(lambda gen: obj.pck_send.add_packets(gen))
stack.append(lambda gen: obj.pck_send.pop_packet()) # -> bytes(data)
stack.append(lambda gen: obj.pck_len.pck_out(gen))
self.packet.server.command_stack.append(lambda data: key_packet_initialization_remover(data))
return to_s_data

Expand Down Expand Up @@ -63,7 +62,6 @@ def __init__(self, name):
self.name = name
self._data = b''
self.command_stack = [] # func(gen: types.GeneratorType) -> types.GeneratorType
self.pck_rcv = LenL2PacketRcv()
self.pck_send = LenL2PacketSend()
self.pck_len = LenPackets()
self.xor_in = Xor('decode')
self.xor_out = Xor('code')
39 changes: 9 additions & 30 deletions src/l2/len_packet.py
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])

0 comments on commit f249839

Please sign in to comment.