From 5b92c317c1e33642ee9a574525ad59d35b79843f Mon Sep 17 00:00:00 2001 From: Jakob Ketterl Date: Tue, 18 May 2021 20:44:05 +0200 Subject: [PATCH] improve connection timeout handling --- owrx/connection.py | 3 ++- owrx/websocket.py | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/owrx/connection.py b/owrx/connection.py index f4146b2..94ce721 100644 --- a/owrx/connection.py +++ b/owrx/connection.py @@ -16,7 +16,7 @@ from owrx.waterfall import WaterfallOptions from owrx.websocket import Handler from queue import Queue, Full, Empty from js8py import Js8Frame -from abc import ABC, ABCMeta, abstractmethod +from abc import ABCMeta, abstractmethod import json import threading @@ -517,6 +517,7 @@ class HandshakeMessageHandler(Handler): logger.warning("invalid connection type: %s", handshake["type"]) if client is not None: + logger.debug("handing off connection handling to %s", type(client).__name__) # hand off all further communication to the correspondig connection conn.send("CLIENT DE SERVER server=openwebrx version={version}".format(version=openwebrx_version)) conn.setMessageHandler(client) diff --git a/owrx/websocket.py b/owrx/websocket.py index b841bb6..7aa3b86 100644 --- a/owrx/websocket.py +++ b/owrx/websocket.py @@ -137,10 +137,10 @@ class WebSocketConnection(object): self._sendBytes(data_to_send) def _sendBytes(self, data_to_send): - def chunks(l, n): - """Yield successive n-sized chunks from l.""" - for i in range(0, len(l), n): - yield l[i : i + n] + def chunks(input, n): + """Yield successive n-sized chunks from input.""" + for i in range(0, len(input), n): + yield input[i: i + n] try: with self.sendLock: @@ -151,9 +151,11 @@ class WebSocketConnection(object): if written != len(chunk): logger.error("incomplete write! closing socket!") self.close() + break else: logger.debug("socket not returned from select; closing") self.close() + break # these exception happen when the socket is closed except OSError: logger.exception("OSError while writing data") @@ -214,9 +216,10 @@ class WebSocketConnection(object): length = (header[0] << 8) + header[1] if mask: masking_key = protected_read(4) - data = protected_read(length) - if mask: + data = protected_read(length) data = bytes([b ^ masking_key[index % 4] for (index, b) in enumerate(data)]) + else: + data = protected_read(length) if opcode == OPCODE_TEXT_MESSAGE: message = data.decode("utf-8") try: