X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fntp.cpp;h=720cfb14583d633361ec72f3b2ebb29e849dcfb9;hb=f2bcacaf01a0c77083fdf9d1baa6a201078c1efd;hp=586aa34075e76a92cfe76b6a2c0d913369473475;hpb=588e63208c3a95f5fd4389e230b9a07442742071;p=novacoin.git diff --git a/src/ntp.cpp b/src/ntp.cpp index 586aa34..720cfb1 100644 --- a/src/ntp.cpp +++ b/src/ntp.cpp @@ -1,9 +1,9 @@ -#include #ifdef WIN32 #include #else #include +#include #include #include #include @@ -13,6 +13,7 @@ #endif #include "netbase.h" +#include "util.h" extern int GetRandInt(int nMax); @@ -57,12 +58,6 @@ inline void Ntp2Unix(uint32_t &n, time_t &u) u = n - 0x83aa7e80; // 2208988800 1970 - 1900 in seconds } -inline void HTONL_FP(l_fp *h, l_fp *n) -{ - (n)->Ul_i.Xl_ui = htonl((h)->Ul_i.Xl_ui); - (n)->Ul_f.Xl_uf = htonl((h)->Ul_f.Xl_uf); -} - inline void ntohl_fp(l_fp *n, l_fp *h) { (h)->Ul_i.Xl_ui = ntohl((n)->Ul_i.Xl_ui); @@ -254,6 +249,7 @@ bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, return false; // "connection" error } + *pcliaddr = *((struct sockaddr *) &servaddr); servlen = sizeof(servaddr); @@ -263,6 +259,20 @@ bool InitWithHost(std::string &strHostName, SOCKET &sockfd, socklen_t &servlen, int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) { + +#ifdef WIN32 + u_long nOne = 1; + if (ioctlsocket(sockfd, FIONBIO, &nOne) == SOCKET_ERROR) + { + printf("ConnectSocket() : ioctlsocket non-blocking setting failed, error %d\n", WSAGetLastError()); +#else + if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == SOCKET_ERROR) + { + printf("ConnectSocket() : fcntl non-blocking setting failed, error %d\n", errno); +#endif + return -2; + } + struct pkt *msg = new pkt; struct pkt *prt = new pkt; @@ -283,8 +293,26 @@ int64_t DoReq(SOCKET sockfd, socklen_t servlen, struct sockaddr cliaddr) msg->xmt.Ul_f.Xl_f=0; int len=48; - sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen); - int n = recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL); + int retcode = sendto(sockfd, (char *) msg, len, 0, &cliaddr, servlen); + if (retcode < 0) { + printf("sendto() failed: %d", retcode); + return -3; + } + + retcode = 0; + int nWait = 0; + + while(retcode <= 0) { + Sleep(1000); + retcode = recvfrom(sockfd, (char *) msg, len, 0, NULL, NULL); + + if (nWait > 4) { + printf("recvfrom() timeout"); + return -4; + } + + nWait++; + } ntohl_fp(&msg->rec, &prt->rec); ntohl_fp(&msg->xmt, &prt->xmt);