Source

ppenet / ppenet / peer.py

import random
import protocol

DEFAULT_ROUND_TRIP_TIME = 500
DEFAULT_PACKET_THROTTLE = 32
PACKET_THROTTLE_SCALE = 32
PACKET_THROTTLE_COUNTER = 7
PACKET_THROTTLE_ACCELERATION = 2
PACKET_THROTTLE_DECELERATION = 2
PACKET_THROTTLE_INTERVAL = 5000
PACKET_LOSS_SCALE = (1 << 16)
PACKET_LOSS_INTERVAL = 10000
WINDOW_SIZE_SCALE = 64 * 1024
TIMEOUT_LIMIT = 32
TIMEOUT_MINIMUM = 5000
TIMEOUT_MAXIMUM = 30000
PING_INTERVAL = 500
UNSEQUENCED_WINDOWS = 64
UNSEQUENCED_WINDOW_SIZE = 1024
FREE_UNSEQUENCED_WINDOWS = 32
RELIABLE_WINDOWS = 16
RELIABLE_WINDOW_SIZE = 0x1000
FREE_RELIABLE_WINDOWS = 8


class State:
    (DISCONNECTED,
    CONNECTING,
    ACKNOWLEDGING_CONNECT,
    CONNECTION_PENDING,
    CONNECTION_SUCCEEDED,
    CONNECTED,
    DISCONNECT_LATER,
    DISCONNECTING,
    ACKNOWLEDGING_DISCONNECT,
    ZOMBIE) = range(10)


class Channel(object):
    def __init__(self):
        self.outgoing_reliable_sequence_number = 0
        self.outgoing_unreliable_sequence_number = 0
        self.used_reliable_windows = 0
        self.reliable_windows = []
        self.incoming_reliable_sequence_number = 0
        self.incoming_unreliable_sequence_number = 0
        self.incoming_reliable_commands = []
        self.incoming_unreliable_commands = []


class Peer(object):
    def __init__(self, host, address, peer_id, channel_count, data=None):
        self.host = host
        self.outgoing_peer_id = protocol.MAXIMUM_PEER_ID
        self.incoming_peer_id = peer_id
        self.connect_id = 0
        self.outgoing_session_id = self.incoming_session_id = 0xFF
        self.address = address
        self.data = data
        self.state = State.DISCONNECTED
        self.channels = []
        self.channel_count = 0
        self.incoming_bandwidth = 0
        self.outgoing_bandwidth = 0
        self.incoming_bandwidth_throttle_epoch = 0
        self.outgoing_bandwidth_throttle_epoch = 0
        self.incoming_data_total = 0
        self.outgoing_data_total = 0
        self.last_send_time = 0
        self.last_receive_time = 0
        self.next_timeout = 0
        self.earliest_timeout = 0
        self.packet_loss_epoch = 0
        self.packets_sent = 0
        self.packets_lost = 0
        self.packet_loss = 0
        self.packet_loss_variance = 0
        self.packet_throttle = DEFAULT_PACKET_THROTTLE
        self.packet_throttle_limit = PACKET_THROTTLE_SCALE
        self.packet_throttle_counter = 0
        self.packet_throttle_epoch = 0
        self.packet_throttle_acceleration = PACKET_THROTTLE_ACCELERATION
        self.packet_throttle_deceleration = PACKET_THROTTLE_DECELERATION
        self.packet_throttle_interval = PACKET_THROTTLE_INTERVAL
        self.ping_interval = PING_INTERVAL
        self.timeout_limit = TIMEOUT_LIMIT
        self.timeout_minimum = TIMEOUT_MINIMUM
        self.timeout_maximum = TIMEOUT_MAXIMUM
        self.last_round_trip_time = DEFAULT_ROUND_TRIP_TIME
        self.lowest_round_trip_time = DEFAULT_ROUND_TRIP_TIME
        self.last_round_trip_time_variance = 0
        self.highest_round_trip_time_variance = 0
        self.round_trip_time = DEFAULT_ROUND_TRIP_TIME
        self.round_trip_time_variance = 0
        self.mtu = host.mtu
        self.window_size = protocol.MAXIMUM_WINDOW_SIZE
        self.reliable_data_in_transit = 0
        self.outgoing_reliable_sequence_number = 0
        self.acknowledgements = []
        self.sent_reliable_commands = []
        self.sent_unreliable_commands = []
        self.outgoing_reliable_commands = []
        self.outgoing_unreliable_commands = []
        self.dispatched_commands = []
        self.needs_dispatch = 0
        self.incoming_unsequenced_group = 0
        self.outgoing_unsequenced_group = 0
        self.unsequenced_window = []  # probably bytearray or memoryview
        self.event_data = 0

    def connect(self, address):
        host = self.host
        self.state = State.CONNECTING
        self.address = address
        self.connect_id = random.getrandbits(32)
        if host.outgoing_bandwidth == 0:
            window_size = protocol.MAXIMUM_WINDOW_SIZE
        else:
            window_size = host.outgoing_bandwidth / WINDOW_SIZE_SCALE\
                          * protocol.MAXIMUM_WINDOW_SIZE
        self.window_size = max(min(window_size, protocol.MAXIMUM_WINDOW_SIZE),
                               protocol.MINIMUM_WINDOW_SIZE)
        protocol.CommandHeader(
            protocol.Command.CONNECT | protocol.Flag.COMMAND_ACKNOWLEDGE,
            0xFF,
            0
        )
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.