-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTcpDataServer.py
More file actions
58 lines (48 loc) · 1.69 KB
/
TcpDataServer.py
File metadata and controls
58 lines (48 loc) · 1.69 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
49
50
51
52
53
54
55
56
57
58
from queue import Queue, Empty
import socket
import threading
class TcpDataServer:
def __init__(self, queue: Queue, host='127.0.0.1', port=0, thread_name=None):
self.queue = queue
self.host = host
self.port = port
self.socket = None
self.client_socket = None
self.start_thread = threading.Thread(target=self._start, name=thread_name)
self.stop_event = threading.Event()
self.started = False
def _start(self):
try:
self.client_socket, _ = self.socket.accept()
while True:
try:
data = self.queue.get(timeout=0.5)
self.client_socket.send(data)
except Empty:
if self.stop_event.is_set():
return
continue
except Exception as e:
self.queue.task_done()
raise e
self.queue.task_done()
except OSError:
return
finally:
if self.client_socket is not None:
self.client_socket.close()
self.client_socket = None
def start(self):
if self.started:
raise RuntimeError("TcpDataServer can only be start() once.")
self.started = True
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.host, self.port))
self.socket.listen(1)
self.start_thread.start()
def stop(self):
self.socket.close()
self.stop_event.set()
self.start_thread.join()
def get_port(self):
return self.socket.getsockname()[1]