-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket_handler.py
More file actions
executable file
·48 lines (37 loc) · 1.3 KB
/
socket_handler.py
File metadata and controls
executable file
·48 lines (37 loc) · 1.3 KB
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
43
44
45
46
47
48
#!/usr/bin/env python
import struct
from socket import *
from structures import *
import threading
import loggers
class SocketThread(threading.Thread):
def __init__(self, session):
threading.Thread.__init__(self)
self.session = session
self.running = True
#open socket
self.socket = socket(AF_INET, SOCK_DGRAM)
self.socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.socket.bind(('', 20777))
self.start()
def close(self):
self.running = False
def run(self):
print "Waiting"
while self.running:
#populate a packet object
#todo, remove calcsize from here and do it in the pkt object
size = struct.calcsize('f' * len(Packet.keys))
raw_packet = self.socket.recv(size)
packet = Packet(raw_packet)
#add this packet object to the current session
self.session.current_lap.add_packet(packet)
#we've signalled for the recv thread to stop, so do cleanup
self.socket.close()
if __name__ == '__main__':
#Threading probably not required here, but could make adding a GUI later
#a bit easier?
s = Session(loggers.GoogleDocs())
thread = SocketThread(s);
while True:
pass #todo print out some stuff