part of close()

utcp_objects
Роман Бородин 2019-04-08 09:34:47 +03:00
parent 586e69552b
commit 18c0f7d03a
1 changed files with 19 additions and 18 deletions

View File

@ -45,6 +45,9 @@ class Connection:
def seq_inc(self, inc=1): def seq_inc(self, inc=1):
self.seq += inc self.seq += inc
return self.seq return self.seq
def set_ack(self, ack):
self.ack = ack
return ack
class TCPPacket(object): class TCPPacket(object):
def __init__(self, seq): def __init__(self, seq):
@ -249,13 +252,12 @@ class TCP(object):
except socket.error as error: except socket.error as error:
raise EOFError('Socket was closed before executing command. Error is: %s.' % error) raise EOFError('Socket was closed before executing command. Error is: %s.' % error)
def send_ack(self, connection, ack): def send_ack(self, connection, ack):
self.connections[connection].ack = ack conn = self.connections[connection]
self.connections[connection].seq += 1 ack_packet = TCPPacket(conn.seq_inc())
self.connections[connection].set_flags(ack=True) ack_packet.ack = conn.set_ack(ack)
self.connections[connection].data = b'' ack_packet.set_flags(ack=True)
packet_to_send = pickle.dumps(self.connections[connection]) packet_to_send = pickle.dumps(ack_packet)
self.own_socket.sendto(packet_to_send, connection) # after receiving correct info sends ack self.own_socket.sendto(packet_to_send, connection)
self.connections[connection].set_flags()
def listen_handler(self, max_connections): def listen_handler(self, max_connections):
try: try:
while True and self.status: while True and self.status:
@ -342,13 +344,11 @@ class TCP(object):
conn.peer_pub = simplecrypto.RsaPublicKey(peer_pub) conn.peer_pub = simplecrypto.RsaPublicKey(peer_pub)
except: except:
raise socket.error('Decrypt peer public key error') raise socket.error('Decrypt peer public key error')
ack_packet = ack_packet = TCPPacket(conn.seq_inc())
self.connections[server_address].ack = answer.seq + 1 ack_packet.ack = conn.set_ack(answer.seq + 1)
self.connections[server_address].seq += 1 ack_packet.set_flags(ack=True)
self.connections[server_address].set_flags(ack=True) second_packet_to_send = pickle.dumps(ack_packet)
second_packet_to_send = pickle.dumps(self.connections[server_address]) self.own_socket.sendto(second_packet_to_send, server_address)
self.own_socket.sendto(second_packet_to_send, list(self.connections.keys())[FIRST])
self.connections[server_address].set_flags()
except socket.error as error: except socket.error as error:
self.own_socket.close() self.own_socket.close()
@ -374,12 +374,13 @@ class TCP(object):
connection = list(self.connections.keys())[0] connection = list(self.connections.keys())[0]
else: else:
raise EOFError('Connection not in connected devices') raise EOFError('Connection not in connected devices')
self.connections[connection].set_flags(fin=True) conn = self.connections[connection]
self.connections[connection].seq += 1 fin_packet = TCPPacket(conn.seq_inc())
packet_to_send = pickle.dumps(self.connections[connection]) fin_packet.set_flags(fin=True)
packet_to_send = pickle.dumps(fin_packet)
self.own_socket.sendto(packet_to_send, connection) self.own_socket.sendto(packet_to_send, connection)
answer = self.find_correct_packet('ACK', connection) # change cause may get a None value answer = self.find_correct_packet('ACK', connection) # change cause may get a None value
self.connections[connection].ack += 1 conn.ack += 1
answer = self.find_correct_packet('FIN-ACK', connection) answer = self.find_correct_packet('FIN-ACK', connection)
if answer.flag_fin != 1: if answer.flag_fin != 1:
raise Exception('The receiver didn\'t send the fin packet') raise Exception('The receiver didn\'t send the fin packet')