diff --git a/fstream-server.py b/fstream-server.py index 098d6ec..39f08e6 100644 --- a/fstream-server.py +++ b/fstream-server.py @@ -101,28 +101,33 @@ class inThread(threading.Thread): self.connection = connection self.address = address - def closeThread(self): + def closeThread(self,closeConnection = True): with threadsLock: - for thread in outThreads: - thread = outThreads[thread] - if thread.user == self.user: - try: - thread.connection.close() - except Exception as e: - print("closing a connection failed: " +str(e)) - pass - - try: - self.connection.close() - except Exception as e: - print("closing a connection failed: " +str(e)) - pass + if closeConnection: + for thread in outThreads: + thread = outThreads[thread] + if thread.user == self.user: + try: + thread.connection.close() + except Exception as e: + print("closing a connection failed: " +str(e)) + pass + + try: + self.connection.close() + except Exception as e: + print("closing a connection failed: " +str(e)) + pass del inThreads[str(self.threadId)] def run(self): try: global threadId - data = self.connection.recv(1000).decode("utf-8") + data = self.connection.recv(1000) + if data == b"": + self.closeThread() + return + data = data.decode("utf-8") while data[-1] == " ": data = data[:-1] args = commandToList(data) cmd = args.pop(0) @@ -149,8 +154,10 @@ class inThread(threading.Thread): with threadsLock: thread = outThread(self.threadId,self.connection,self.address,self.user) - outThreads[str(threadId)] = thread + outThreads[str(self.threadId)] = thread thread.start() + + self.closeThread(False) return if cmd == "broadcast": @@ -160,6 +167,9 @@ class inThread(threading.Thread): while True: data = self.connection.recv(bufferSize) + if data == b"": + self.closeThread() + return with threadsLock: for thread in outThreads: thread = outThreads[thread]