From e32837031f6fd5357ce724db3c988281b69699f5 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 6 Jul 2017 22:31:16 -0300 Subject: [PATCH] Implemented timeout to avoid hang due to connection error. --- include/libp2p/peer/peer.h | 2 +- peer/peer.c | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/libp2p/peer/peer.h b/include/libp2p/peer/peer.h index a17aeb4..9c82461 100644 --- a/include/libp2p/peer/peer.h +++ b/include/libp2p/peer/peer.h @@ -47,7 +47,7 @@ void libp2p_peer_free(struct Libp2pPeer* in); * @param peer the peer to connect to * @returns true(1) on success, false(0) if we could not connect */ -int libp2p_peer_connect(struct Libp2pPeer* peer); +int libp2p_peer_connect(struct Libp2pPeer* peer, int timeout); /** * Make a copy of a peer diff --git a/peer/peer.c b/peer/peer.c index 8c2784f..a1a2d68 100644 --- a/peer/peer.c +++ b/peer/peer.c @@ -1,4 +1,5 @@ #include +#include #include "libp2p/peer/peer.h" #include "libp2p/utils/linked_list.h" @@ -48,7 +49,8 @@ struct Libp2pPeer* libp2p_peer_new_from_multiaddress(const struct MultiAddress* * @param peer the peer to connect to * @returns true(1) on success, false(0) if we could not connect */ -int libp2p_peer_connect(struct Libp2pPeer* peer) { +int libp2p_peer_connect(struct Libp2pPeer* peer, int timeout) { + time_t now, prev = time(NULL); // find an appropriate address struct Libp2pLinkedList* current_address = peer->addr_head; while (current_address != NULL && peer->connection_type != CONNECTION_TYPE_CONNECTED) { @@ -64,6 +66,9 @@ int libp2p_peer_connect(struct Libp2pPeer* peer) { } free(ip); } // is IP + now = time(NULL); + if (now >= (prev + timeout)) + break; } // trying to connect return peer->connection_type == CONNECTION_TYPE_CONNECTED; }