-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.py
More file actions
66 lines (52 loc) · 1.76 KB
/
peer.py
File metadata and controls
66 lines (52 loc) · 1.76 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
59
60
61
62
63
64
65
66
from typing import Callable, Optional
import threading
import socket
class EasySocketPeer(object):
def __init__(self, sock: socket.socket, suffix: bytes):
self.event_messaged = threading.Event()
self.closed = False
self.sock = sock
self.suffix = suffix
self.on_message: Optional[Callable[[bytes], None]] = None
self.on_disconnect: Optional[Callable[[], None]] = None
threading.Thread(target=self._listen, daemon=True).start()
def _listen(self):
try:
while True:
if self.on_message:
self.event_messaged.clear()
message = self.recv_message()
if not message and self.closed:
break
self.on_message(message)
self.event_messaged.set()
except Exception:
self.close()
def close(self):
if not self.closed:
self.closed = True
try:
self.sock.shutdown(socket.SHUT_RDWR)
except Exception:
pass
try:
self.sock.close()
except Exception:
pass
if self.on_disconnect:
self.on_disconnect()
def recv_message(self):
message = b""
while True:
chunk = self.sock.recv(1)
if not chunk:
self.close()
return b""
message += chunk
if message.endswith(self.suffix):
break
return message[:-len(self.suffix)]
def send_message(self, message: bytes):
self.sock.send(message + self.suffix)
def wait_message(self):
self.event_messaged.wait()