diff --git a/build-standalone/MorphoNet_Standalone.py b/build-standalone/MorphoNet_Standalone.py index b532635ab20eaacc88f9e11b7f2cbf2e212a4b53..29153f1b989f8ef12473536b5c15019c2a6782e1 100644 --- a/build-standalone/MorphoNet_Standalone.py +++ b/build-standalone/MorphoNet_Standalone.py @@ -382,7 +382,6 @@ if __name__ == "__main__": self.server_lineage_thread = None def manage_command(self, msg): - print("manage command "+str(msg)) command = msg action = _get_param(command, "action") objects = _get_param(command, "objects").split(";") diff --git a/morphonet/plot/SocketServer.py b/morphonet/plot/SocketServer.py index 76824069a91eba5200ad66c65bcb4b40ef8e7e3b..f7fd29ca75ee2aa0b8a85ccbcb9b128c4fb5195e 100644 --- a/morphonet/plot/SocketServer.py +++ b/morphonet/plot/SocketServer.py @@ -83,80 +83,83 @@ class _MorphoServer(Thread): # bind the socket to a public host, and a well-known port s.bind((self.host, self.port)) # become a server socket - s.listen(5) - connection, addr = s.accept() - with connection: - # setup parameters - message = None - temp_msg = bytearray(0) - index = 0 - msg_size = -1 - data = None - while True: - if self.exit: # if flag raised: stop looping - s.close() - return - data = connection.recv(1024) - if data != b'': - prepared_buffer = data - if message is None: # if message is not initialized: we read the header and init msg size - temp_msg2 = temp_msg + data - head = temp_msg2.decode("utf-8") - # find message size - if ";" not in head: - print("ERROR: could not receive header of request.") - return - else: - headstr = head.split(";")[0] - msg_size = int(headstr) - message = bytearray(msg_size) - index = 0 - prepared_buffer = data[len(headstr)+1:] + s.listen() + while True: + connection, addr = s.accept() + with connection: + # setup parameters + message = None + temp_msg = bytearray(0) + index = 0 + msg_size = -1 + data = None + while True: + if self.exit: # if flag raised: stop looping + s.close() + return + data = connection.recv(1024) + if not data: + break + if data != b'': + prepared_buffer = data + if message is None: # if message is not initialized: we read the header and init msg size + temp_msg2 = temp_msg + data + head = temp_msg2.decode("utf-8") + # find message size + if ";" not in head: + print("ERROR: could not receive header of request.") + return + else: + headstr = head.split(";")[0] + msg_size = int(headstr) + message = bytearray(msg_size) + index = 0 + prepared_buffer = data[len(headstr)+1:] - # then we start reading the buffer - if message is not None: - if index + len(prepared_buffer) >= len(message): # if we are going over the msg size: - # just fill the message with enough bytes to fill it - message[index:] = prepared_buffer[:len(message) - index] - self.manage_command(message) # manage message (means msg is fully received) - if len(message) - index < len(prepared_buffer): # if there is still data in buffer - overall_index = len(message) - index - while True: # for as long as you can: - buf = prepared_buffer[overall_index:] # put remaining data in new buffer - head = buf.decode("utf-8") - if ";" not in head: # if there is NO header in remaining data - # then put buffer in temp data for next iteration and reset params - temp_msg = buf - message = None - msg_size = -1 - break - else: - headstr = head.split(";")[0] - msg_size = int(headstr) - message = bytearray(msg_size) - index = 0 - # if the message is longer than the remaining buffer: - if len(buf) - (len(headstr) + 1) < len(message): - # just fill with data minus header - message[:len(buf) - (len(headstr) + 1)] = buf[(len(headstr) + 1):] - index = len(buf) - (len(headstr) + 1) - #also break the infinite loop: nothing else to read + # then we start reading the buffer + if message is not None: + if index + len(prepared_buffer) >= len(message): # if we are going over the msg size: + # just fill the message with enough bytes to fill it + message[index:] = prepared_buffer[:len(message) - index] + self.manage_command(message) # manage message (means msg is fully received) + if len(message) - index < len(prepared_buffer): # if there is still data in buffer + overall_index = len(message) - index + while True: # for as long as you can: + buf = prepared_buffer[overall_index:] # put remaining data in new buffer + head = buf.decode("utf-8") + if ";" not in head: # if there is NO header in remaining data + # then put buffer in temp data for next iteration and reset params + temp_msg = buf + message = None + msg_size = -1 break - else: # otherwise the whole message is in remaining buffer: - message[0:] = buf[(len(headstr) + 1):len(message) + (len(headstr) + 1)] - index = len(message) - self.manage_command(message) # take care of message - # and add to overall index for next iteration of loop - overall_index += index + (len(headstr) + 1) + else: + headstr = head.split(";")[0] + msg_size = int(headstr) + message = bytearray(msg_size) + index = 0 + # if the message is longer than the remaining buffer: + if len(buf) - (len(headstr) + 1) < len(message): + # just fill with data minus header + message[:len(buf) - (len(headstr) + 1)] = buf[(len(headstr) + 1):] + index = len(buf) - (len(headstr) + 1) + #also break the infinite loop: nothing else to read + break + else: # otherwise the whole message is in remaining buffer: + message[0:] = buf[(len(headstr) + 1):len(message) + (len(headstr) + 1)] + index = len(message) + self.manage_command(message) # take care of message + # and add to overall index for next iteration of loop + overall_index += index + (len(headstr) + 1) - else: # if message was finished: reset parameters - msg_size = -1 - index = 0 - message = None - else: # else if message is still not finished after the buffer: - # just put the whole buffer in the message - message[index:index+len(prepared_buffer)] = prepared_buffer - index += len(prepared_buffer) + else: # if message was finished: reset parameters + msg_size = -1 + index = 0 + message = None + else: # else if message is still not finished after the buffer: + # just put the whole buffer in the message + message[index:index+len(prepared_buffer)] = prepared_buffer + index += len(prepared_buffer) except Exception as e: print(f"ERROR RECEIVE : {e}")