Implemented timeout to avoid hang due to connection error.

This commit is contained in:
Jose Marcial Vieira Bisneto 2017-07-06 22:31:16 -03:00
parent e51643a8f4
commit e32837031f
2 changed files with 7 additions and 2 deletions

View file

@ -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

View file

@ -1,4 +1,5 @@
#include <stdlib.h>
#include <time.h>
#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;
}