Minor refactoring
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 2m1s

Accept connections until there are no more connections to accept in a
single iteration.
This commit is contained in:
Stephen Seo 2024-11-06 14:47:47 +09:00
parent 9ff991d907
commit a27930de85

View file

@ -560,39 +560,45 @@ int main(int argc, char **argv) {
} }
socket_len = sizeof(struct sockaddr_in6); socket_len = sizeof(struct sockaddr_in6);
ret = accept(tcp_socket, (struct sockaddr *)&peer_info, &socket_len); while (1) {
if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { ret = accept(tcp_socket, (struct sockaddr *)&peer_info, &socket_len);
// No connecting peers, do nothing. if (ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
} else if (ret == -1) { // No connecting peers, do nothing.
printf("WARNING: accept: errno %d\n", errno); break;
} else if (ret >= 0) { } else if (ret == -1) {
// Received connection, handle it. printf("WARNING: accept: errno %d\n", errno);
if ((args.flags & 1) == 0) { break;
printf("Peer connected: addr is "); } else if (ret >= 0) {
c_simple_http_print_ipv6_addr(stdout, &peer_info.sin6_addr); // Received connection, handle it.
printf(", fd is %d\n", ret); if ((args.flags & 1) == 0) {
printf("Peer connected: addr is ");
c_simple_http_print_ipv6_addr(stdout, &peer_info.sin6_addr);
printf(", fd is %d\n", ret);
} else {
printf("Peer connected.\n");
}
int connection_fd = ret;
// Set non-blocking.
ret = fcntl(connection_fd, F_SETFL, O_NONBLOCK);
if (ret < 0) {
fprintf(
stderr, "ERROR Failed to set non-blocking on connection fd!\n");
close(connection_fd);
continue;
}
ConnectionItem *citem = malloc(sizeof(ConnectionItem));
citem->fd = connection_fd;
ret = clock_gettime(CLOCK_MONOTONIC, &citem->time_point);
citem->peer_addr = peer_info.sin6_addr;
simple_archiver_list_add(connections,
citem,
c_simple_http_cleanup_connection_item);
} else { } else {
printf("Peer connected.\n"); printf("WARNING: accept: Unknown invalid state!\n");
break;
} }
int connection_fd = ret;
// Set non-blocking.
ret = fcntl(connection_fd, F_SETFL, O_NONBLOCK);
if (ret < 0) {
fprintf(stderr, "ERROR Failed to set non-blocking on connection fd!\n");
close(connection_fd);
continue;
}
ConnectionItem *citem = malloc(sizeof(ConnectionItem));
citem->fd = connection_fd;
ret = clock_gettime(CLOCK_MONOTONIC, &citem->time_point);
citem->peer_addr = peer_info.sin6_addr;
simple_archiver_list_add(connections,
citem,
c_simple_http_cleanup_connection_item);
} else {
printf("WARNING: accept: Unknown invalid state!\n");
} }
clock_gettime(CLOCK_MONOTONIC, &connection_context.current_time); clock_gettime(CLOCK_MONOTONIC, &connection_context.current_time);