Commits

Szymon Wróblewski committed 5810f12

added Host and Peer classes

Comments (0)

Files changed (5)

ppenet/constants.py

+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
+import socket
+from Queue import Queue
+import protocol
+from peer import Peer, State
+
+HOST_ANY = 0
+HOST_BROADCAST = 0xFFFFFFFF
+PORT_ANY = 0
+
+BANDWIDTH_THROTTLE_INTERVAL = 1000
+DEFAULT_MTU = 1400
+RECEIVE_BUFFER_SIZE = 256 * 1024
+SEND_BUFFER_SIZE = 256 * 1024
+
+
+class Host(object):
+    def __init__(self, address=None, peer_count=0, channel_limit=0,
+                 incoming_bandwidth=0, outgoing_bandwidth=0):
+        self.socket = s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        if address is not None:
+            address = socket.gethostbyname(address[0]), address[1:]
+            s.bind(address)
+        self.address = address
+        # disable blocking read / write
+        s.setblocking(0)
+        # enable sending / receiving broadcast packets
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
+        # set buffers size
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, RECEIVE_BUFFER_SIZE)
+        s.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, SEND_BUFFER_SIZE)
+        self.peers = []
+        if not peer_count:
+            self.peer_count = protocol.MAXIMUM_PEER_ID
+        else:
+            self.peer_count = min(peer_count, protocol.MAXIMUM_PEER_ID)
+        if not channel_limit:
+            self.channel_limit = protocol.MAXIMUM_CHANNEL_COUNT
+        else:
+            self.channel_limit = max(min(channel_limit,
+                                         protocol.MAXIMUM_CHANNEL_COUNT),
+                                     protocol.MAXIMUM_CHANNEL_COUNT)
+        self.incoming_bandwidth = incoming_bandwidth
+        self.outgoing_bandwidth = outgoing_bandwidth
+        self.bandwidth_throttle_epoch = 0
+        self.recalculate_bandwidth_limits = 0
+        self.mtu = self.DEFAULT_MTU
+        self.commands = []
+        self.command_count = 0
+        self.buffer = bytearray()
+        self.buffer_count = 0
+        self.checksum = None
+        self.received_address = (HOST_ANY, PORT_ANY)
+        self.received_data = None
+        self.total_sent_data = 0
+        self.total_sent_packets = 0
+        self.total_received_data = 0
+        self.total_received_packets = 0
+        self.compressor = None
+        self.dispatch_queue = Queue()
+
+    def __del__(self):
+        for peer in self.peers:
+            peer.reset()
+        self.socket.close()
+
+    def __repr__(self):
+        return '%s(%s, %d, %d, %d, %d)' % (
+            self.__name__, self.address, self.peer_count, self.channel_limit,
+            self.incoming_bandwidth, self.outgoing_bandwidth)
+
+    def __str__(self):
+        return '%s %s sent: %d recv: %d' % (
+            self.__name__, self.address,
+            self.total_sent_packets, self.total_received_packets)
+
+    def connect(self, address, channel_count=0, data=0):
+        for peer in self.peers:
+            if peer.state == State.DISCONNECTED:
+                break
+        else:  # No empty slot for new peer
+            return
+        return self.peers.append(Peer(self, address, channel_count, data))
+class State(object):
+    (DISCONNECTED,
+    CONNECTING,
+    ACKNOWLEDGING_CONNECT,
+    CONNECTION_PENDING,
+    CONNECTION_SUCCEEDED,
+    CONNECTED,
+    DISCONNECT_LATER,
+    DISCONNECTING,
+    ACKNOWLEDGING_DISCONNECT,
+    ZOMBIE) = range(10)
+
+
+class Peer(object):
+    pass

ppenet/protocol.py

 
 
 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)
+    (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:
 
 
 Header = namedtuple('Header', (
-    'peerID',
-    'sentTime',
+    'peer_id',
+    'sent_time',
 ))
 HeaderStruct = Struct('!HH')
 

tests/__init__.py

Empty file added.