forked from hipstercat/giantsd
81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
|
from .packet import Packet
|
||
|
|
||
|
|
||
|
class DFrame:
|
||
|
PACKET_COMMAND_DATA = 0x01
|
||
|
PACKET_COMMAND_RELIABLE = 0x02
|
||
|
PACKET_COMMAND_SEQUENTIAL = 0x04
|
||
|
PACKET_COMMAND_POLL = 0x08
|
||
|
PACKET_COMMAND_NEW_MSG = 0x10
|
||
|
PACKET_COMMAND_END_MSG = 0x20
|
||
|
PACKET_COMMAND_USER_1 = 0x40
|
||
|
PACKET_COMMAND_USER_2 = 0x80
|
||
|
|
||
|
PACKET_CONTROL_RETRY = 0x01
|
||
|
PACKET_CONTROL_KEEPALIVE_OR_CORRELATE = 0x02
|
||
|
PACKET_CONTROL_COALESCE = 0x04
|
||
|
PACKET_CONTROL_END_STREAM = 0x08
|
||
|
PACKET_CONTROL_SACK1 = 0x10
|
||
|
PACKET_CONTROL_SACK2 = 0x20
|
||
|
PACKET_CONTROL_SEND1 = 0x40
|
||
|
PACKET_CONTROL_SEND2 = 0x80
|
||
|
|
||
|
def __init__(self, packet=None):
|
||
|
self.Command = DFrame.PACKET_COMMAND_DATA
|
||
|
self.Control = 0x00
|
||
|
self.Seq = 0
|
||
|
self.NRcv = 0
|
||
|
self.SACKMask1 = b''
|
||
|
self.SACKMask2 = b''
|
||
|
self.SendMask1 = b''
|
||
|
self.SendMask2 = b''
|
||
|
self.Signature = b''
|
||
|
self.SessID = b''
|
||
|
self.Payload = b''
|
||
|
|
||
|
if packet:
|
||
|
self.parse(packet)
|
||
|
|
||
|
def parse(self, packet):
|
||
|
packet.seek(0)
|
||
|
self.Command = packet.getByte()
|
||
|
self.Control = packet.getByte()
|
||
|
self.Seq = packet.getByte()
|
||
|
self.NRcv = packet.getByte()
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SACK1:
|
||
|
self.SACKMask1 = packet.getLong()
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SACK2:
|
||
|
self.SACKMask2 = packet.getLong()
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SEND1:
|
||
|
self.SendMask1 = packet.getLong()
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SEND2:
|
||
|
self.SendMask2 = packet.getLong()
|
||
|
|
||
|
# TODO: handle signature
|
||
|
|
||
|
if self.Control & DFrame.PACKET_CONTROL_KEEPALIVE_OR_CORRELATE:
|
||
|
self.SessID = packet.getLong()
|
||
|
self.Payload = packet.read()
|
||
|
|
||
|
def to_packet(self):
|
||
|
packet = Packet()
|
||
|
packet.putByte(self.Command)
|
||
|
packet.putByte(self.Control)
|
||
|
packet.putByte(self.Seq)
|
||
|
packet.putByte(self.NRcv)
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SACK1:
|
||
|
packet.putLong(self.SACKMask1)
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SACK2:
|
||
|
packet.putLong(self.SACKMask2)
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SEND1:
|
||
|
packet.putLong(self.SendMask1)
|
||
|
if self.Control & DFrame.PACKET_CONTROL_SEND2:
|
||
|
packet.putLong(self.SendMask2)
|
||
|
if self.Signature:
|
||
|
packet.putLongLong(self.Signature)
|
||
|
if self.Control & DFrame.PACKET_CONTROL_KEEPALIVE_OR_CORRELATE:
|
||
|
packet.putLong(self.SessID)
|
||
|
packet.putBytes(self.Payload)
|
||
|
|
||
|
return packet
|