ppenet / ppenet / protocol.py

from collections import namedtuple
import socket
from struct import Struct


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_FLAG_ACKNOWLEDGE = 1 << 7
    COMMAND_FLAG_UNSEQUENCED = 1 << 6

    HEADER_FLAG_COMPRESSED = 1 << 14
    HEADER_FLAG_SENT_TIME = 1 << 15
    HEADER_FLAG_MASK = HEADER_FLAG_COMPRESSED | HEADER_FLAG_SENT_TIME
    HEADER_SESSION_MASK = 3 << 12
    HEADER_SESSION_SHIFT = 12


Header = namedtuple('Header', (
    'peerID',
    'sentTime',
))
HeaderStruct = Struct('!HH')

CommandHeader = namedtuple('CommandHeader', (
    'command',
    'channel_id',
    'reliable_sequence_number',
))
CommandHeaderStruct = Struct('!BBH')

Acknowledge = namedtuple('Acknowledge', (
    'header',
    'received_reliable_sequence_number',
    'received_sent_time'
))
AcknowledgeStruct = Struct('!%dsHH' % CommandHeaderStruct.size)

Connect = namedtuple('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',
))
ConnectStruct = Struct('!%dsHBB10I' % CommandHeaderStruct.size)

VerifyConnect = namedtuple('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',
))
VerifyConnectStruct = Struct('!%dsHBB9I' % CommandHeaderStruct.size)

BandwidthLimit = namedtuple('BandwidthLimit', (
    'header',
    'incoming_bandwidth',
    'outgoing_bandwidth',
))
BandwidthLimitStruct = Struct('!%dsII' % CommandHeaderStruct.size)

ThrottleConfigure = namedtuple('ThrottleConfigure', (
    'header',
    'packet_throttle_interval',
    'packet_throttle_acceleration',
    'packet_throttle_deceleration',
))
ThrottleConfigureStruct = Struct('!%dsIII' % CommandHeaderStruct.size)

Disconnect = namedtuple('Disconnect', (
    'header',
    'data',
))
DisconnectStruct = Struct('!%dsI' % CommandHeaderStruct.size)

Ping = namedtuple('Ping', (
    'header',
))
PingStruct = Struct('!%ds' % CommandHeaderStruct.size)

SendReliable = namedtuple('SendReliable', (
    'header',
    'data_length',
))
SendReliableStruct = Struct('!%dsH' % CommandHeaderStruct.size)

SendUnreliable = namedtuple('SendUnreliable', (
    'header',
    'unreliable_sequence_number',
    'data_length',
))
SendUnreliableStruct = Struct('!%dsHH' % CommandHeaderStruct.size)

SendUnsequenced = namedtuple('SendUnsequenced', (
    'header',
    'unsequenced_group',
    'data_length',
))
SendUnsequencedStruct = Struct('!%dsHH' % CommandHeaderStruct.size)

SendFragment = namedtuple('SendFragment', (
    'header',
    'start_sequence_number',
    'data_length',
    'fragment_count',
    'fragment_number',
    'total_length',
    'fragment_offset',
))
SendFragmentStruct = Struct('!%dsHH4I' % CommandHeaderStruct.size)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.