summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGDR! <gdr@gdr.name>2016-05-05 19:39:42 +0200
committerGDR! <gdr@gdr.name>2016-05-05 19:39:42 +0200
commitcc633e9ad431850d298638dbcdcbf1e8a4dfdeb1 (patch)
treeec13fefdedf0804b92d1157e80e0904579acfbb9
parent5d9e9c5d9078d8d1987375e972282220d50b9328 (diff)
Possible fix for Issue #16
-rw-r--r--main.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/main.c b/main.c
index 374ed7a..24c8df6 100644
--- a/main.c
+++ b/main.c
@@ -279,7 +279,12 @@ int send_frame(protocol_frame *frame, uint8_t *data)
279 /* If this branch is ran, most likely we've hit congestion control. */ 279 /* If this branch is ran, most likely we've hit congestion control. */
280 if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_SENDQ) 280 if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_SENDQ)
281 { 281 {
282 log_printf(L_DEBUG, "[%d] Failed to send packet to friend %d (Packet queue is full)\n", i, frame->friendnumber, custom_packet_error); 282 log_printf(L_DEBUG, "[%d] Failed to send packet to friend %d (Packet queue is full)\n", i, frame->friendnumber);
283 }
284 else if(custom_packet_error == TOX_ERR_FRIEND_CUSTOM_PACKET_FRIEND_NOT_CONNECTED)
285 {
286 log_printf(L_DEBUG, "[%d] Failed to send packet to friend %d (Friend gone)\n", i, frame->friendnumber);
287 break;
283 } 288 }
284 else 289 else
285 { 290 {
@@ -750,7 +755,8 @@ void cleanup(int status, void *tmp)
750 755
751int do_server_loop() 756int do_server_loop()
752{ 757{
753 struct timeval tv; 758 struct timeval tv, tv_start, tv_end;
759 unsigned long long ms_start, ms_end;
754 fd_set fds; 760 fd_set fds;
755 unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE]; 761 unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE];
756 tunnel *tun = NULL; 762 tunnel *tun = NULL;
@@ -776,6 +782,8 @@ int do_server_loop()
776 tox_do_interval_ms = tox_iteration_interval(tox); 782 tox_do_interval_ms = tox_iteration_interval(tox);
777 tv.tv_usec = (tox_do_interval_ms % 1000) * 1000; 783 tv.tv_usec = (tox_do_interval_ms % 1000) * 1000;
778 tv.tv_sec = tox_do_interval_ms / 1000; 784 tv.tv_sec = tox_do_interval_ms / 1000;
785 log_printf(L_DEBUG, "Iteration interval: %dms\n", tox_do_interval_ms);
786 gettimeofday(&tv_start, NULL);
779 787
780 /* Check change in connection state */ 788 /* Check change in connection state */
781 tmp_isconnected = connection_status; 789 tmp_isconnected = connection_status;
@@ -805,12 +813,20 @@ int do_server_loop()
805 READ_BUFFER_SIZE, 0); 813 READ_BUFFER_SIZE, 0);
806 814
807 /* Check if connection closed */ 815 /* Check if connection closed */
808 if(nbytes == 0) 816 if(nbytes <= 0)
809 { 817 {
810 char data[PROTOCOL_BUFFER_OFFSET]; 818 char data[PROTOCOL_BUFFER_OFFSET];
811 protocol_frame frame_st, *frame; 819 protocol_frame frame_st, *frame;
812 820
813 log_printf(L_WARNING, "conn closed!\n"); 821 if(nbytes == 0)
822 {
823 log_printf(L_WARNING, "conn closed!\n");
824 }
825 else
826 {
827 log_printf(L_WARNING, "conn closed, code=%d (%s)\n",
828 errno, strerror(errno));
829 }
814 830
815 frame = &frame_st; 831 frame = &frame_st;
816 memset(frame, 0, sizeof(protocol_frame)); 832 memset(frame, 0, sizeof(protocol_frame));
@@ -838,6 +854,16 @@ int do_server_loop()
838 } 854 }
839 } 855 }
840 } 856 }
857
858 gettimeofday(&tv_end, NULL);
859 ms_start = 1000 * tv_start.tv_sec + tv_start.tv_usec/1000;
860 ms_end = 1000 * tv_end.tv_sec + tv_end.tv_usec/1000;
861
862 if(ms_end - ms_start < tox_do_interval_ms)
863 {
864 log_printf(L_DEBUG, "Sleeping for %d ms extra to prevent high CPU usage\n", (tox_do_interval_ms - (ms_end - ms_start)));
865 usleep((tox_do_interval_ms - (ms_end - ms_start)) * 1000);
866 }
841 } 867 }
842} 868}
843 869