+ if (epoll_ctl(ep_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
+ perror("EPOLL_CTL_ADD");
+ exit(1);
+ }
+ continue;
+ }
+
+ ret = read(sock, buf, 65536);
+ if (ret == 0) {
+ close(sock);
+
+ if (epoll_ctl(ep_fd, EPOLL_CTL_DEL, sock, NULL) == -1) {
+ perror("EPOLL_CTL_ADD");
+ exit(1);
+ }
+
+ continue;
+ }
+
+ bytes_received += ret;
+
+ // update the central counter after every 1MB (8ms
+ // at gigabit speeds, should be enough) of received data
+ if (bytes_received > update_frequency) {
+ pthread_mutex_lock(&receive_mutex);
+ total_bytes_received += bytes_received;
+ pthread_mutex_unlock(&receive_mutex);
+
+ bytes_received = 0;
+ }