4 # Avoid "Address already in use" errors.
5 socketserver.TCPServer.allow_reuse_address = True
11 def __init__(self, socket):
14 def send(self, message, silent_connection_break=False):
15 """Send via self.socket, encoded/delimited as way recv() expects.
17 In detail, all \ and $ in message are escaped with prefixed \,
18 and an unescaped $ is appended as a message delimiter. Then,
19 socket.send() is called as often as necessary to ensure
20 message is sent fully, as socket.send() due to buffering may
21 not send all of it right away.
23 Assuming socket is blocking, it's rather improbable that
24 socket.send() will be partial / return a positive value less
25 than the (byte) length of msg – but not entirely out of the
26 question. See: - <http://stackoverflow.com/q/19697218> -
27 <http://stackoverflow.com/q/2618736> -
28 <http://stackoverflow.com/q/8900474>
30 This also handles a socket.send() return value of 0, which
31 might be possible or not (?) for blocking sockets: -
32 <http://stackoverflow.com/q/34919846>
35 from plomrogue.errors import BrokenSocketConnection
38 if char in ('\\', '$'):
39 escaped_message += '\\'
40 escaped_message += char
41 escaped_message += '$'
42 data = escaped_message.encode()
44 while totalsent < len(data):
47 sent = self.socket.send(data[totalsent:])
48 socket_broken = sent == 0
49 except OSError as err:
50 if err.errno == 9: # "Bad file descriptor", when connection broken
54 if socket_broken and not silent_connection_break:
55 raise BrokenSocketConnection
56 totalsent = totalsent + sent
59 """Get full send()-prepared message from self.socket.
61 In detail, socket.recv() is looped over for sequences of bytes
62 that can be decoded as a Unicode string delimited by an
63 unescaped $, with \ and $ escapable by \. If a sequence of
64 characters that ends in an unescaped $ cannot be decoded as
65 Unicode, None is returned as its representation. Stop once
66 socket.recv() returns nothing.
68 Under the hood, the TCP stack receives packets that construct
69 the input payload in an internal buffer; socket.recv(BUFSIZE)
70 pops up to BUFSIZE bytes from that buffer, without knowledge
71 either about the input's segmentation into packets, or whether
72 the input is segmented in any other meaningful way; that's why
73 we do our own message segmentation with $ as a delimiter.
80 data += self.socket.recv(1024)
94 except UnicodeDecodeError:
103 class IO_Handler(socketserver.BaseRequestHandler):
106 """Move messages between network socket and game IO loop via queues.
108 On start (a new connection from client to server), sets up a
109 new queue, sends it via self.server.queue_out to the game IO
110 loop thread, and from then on receives messages to send back
111 from the game IO loop via that new queue.
113 At the same time, loops over socket's recv to get messages
114 from the outside into the game IO loop by way of
115 self.server.queue_out into the game IO. Ends connection once a
116 'QUIT' message is received from socket, and then also calls
117 for a kill of its own queue.
121 def send_queue_messages(plom_socket, queue_in, thread_alive):
122 """Send messages via socket from queue_in while thread_alive[0]."""
123 while thread_alive[0]:
125 msg = queue_in.get(timeout=1)
128 plom_socket.send(msg, True)
133 plom_socket = PlomSocket(self.request)
134 print('CONNECTION FROM:', str(self.client_address))
135 connection_id = uuid.uuid4()
136 queue_in = queue.Queue()
137 self.server.clients[connection_id] = queue_in
138 thread_alive = [True]
139 t = threading.Thread(target=send_queue_messages,
140 args=(plom_socket, queue_in, thread_alive))
142 for message in plom_socket.recv():
144 plom_socket.send('BAD MESSAGE', True)
145 elif 'QUIT' == message:
146 plom_socket.send('BYE', True)
149 self.server.queue_out.put((connection_id, message))
150 del self.server.clients[connection_id]
151 thread_alive[0] = False
152 print('CONNECTION CLOSED FROM:', str(self.client_address))
153 plom_socket.socket.close()
157 class PlomTCPServer(socketserver.ThreadingTCPServer):
158 """Bind together threaded IO handling server and message queue."""
160 def __init__(self, queue, port, *args, **kwargs):
161 super().__init__(('0.0.0.0', port), IO_Handler, *args, **kwargs)
162 self.queue_out = queue
163 self.daemon_threads = True # Else, server's threads have daemon=False.