From 4c12ffc3756988a3cb46e65dcdfb356e9d5fb926 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Fri, 9 Apr 2021 18:51:02 +0200 Subject: [PATCH] Additional thread for sending --- clientBlaster.py | 50 +++++++++++++++++++++++----- modules/[text server]/main/module.py | 6 ++-- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/clientBlaster.py b/clientBlaster.py index 4c54490..69803f3 100644 --- a/clientBlaster.py +++ b/clientBlaster.py @@ -21,6 +21,7 @@ sp = pUp(s) import threading import socket import struct +import time addr = ("127.0.0.1",21779) @@ -79,20 +80,48 @@ def addEventHandler(event,func): def sendResponse(connection,data): connection.sendall(len(data).to_bytes(4,"big") + data) -class connectionThread(threading.Thread): - global threadsLock +class senderThread(threading.Thread): + def __init__(self,connectionThread): + threading.Thread.__init__(self) + self.lock = threading.Lock() + with self.lock: + self.connectionThread = connectionThread + self.queue = [] + def closeThread(self): + with self.lock: + self.queue = [["close"]] + + def addToQueue(self,entry): + with self.lock: + self.queue.append(entry) + + def run(self): + while True: + time.sleep(0.0333) + with self.lock: + if len(self.queue) == 0: continue + for entry in self.queue: + if entry[0] == "close": return + entry[0](*entry[1],**entry[2]) + self.queue = [] + +class connectionThread(threading.Thread): def __init__(self,threadId,connection,address): threading.Thread.__init__(self) - self.threadId = threadId - self.connection = connection - self.address = address - self.closed = False - self.user = False self.lock = threading.Lock() + with self.lock: + self.threadId = threadId + self.connection = connection + self.address = address + self.closed = False + self.user = False + self.senderThread = senderThread(self) + self.senderThread.start() def closeThread(self): with self.lock, threadsLock: + self.senderThread.closeThread() try: self.connection.close() except: @@ -103,8 +132,11 @@ class connectionThread(threading.Thread): print("thread closed: " +str(self.threadId)+ " (open: " +str(len(threads))+ ")") self.closed = True + def sendResponse(self,data): + with self.lock: + self.senderThread.addToQueue([sendResponse,[self.connection,data],{}]) + def run(self): - # inform about connection with self.lock: print("thread opened: " +", ".join((str(self.threadId),str(self.address)))) @@ -179,6 +211,8 @@ def main(): global close while True: connection, address = socketServer.accept() + + # inform about connection with threadsLock: if close: break cancel = triggerEvent("onConnect",connection,address) diff --git a/modules/[text server]/main/module.py b/modules/[text server]/main/module.py index 7b6c219..0779762 100644 --- a/modules/[text server]/main/module.py +++ b/modules/[text server]/main/module.py @@ -43,7 +43,7 @@ def textOnRequest(event,self,requestLength): response = textCommandRun(self,textCommandToList(text)) print("response: " +textListToCommand(response)) - sendResponse(self.connection,textListToCommand(response).encode("utf-8")) + self.sendResponse(textListToCommand(response).encode("utf-8")) self.connection.settimeout(textKeepAliveTimeout) addEventHandler("onRequest",textOnRequest) @@ -52,8 +52,8 @@ global textOnException def textOnException(event,self,exc): self.connection.settimeout(textTimeout) if type(exc) == socket.timeout: - sendResponse(self.connection,textListToCommand(["error","fatal","timeout"]).encode("utf-8")) + self.sendResponse(textListToCommand(["error","fatal","timeout"]).encode("utf-8")) return - sendResponse(self.connection,textListToCommand(["error","fatal","unhandled",str(exc)]).encode("utf-8")) + self.sendResponse(textListToCommand(["error","fatal","unhandled",str(exc)]).encode("utf-8")) addEventHandler("onException",textOnException) \ No newline at end of file