Outbound queue for client

This commit is contained in:
Fierelier 2021-04-14 22:03:53 +02:00
parent ffbc7d1d0e
commit 40414cb9a5

View File

@ -24,6 +24,7 @@ import threading
import queue import queue
bufferSize = 1000 # buffer size in bytes bufferSize = 1000 # buffer size in bytes
maxAccumulatedData = 50*1000*1000 # How much data can be in an outbound thread's queue at maximum before the connection is closed?
timeout = 15 # timeout in seconds timeout = 15 # timeout in seconds
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -37,6 +38,28 @@ class stdoutThread(threading.Thread):
data = self.queue.get(timeout=timeout) data = self.queue.get(timeout=timeout)
sys.stdout.buffer.write(data) sys.stdout.buffer.write(data)
class stdinThread(threading.Thread):
def __init__(self,connection):
threading.Thread.__init__(self)
self.queue = queue.Queue()
self.connection = connection
def run(self):
try:
while True:
accumulatedData = self.queue.qsize() * bufferSize
if accumulatedData > maxAccumulatedData:
print("Accumulated data limit reached. Closing.")
self.connection.close()
self.queue = False
return
data = self.queue.get()
self.connection.sendall(data)
except:
self.connection.close()
self.queue = False
raise
def listToCommand(lst): def listToCommand(lst):
cmd = "" cmd = ""
for arg in lst: for arg in lst:
@ -65,17 +88,27 @@ def main():
connection.sendall(makePayload(sys.argv[2:])) connection.sendall(makePayload(sys.argv[2:]))
if sys.argv[2] == "watch": if sys.argv[2] == "watch":
stdoutThr = stdoutThread() try:
stdoutThr.start() stdoutThr = stdoutThread()
while True: stdoutThr.start()
data = connection.recv(bufferSize) while True:
if data == b"": return data = connection.recv(bufferSize)
stdoutThr.queue.put(data) if data == b"": return
stdoutThr.queue.put(data)
except:
connection.close()
raise
if sys.argv[2] == "broadcast": if sys.argv[2] == "broadcast":
while True: try:
data = sys.stdin.buffer.read(bufferSize) stdinThr = stdinThread(connection)
connection.sendall(data) stdinThr.start()
while True:
data = sys.stdin.buffer.read(bufferSize)
stdinThr.queue.put(data)
except:
connection.close()
raise
if __name__ == '__main__': if __name__ == '__main__':
main() main()