fully working version

utcp_uuid
Роман Бородин 2019-04-08 23:38:35 +03:00
parent 11b7ec1ee2
commit 8ef5153e2e
1 changed files with 21 additions and 14 deletions

View File

@ -242,6 +242,8 @@ class TCP(object):
self.port = addr[1] self.port = addr[1]
self.own_socket.bind(addr) self.own_socket.bind(addr)
def send(self, data, connection=None): def send(self, data, connection=None):
if self.closed:
raise EOFError
try: try:
if connection not in list(self.connections.keys()): if connection not in list(self.connections.keys()):
if connection is None: if connection is None:
@ -259,6 +261,8 @@ 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 recv(self, size, connection=None): def recv(self, size, connection=None):
if self.closed:
raise EOFError
try: try:
if connection not in list(self.connections.keys()): if connection not in list(self.connections.keys()):
if connection is None: if connection is None:
@ -327,10 +331,12 @@ class TCP(object):
t.start() t.start()
except Exception as error: except Exception as error:
raise EOFError('Something went wrong in listen func! Error is: %s.' % str(error)) raise EOFError('Something went wrong in listen func! Error is: %s.' % str(error))
def stop(self):
self.own_socket.close()
self.status = 0
def accept(self): def accept(self):
while self.status:
try: try:
while True:
self.new_conn_event.wait(0.1) self.new_conn_event.wait(0.1)
if self.connection_queue: if self.connection_queue:
with self.queue_lock: with self.queue_lock:
@ -341,7 +347,12 @@ class TCP(object):
syn_ack.set_pub(conn.peer_pub.encrypt_raw(conn.pubkey)) syn_ack.set_pub(conn.peer_pub.encrypt_raw(conn.pubkey))
answer = self.__send_packet(conn.peer_addr, syn_ack, retransmit=True) answer = self.__send_packet(conn.peer_addr, syn_ack, retransmit=True)
return ConnectedSOCK(self, conn.peer_addr), conn.peer_addr return ConnectedSOCK(self, conn.peer_addr), conn.peer_addr
except EOFError:
if conn.peer_addr in self.connections:
self.close(conn.peer_addr)
continue
except Exception as error: except Exception as error:
if conn.peer_addr in self.connections:
self.close(conn.peer_addr) self.close(conn.peer_addr)
raise EOFError('Something went wrong in accept func: ' + str(error)) raise EOFError('Something went wrong in accept func: ' + str(error))
@ -380,7 +391,7 @@ class TCP(object):
return self.own_socket.fileno() return self.own_socket.fileno()
@property @property
def closed(self): def closed(self):
return self.own_socket._closed return not bool(len(self.connections))
def drop_connection(self, connection): def drop_connection(self, connection):
with self.connection_lock: with self.connection_lock:
if len(self.connections): if len(self.connections):
@ -398,12 +409,9 @@ class TCP(object):
if not answer: if not answer:
raise Exception('The receiver didn\'t send the fin packet') raise Exception('The receiver didn\'t send the fin packet')
else: else:
ack = Ack(fin.id)
self.__send_packet(connection, ack)
self.drop_connection(connection) self.drop_connection(connection)
if len(self.connections) == 0 and self.client: if len(self.connections) == 0 and self.client:
self.own_socket.close() self.stop()
self.status = 0
except Exception as error: except Exception as error:
raise EOFError('Something went wrong in the close func! Error is: %s.' % error) raise EOFError('Something went wrong in the close func! Error is: %s.' % error)
@ -413,7 +421,7 @@ class TCP(object):
self.__send_packet(connection, ack) self.__send_packet(connection, ack)
fin_ack = FinAck(fin_id) fin_ack = FinAck(fin_id)
try: try:
answer = self.__send_packet(connection, fin_ack, retransmit=True) answer = self.__send_packet(connection, fin_ack)
except: except:
pass pass
with self.connection_lock: with self.connection_lock:
@ -473,7 +481,7 @@ class TCP(object):
return packet return packet
except KeyError: except KeyError:
not_found = True not_found = True
self.incoming_packet_event.wait(0.1) self.incoming_packet_event.wait(0.5)
def blink_incoming_packet_event(self): def blink_incoming_packet_event(self):
self.incoming_packet_event.set() self.incoming_packet_event.set()
self.incoming_packet_event.clear() self.incoming_packet_event.clear()
@ -526,7 +534,6 @@ class TCP(object):
except socket.error as error: except socket.error as error:
self.own_socket.close() self.own_socket.close()
self.status = 0 self.status = 0
# print('An error has occured: Socket error %s' % error)
def central_receive(self): def central_receive(self):
t = threading.Thread(target=self.central_receive_handler) t = threading.Thread(target=self.central_receive_handler)