Source

ppenet / ppenet / protocol.py

Full commit
import socket
from structtype import structtype


MINIMUM_MTU = 576
MAXIMUM_MTU = 4096
MAXIMUM_PACKET_COMMANDS = 32
MINIMUM_WINDOW_SIZE = 4096
MAXIMUM_WINDOW_SIZE = 32768
MINIMUM_CHANNEL_COUNT = 1
MAXIMUM_CHANNEL_COUNT = 255
MAXIMUM_PEER_ID = 0xFFF
MAXIMUM_PACKET_SIZE = 1024 * 1024 * 1024
MAXIMUM_FRAGMENT_COUNT = 1024 * 1024


class Command:
    (NONE,
    ACKNOWLEDGE,
    CONNECT,
    VERIFY_CONNECT,
    DISCONNECT,
    PING,
    SEND_RELIABLE,
    SEND_UNRELIABLE,
    SEND_FRAGMENT,
    SEND_UNSEQUENCED,
    BANDWIDTH_LIMIT,
    THROTTLE_CONFIGURE,
    SEND_UNRELIABLE_FRAGMENT,
    COUNT) = range(14)


class Flag:
    COMMAND_ACKNOWLEDGE = 1 << 7
    COMMAND_UNSEQUENCED = 1 << 6

    HEADER_COMPRESSED = 1 << 14
    HEADER_SENT_TIME = 1 << 15
    HEADER_MASK = HEADER_COMPRESSED | HEADER_SENT_TIME
    HEADER_SESSION_MASK = 3 << 12
    HEADER_SESSION_SHIFT = 12


Header = structtype('Header', (
    'peer_id',
    'sent_time',
), '!HH', default=0)

CommandHeader = structtype('CommandHeader', (
    'command',
    'channel_id',
    'reliable_sequence_number',
), '!BBH', default=0)

Acknowledge = structtype('Acknowledge', (
    'header',
    'received_reliable_sequence_number',
    'received_sent_time'
), '!%dsHH' % int(CommandHeader.size), default=0)

Connect = structtype('Connect', (
    'header',
    'outgoing_peer_id',
    'incoming_session_id',
    'outgoing_session_id',
    'mtu',
    'window_size',
    'channel_count',
    'incoming_bandwidth',
    'outgoing_bandwidth',
    'packet_throttle_interval',
    'packet_throttle_acceleration',
    'packet_throttle_deceleration',
    'connect_id',
    'data',
), '!%dsHBB10I' % CommandHeader.size, default=0)

VerifyConnect = structtype('VerifyConnect', (
    'header',
    'outgoing_peer_id',
    'incoming_session_id',
    'outgoing_session_id',
    'mtu',
    'window_size',
    'channel_count',
    'incoming_bandwidth',
    'outgoing_bandwidth',
    'packet_throttle_interval',
    'packet_throttle_acceleration',
    'packet_throttle_deceleration',
    'connect_id',
), '!%dsHBB9I' % CommandHeader.size, default=0)

BandwidthLimit = structtype('BandwidthLimit', (
    'header',
    'incoming_bandwidth',
    'outgoing_bandwidth',
), '!%dsII' % CommandHeader.size, default=0)

ThrottleConfigure = structtype('ThrottleConfigure', (
    'header',
    'packet_throttle_interval',
    'packet_throttle_acceleration',
    'packet_throttle_deceleration',
), '!%dsIII' % CommandHeader.size, default=0)

Disconnect = structtype('Disconnect', (
    'header',
    'data',
), '!%dsI' % CommandHeader.size, default=0)

Ping = structtype('Ping', (
    'header',
), '!%ds' % CommandHeader.size, default=0)

SendReliable = structtype('SendReliable', (
    'header',
    'data_length',
), '!%dsH' % CommandHeader.size, default=0)

SendUnreliable = structtype('SendUnreliable', (
    'header',
    'unreliable_sequence_number',
    'data_length',
), '!%dsHH' % CommandHeader.size, default=0)

SendUnsequenced = structtype('SendUnsequenced', (
    'header',
    'unsequenced_group',
    'data_length',
), '!%dsHH' % CommandHeader.size, default=0)

SendFragment = structtype('SendFragment', (
    'header',
    'start_sequence_number',
    'data_length',
    'fragment_count',
    'fragment_number',
    'total_length',
    'fragment_offset',
), '!%dsHH4I' % CommandHeader.size, default=0)