diff --git a/iguana/exchanges/LP_nativeDEX.c b/iguana/exchanges/LP_nativeDEX.c index a1dadda84..798cda041 100644 --- a/iguana/exchanges/LP_nativeDEX.c +++ b/iguana/exchanges/LP_nativeDEX.c @@ -482,7 +482,7 @@ void LPinit(uint16_t myport,uint16_t mypullport,uint16_t mypubport,double profit nn_close(pullsock); pullsock = LP_initpublicaddr(ctx,&mypullport,pushaddr,myipaddr,mypullport,0); LP_deadman_switch = (uint32_t)time(NULL); - LP_forwarding_register(LP_mypubkey,pushaddr,mypullport,100000); + LP_forwarding_register(LP_mypubkey,pushaddr,mypullport,MAX_PSOCK_PORT); } } } diff --git a/iguana/exchanges/LP_network.c b/iguana/exchanges/LP_network.c index b482dee2b..cf95fc9db 100644 --- a/iguana/exchanges/LP_network.c +++ b/iguana/exchanges/LP_network.c @@ -19,6 +19,8 @@ // #define PSOCK_KEEPALIVE 600 +#define MAX_PSOCK_PORT 60000 +#define MIN_PSOCK_PORT 10000 struct psock { @@ -28,7 +30,7 @@ struct psock char sendaddr[128],publicaddr[128]; } *PSOCKS; -uint16_t Numpsocks,Psockport = 10000; +uint16_t Numpsocks,Psockport = MIN_PSOCK_PORT; char *nanomsg_transportname(int32_t bindflag,char *str,char *ipaddr,uint16_t port) { @@ -47,7 +49,7 @@ int32_t LP_send(int32_t sock,char *msg,int32_t freeflag) return(-1); } len = (int32_t)strlen(msg) + 1; - for (i=0; i<1000; i++) + for (i=0; i<1000; i++) // 1000 * (1 ms + 1000 us) = 2 seconds { pfd.fd = sock; pfd.events = NN_POLLOUT; @@ -66,8 +68,6 @@ int32_t LP_send(int32_t sock,char *msg,int32_t freeflag) usleep(1000); } printf("error LP_send sock.%d, i.%d timeout.(%s) %s\n",sock,i,msg,nn_strerror(nn_errno())); - //if ( (sentbytes= nn_send(sock,msg,len,0)) != len ) - // printf("LP_send sent %d instead of %d\n",sentbytes,len); if ( freeflag != 0 ) free(msg); return(-1); @@ -132,9 +132,9 @@ void LP_psockloop(void *_ptr) else if ( Numpsocks > 0 ) { if ( pfds == 0 ) - pfds = calloc(60000,sizeof(*pfds)); + pfds = calloc(MAX_PSOCK_PORT,sizeof(*pfds)); portable_mutex_lock(&LP_psockmutex); - memset(pfds,0,sizeof(*pfds) * ((Numpsocks < 30000) ? Numpsocks*2 : 60000)); + memset(pfds,0,sizeof(*pfds) * ((Numpsocks*2 <= MAX_PSOCK_PORT) ? Numpsocks*2 : MAX_PSOCK_PORT)); for (iter=0; iter<2; iter++) { for (i=n=0; i 60000 ) - Psockport = 10000; - if ( i == 100 ) + if ( Psockport > MAX_PSOCK_PORT ) + Psockport = MIN_PSOCK_PORT; + if ( i == maxiters ) jaddstr(retjson,"error","cant find psock ports"); return(jprint(retjson,1)); } @@ -419,38 +421,41 @@ int32_t LP_initpublicaddr(void *ctx,uint16_t *mypullportp,char *publicaddr,char printf("try to get publicaddr again\n"); } } - if ( (pullsock= nn_socket(AF_SP,nntype)) >= 0 ) + while ( 1 ) { - if ( LP_canbind == 0 ) + if ( (pullsock= nn_socket(AF_SP,nntype)) >= 0 ) { - if ( nn_connect(pullsock,connectaddr) < 0 ) + if ( LP_canbind == 0 ) { - printf("bind to %s error for %s: %s\n",connectaddr,publicaddr,nn_strerror(nn_errno())); - exit(-1); - } else printf("nntype.%d NN_PAIR.%d connect to %s connectsock.%d\n",nntype,NN_PAIR,connectaddr,pullsock); - } - else - { - if ( nn_bind(pullsock,bindaddr) < 0 ) + if ( nn_connect(pullsock,connectaddr) < 0 ) + { + printf("bind to %s error for %s: %s\n",connectaddr,publicaddr,nn_strerror(nn_errno())); + exit(-1); + } else printf("nntype.%d NN_PAIR.%d connect to %s connectsock.%d\n",nntype,NN_PAIR,connectaddr,pullsock); + } + else { - printf("bind to %s error for %s: %s\n",bindaddr,publicaddr,nn_strerror(nn_errno())); - exit(-1); + if ( nn_bind(pullsock,bindaddr) < 0 ) + { + printf("bind to %s error for %s: %s\n",bindaddr,publicaddr,nn_strerror(nn_errno())); + exit(-1); + } } + timeout = 1; + nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); + timeout = 1; + nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); + //maxsize = 2 * 1024 * 1024; + //nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)); + if ( nntype == NN_SUB ) + nn_setsockopt(pullsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); } - timeout = 1; - nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVTIMEO,&timeout,sizeof(timeout)); - timeout = 1; - nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_SNDTIMEO,&timeout,sizeof(timeout)); - maxsize = 2 * 1024 * 1024; - nn_setsockopt(pullsock,NN_SOL_SOCKET,NN_RCVBUF,&maxsize,sizeof(maxsize)); - if ( nntype == NN_SUB ) - nn_setsockopt(pullsock,NN_SUB,NN_SUB_SUBSCRIBE,"",0); - //LP_send(pullsock,"hello init",0); - } - if ( LP_canbind == 0 && ispaired == 0 && nn_tests(ctx,pullsock,publicaddr,NN_PUSH) < 0 ) - { - printf("command socket didnt work\n"); - exit(-1); + if ( LP_canbind != 0 || ispaired != 0 || nn_tests(ctx,pullsock,publicaddr,NN_PUSH) >= 0 ) + break; + printf("nn_tests failed, try again\n"); + sleep(3); + if ( pullsock >= 0 ) + nn_close(pullsock); } return(pullsock); }