Mentions légales du service

Skip to content
Snippets Groups Projects
Commit e95a8332 authored by Tao Laurent's avatar Tao Laurent
Browse files

proper loop restart connection when lost

parent 2bad43c7
Branches
No related tags found
No related merge requests found
......@@ -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(";")
......
......@@ -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}")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment