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