tMake the client reconnect RETRY times - synk - synchronize files between hosts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 5634711a7f8b6ef5b1d2d011697c21f3d4223b84
 (DIR) parent cc46dfd595f112a68c17b7d01be792228e506aec
 (HTM) Author: Willy <willyatmailoodotorg>
       Date:   Sat,  3 Sep 2016 23:36:20 +0200
       
       Make the client reconnect RETRY times
       
       Diffstat:
         M synk.c                              |      17 ++++++++++++-----
       
       1 file changed, 12 insertions(+), 5 deletions(-)
       ---
 (DIR) diff --git a/synk.c b/synk.c
       t@@ -1,3 +1,4 @@
       +#include <errno.h>
        #include <limits.h>
        #include <stdarg.h>
        #include <stdint.h>
       t@@ -25,6 +26,7 @@
        #define CONNECTION_MAX 1
        #define RECVSIZ        512
        #define TIMEOUT        5
       +#define RETRY          8
        
        /* hold a socket connection, used to pass a connection to a thread */
        struct client_t {
       t@@ -373,7 +375,7 @@ uptodate(struct peers_t *plist)
        int
        getpeermeta(struct peer_t *clt, struct metadata_t *local)
        {
       -        int cfd;
       +        int i, cfd;
                ssize_t r, len = 0;
        
                if ((cfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
       t@@ -381,9 +383,15 @@ getpeermeta(struct peer_t *clt, struct metadata_t *local)
                        return -1;
                }
        
       -        if (connect(cfd, (struct sockaddr *) &(clt->peer), sizeof(clt->peer)) < 0) {
       -                perror(inet_ntoa(clt->peer.sin_addr));
       -                return -1;
       +        for (i=0; i<RETRY; i++) {
       +                if (!connect(cfd, (struct sockaddr *) &(clt->peer), sizeof(clt->peer)))
       +                        break;
       +
       +                if (errno != ECONNREFUSED || i+1 >= RETRY) {
       +                        perror(inet_ntoa(clt->peer.sin_addr));
       +                        return -1;
       +                }
       +                usleep(250000);
                }
        
                if (write(cfd, local, sizeof(struct metadata_t)) < 0) {
       t@@ -564,7 +572,6 @@ main(int argc, char *argv[])
                case SYNK_CLIENT:
                        while ((fn = *(argv++)) != NULL) {
                                spawnremote(&plist);
       -                        sleep(1);
                                syncfile(&plist, fn);
                        }
                        break;