-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackgroundprocesses.py
42 lines (31 loc) · 1.11 KB
/
backgroundprocesses.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import select
import socket
import threading
from constants import *
from crypto import *
from packetEngine import PacketAccumulator, MessageAggregator
class BackgroundListener(threading.Thread):
def __init__(self, address, port, sk):
threading.Thread.__init__(self)
self.address = address
self.port = port
self.sk = sk # should be global as well I guess!
def run(self):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setblocking(0)
s.bind((self.address, self.port))
msg_aggregator = MessageAggregator(self.sk)
# Receive loop
while True:
try:
ready = select.select([s], [], [], 1)
if ready[0]:
data, addr = s.recvfrom(1024)
msg_aggregator.feed_packet(data)
# Print any finalized messages.
msg_aggregator.print_ready()
except Exception as e:
print("Exception in listener: ", e)
s.close()
print('Closed the server socket')
print('Terminating ...')