summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2020-08-21 21:41:11 -0400
committerAndrew Cady <d@jerkface.net>2020-08-21 21:42:04 -0400
commit7c03915273d53bee5f25a8816181c4e82b6f7f23 (patch)
treeb99147cd0e01e3852ca71953bdc414256496ad48
parent1c0e0737b49b8f58fd5bd9d1797e842ff973de6e (diff)
factorize usleep()-related code in main.c
-rw-r--r--log.c6
-rw-r--r--log.h2
-rw-r--r--main.c63
-rw-r--r--main.h7
4 files changed, 51 insertions, 27 deletions
diff --git a/log.c b/log.c
index 3124434..36472db 100644
--- a/log.c
+++ b/log.c
@@ -41,6 +41,12 @@ char *log_printable_level(int level)
41 return "DEBUG"; 41 return "DEBUG";
42 case L_DEBUG2: 42 case L_DEBUG2:
43 return "DEBUG2"; 43 return "DEBUG2";
44 case L_DEBUG3:
45 return "DEBUG3";
46 case L_DEBUG4:
47 return "DEBUG4";
48 case L_DEBUG5:
49 return "DEBUG5";
44 } 50 }
45 return "UNKNOWN"; 51 return "UNKNOWN";
46} 52}
diff --git a/log.h b/log.h
index ee25a14..a05af64 100644
--- a/log.h
+++ b/log.h
@@ -7,6 +7,8 @@
7#define L_DEBUG 7 7#define L_DEBUG 7
8#define L_DEBUG2 8 8#define L_DEBUG2 8
9#define L_DEBUG3 9 9#define L_DEBUG3 9
10#define L_DEBUG4 10
11#define L_DEBUG5 11
10 12
11#define L_UNSET 0x29a 13#define L_UNSET 0x29a
12 14
diff --git a/main.c b/main.c
index e90518e..84561b8 100644
--- a/main.c
+++ b/main.c
@@ -973,44 +973,58 @@ void cleanup()
973 log_close(); 973 log_close();
974} 974}
975 975
976struct tox_timer init_tox_timer(Tox *tox)
977{
978 struct tox_timer t;
979 t.tox_iteration_interval_ms = tox_iteration_interval(tox);
980 log_printf(L_DEBUG5, "Iteration interval: %dms\n", t.tox_iteration_interval_ms);
981
982 gettimeofday(&t.tv_start, NULL);
983
984 t.tv.tv_usec = (t.tox_iteration_interval_ms % 1000) * 1000;
985 t.tv.tv_sec = t.tox_iteration_interval_ms / 1000;
986 return t;
987}
988
989void run_tox_timer(Tox *tox, struct tox_timer t)
990{
991 struct timeval tv_end;
992 gettimeofday(&tv_end, NULL);
993 unsigned long long ms_elapsed = tv_end.tv_sec + tv_end.tv_usec/1000 - t.tv_start.tv_sec - t.tv_start.tv_usec/1000;
994
995 if(ms_elapsed < t.tox_iteration_interval_ms)
996 {
997 log_printf(L_DEBUG5, "Sleeping for %d ms extra to prevent high CPU usage\n", t.tox_iteration_interval_ms - ms_elapsed);
998 usleep((t.tox_iteration_interval_ms - ms_elapsed) * 1000);
999 }
1000}
976 1001
977int do_server_loop() 1002int do_server_loop()
978{ 1003{
979 struct timeval tv, tv_start, tv_end;
980 unsigned long long ms_start, ms_end;
981 fd_set fds; 1004 fd_set fds;
982 unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE]; 1005 unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE];
983 tunnel *tun = NULL; 1006 tunnel *tun = NULL;
984 tunnel *tmp = NULL; 1007 tunnel *tmp = NULL;
985 int sent_data = 0;
986 1008
987 tox_callback_friend_lossless_packet(tox, parse_lossless_packet); 1009 tox_callback_friend_lossless_packet(tox, parse_lossless_packet);
988 1010
989 tv.tv_sec = 0;
990 tv.tv_usec = 20000;
991
992 FD_ZERO(&master_server_fds); 1011 FD_ZERO(&master_server_fds);
993 1012
994 while(1) 1013 while(1)
995 { 1014 {
996 uint32_t tox_do_interval_ms; 1015 bool tox_timer_do_sleep = false;
997 int select_rv = 0; 1016 int select_rv = 0;
998 sent_data = 0;
999 1017
1000 /* Let tox do its stuff */ 1018 /* Let tox do its stuff */
1001 tox_iterate(tox, NULL); 1019 tox_iterate(tox, NULL);
1002 1020
1003 /* Get the desired sleep time, used in select() later */ 1021 /* Get the desired sleep time, used in select() later */
1004 tox_do_interval_ms = tox_iteration_interval(tox); 1022 struct tox_timer t = init_tox_timer(tox);
1005 tv.tv_usec = (tox_do_interval_ms % 1000) * 1000;
1006 tv.tv_sec = tox_do_interval_ms / 1000;
1007 log_printf(L_DEBUG3, "Iteration interval: %dms\n", tox_do_interval_ms);
1008 gettimeofday(&tv_start, NULL);
1009 1023
1010 fds = master_server_fds; 1024 fds = master_server_fds;
1011 1025
1012 /* Poll for data from our client connection */ 1026 /* Poll for data from our client connection */
1013 select_rv = select(select_nfds, &fds, NULL, NULL, &tv); 1027 select_rv = select(select_nfds, &fds, NULL, NULL, &t.tv);
1014 if(select_rv == -1) 1028 if(select_rv == -1)
1015 { 1029 {
1016 log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n", 1030 log_printf(L_DEBUG, "Reading from local socket failed: code=%d (%s)\n",
@@ -1018,7 +1032,7 @@ int do_server_loop()
1018 } 1032 }
1019 else if (select_rv == 0) 1033 else if (select_rv == 0)
1020 { 1034 {
1021 log_printf(L_DEBUG3, "Nothing to read..."); 1035 log_printf(L_DEBUG5, "Nothing to read...");
1022 } 1036 }
1023 else 1037 else
1024 { 1038 {
@@ -1032,6 +1046,7 @@ int do_server_loop()
1032 log_printf(L_DEBUG2, "Current tunnel: %p", tun); 1046 log_printf(L_DEBUG2, "Current tunnel: %p", tun);
1033 if(FD_ISSET(tun->sockfd, &fds)) 1047 if(FD_ISSET(tun->sockfd, &fds))
1034 { 1048 {
1049 tox_timer_do_sleep = true;
1035 int nbytes = recv(tun->sockfd, 1050 int nbytes = recv(tun->sockfd,
1036 tox_packet_buf+PROTOCOL_BUFFER_OFFSET, 1051 tox_packet_buf+PROTOCOL_BUFFER_OFFSET,
1037 READ_BUFFER_SIZE, 0); 1052 READ_BUFFER_SIZE, 0);
@@ -1059,8 +1074,6 @@ int do_server_loop()
1059 frame->connid = tun->connid; 1074 frame->connid = tun->connid;
1060 frame->data_length = 0; 1075 frame->data_length = 0;
1061 send_frame(frame, data); 1076 send_frame(frame, data);
1062 sent_data = 1;
1063
1064 tunnel_queue_delete(tun); 1077 tunnel_queue_delete(tun);
1065 1078
1066 continue; 1079 continue;
@@ -1090,14 +1103,9 @@ int do_server_loop()
1090 } 1103 }
1091 } 1104 }
1092 1105
1093 gettimeofday(&tv_end, NULL); 1106 if(tox_timer_do_sleep)
1094 ms_start = 1000 * tv_start.tv_sec + tv_start.tv_usec/1000;
1095 ms_end = 1000 * tv_end.tv_sec + tv_end.tv_usec/1000;
1096
1097 if(!sent_data && (ms_end - ms_start < tox_do_interval_ms))
1098 { 1107 {
1099 /*log_printf(L_DEBUG, "Sleeping for %d ms extra to prevent high CPU usage\n", (tox_do_interval_ms - (ms_end - ms_start)));*/ 1108 run_tox_timer(tox, t);
1100 usleep((tox_do_interval_ms - (ms_end - ms_start)) * 1000);
1101 } 1109 }
1102 } 1110 }
1103} 1111}
@@ -1361,13 +1369,14 @@ int main(int argc, char *argv[])
1361 min_log_level = L_DEBUG2; 1369 min_log_level = L_DEBUG2;
1362 break; 1370 break;
1363 case 3: 1371 case 3:
1364 min_log_level = L_DEBUG2; 1372 min_log_level = L_DEBUG3;
1365 log_tox_trace = 1; 1373 log_tox_trace = 1;
1366 break; 1374 break;
1367 case 4: 1375 case 4:
1376 min_log_level = L_DEBUG4;
1377 case 5:
1368 default: 1378 default:
1369 min_log_level = L_DEBUG3; 1379 min_log_level = L_DEBUG5;
1370 log_tox_trace = 1;
1371 } 1380 }
1372 break; 1381 break;
1373 case 'q': 1382 case 'q':
diff --git a/main.h b/main.h
index 602f8e4..8411cc6 100644
--- a/main.h
+++ b/main.h
@@ -94,6 +94,13 @@ enum Mode {
94 Mode_Client_Ping 94 Mode_Client_Ping
95}; 95};
96 96
97struct tox_timer {
98 uint32_t tox_iteration_interval_ms;
99 struct timeval tv, tv_start;
100};
101struct tox_timer init_tox_timer(Tox *tox);
102void run_tox_timer(Tox *tox, struct tox_timer t);
103
97/**** GLOBAL VARIABLES ****/ 104/**** GLOBAL VARIABLES ****/
98extern Tox *tox; 105extern Tox *tox;
99 106