Переглянути джерело

继续优化代码,增加超时处理

guohui 1 рік тому
батько
коміт
4dacd3e062
3 змінених файлів з 25 додано та 11 видалено
  1. 8 2
      pro/src/app.c
  2. 4 1
      pro/src/common.c
  3. 13 8
      pro/src/ntpclient.c

+ 8 - 2
pro/src/app.c

@@ -1787,11 +1787,16 @@ static void* ntp_thread(void *arg)
     {
         if (pntp->mode > 0)
         {
-            if (ntp_sync_flag || ((time1 - time2) >= NTP_INTERVAL_TIME))
+            printf("___ntp_sync_flag: %d, interval: %d\n",  ntp_sync_flag, abs(time1-time2));
+            if (ntp_sync_flag || (abs(time1 - time2) >= NTP_INTERVAL_TIME))
             {
-                pc = createNTPClient(pntp->ntpaddress, pntp->port, NTP_TIMEOUT);
+                if(pc==NULL) {
+                    pc = createNTPClient(pntp->ntpaddress, pntp->port, NTP_TIMEOUT);
+                }
+
                 if (pc)
                 {
+                    printf("0000000\n");
                     if (getCurrentTimeFromNTP(pc, &utc_time) == 0)
                     {
                         time_t host_time = time(NULL);
@@ -1805,6 +1810,7 @@ static void* ntp_thread(void *arg)
                     {
                         log_e("getCurrentTimeFromNTP failed!");
                     }
+                    printf("2222222\n");
                     destroyNTPClient(pc);
                     pc = NULL;
                 }

+ 4 - 1
pro/src/common.c

@@ -462,12 +462,15 @@ int set_zone(int zone)
 
 void print_time(char *s, struct tm *tm)
 {
-    printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
+    printf("___%s___, %04d-%02d-%02d %02d:%02d:%02d\n", s, tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
 }
 
 void print_utc(char *s, time_t t)
 {
     struct tm *tm=gmtime(&t);
+
+    tm->tm_year += 1900;
+    tm->tm_mon += 1;
     print_time(s, tm);
 }
 

+ 13 - 8
pro/src/ntpclient.c

@@ -23,8 +23,8 @@ static int sendNTPRequest(NTPClient* client) {
 }  
   
 // 接收NTP响应  
-static int receiveNTPResponse(NTPClient* client) {  
-    socklen_t len = sizeof(client->server_addr);  
+static int receiveNTPResponse(NTPClient* client) {
+    socklen_t len = sizeof(client->server_addr);
     ssize_t receivedBytes = recvfrom(client->sockfd, client->ntp_packet, NTP_PACKET_SIZE, 0,  
                               &client->server_addr, &len);  
     if (receivedBytes < 0) {  
@@ -81,9 +81,6 @@ static void *tmp_thread(void *arg)
     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
     pthread_testcancel();
 
-    pc->res = NULL;
-    pc->flag = 0;
-
     sprintf(tmp, "%d", pc->port);
     printf("___getaddrinfo in\n");
     err = getaddrinfo(pc->server, tmp, &hints, &pc->res);
@@ -125,9 +122,11 @@ NTPClient* createNTPClient(const char* ntp_server, int port, int timeout_sec) {
     pthread_t tid;
     client->server = (char*)ntp_server;
     client->port = port;
+    client->res = NULL;
+    client->flag = 0;
     pthread_create(&tid, NULL, tmp_thread, client);
     for(int i=0; i<timeout_sec; i++) {
-        if(client->flag==1) {
+        if(client->flag) {
             break;
         }
         sleep(1);
@@ -139,10 +138,16 @@ NTPClient* createNTPClient(const char* ntp_server, int port, int timeout_sec) {
             pthread_join(tid, NULL);
             printf("___cancel ok!\n");
         }
-        freeaddrinfo(client->res);
+        if(client->res) freeaddrinfo(client->res);
         free(client); return NULL;
     }
-    freeaddrinfo(client->res);
+    if(client->res) freeaddrinfo(client->res);
+
+    struct timeval tv;
+    tv.tv_sec = timeout_sec;
+    tv.tv_usec = 0;
+    setsockopt(client->sockfd, SOL_SOCKET, SO_RCVTIMEO, (const void*)&tv, sizeof(tv));
+
     printf("___createNTPClient ok!\n");
   
     return client;