X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=new2%2Fplomrogue%2Fio_tcp.py;h=f0a49a97dad632fa4f3dc10770432f424ba90155;hb=31d5ee2ee37c0e82e776053a1311c99dda2255e7;hp=5cf66d9e19fc2b278cd5ed94e5ea0c6721495a91;hpb=7ea66be9de28472ea2721b9170d6fe75189a4495;p=plomrogue2-experiments
diff --git a/new2/plomrogue/io_tcp.py b/new2/plomrogue/io_tcp.py
index 5cf66d9..f0a49a9 100644
--- a/new2/plomrogue/io_tcp.py
+++ b/new2/plomrogue/io_tcp.py
@@ -46,6 +46,7 @@ class PlomSocket:
try:
sent = self.socket.send(data[totalsent:])
socket_broken = sent == 0
+ totalsent = totalsent + sent
except OSError as err:
if err.errno == 9: # "Bad file descriptor", when connection broken
socket_broken = True
@@ -53,7 +54,6 @@ class PlomSocket:
raise err
if socket_broken and not silent_connection_break:
raise BrokenSocketConnection
- totalsent = totalsent + sent
def recv(self):
"""Get full send()-prepared message from self.socket.
@@ -77,12 +77,10 @@ class PlomSocket:
data = b''
msg = b''
while True:
- data += self.socket.recv(1024)
+ data = self.socket.recv(1024)
if 0 == len(data):
- return
- cut_off = 0
+ break
for c in data:
- cut_off += 1
if esc:
msg += bytes([c])
esc = False
@@ -93,15 +91,30 @@ class PlomSocket:
yield msg.decode()
except UnicodeDecodeError:
yield None
- data = data[cut_off:]
msg = b''
else:
msg += bytes([c])
+class PlomSocketSSL(PlomSocket):
+
+ def __init__(self, *args, server_side=False, certfile=None, keyfile=None, **kwargs):
+ import ssl
+ super().__init__(*args, **kwargs)
+ if server_side:
+ self.socket = ssl.wrap_socket(self.socket, server_side=True,
+ certfile=certfile, keyfile=keyfile)
+ else:
+ self.socket = ssl.wrap_socket(self.socket)
+
+
+
class IO_Handler(socketserver.BaseRequestHandler):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
def handle(self):
"""Move messages between network socket and game IO loop via queues.
@@ -130,7 +143,13 @@ class IO_Handler(socketserver.BaseRequestHandler):
import uuid
import queue
import threading
- plom_socket = PlomSocket(self.request)
+ if self.server.socket_class == PlomSocketSSL:
+ plom_socket = self.server.socket_class(self.request,
+ server_side=True,
+ certfile=self.server.certfile,
+ keyfile=self.server.keyfile)
+ else:
+ plom_socket = self.server.socket_class(self.request)
print('CONNECTION FROM:', str(self.client_address))
connection_id = uuid.uuid4()
queue_in = queue.Queue()
@@ -155,11 +174,27 @@ class IO_Handler(socketserver.BaseRequestHandler):
class PlomTCPServer(socketserver.ThreadingTCPServer):
- """Bind together threaded IO handling server and message queue."""
+ """Bind together threaded IO handling server and message queue.
- def __init__(self, queue, port, *args, **kwargs):
- super().__init__(('localhost', port), IO_Handler, *args, **kwargs)
+ By default this only serves to localhost connections. For remote
+ connections, consider using PlomTCPServerSSL for more security,
+ which defaults to serving all connections.
+
+ """
+
+ def __init__(self, queue, port, host='127.0.0.1', *args, **kwargs):
+ super().__init__((host, port), IO_Handler, *args, **kwargs)
+ self.socket_class = PlomSocket
self.queue_out = queue
self.daemon_threads = True # Else, server's threads have daemon=False.
self.clients = {}
+
+
+class PlomTCPServerSSL(PlomTCPServer):
+
+ def __init__(self, *args, certfile=None, keyfile=None, **kwargs):
+ super().__init__(*args, host='0.0.0.0', **kwargs)
+ self.certfile = certfile
+ self.keyfile = keyfile
+ self.socket_class = PlomSocketSSL