| 348 | /* ── Accept ───────────────────────────────────────────────────── */ |
| 349 | |
| 350 | cbm_http_conn_t *cbm_httpd_accept(cbm_httpd_t *d, int timeout_ms) { |
| 351 | if (!d) |
| 352 | return NULL; |
| 353 | cbm_mutex_lock(&d->active_mutex); |
| 354 | bool interrupted = d->interrupted; |
| 355 | int send_buffer = d->send_buffer_for_test; |
| 356 | cbm_mutex_unlock(&d->active_mutex); |
| 357 | if (interrupted) |
| 358 | return NULL; |
| 359 | if (wait_readable(d->fd, timeout_ms) != 1) |
| 360 | return NULL; |
| 361 | |
| 362 | cbm_sock_t cfd = accept(d->fd, NULL, NULL); |
| 363 | if (cfd == CBM_SOCK_BAD) |
| 364 | return NULL; |
| 365 | |
| 366 | int one = 1; |
| 367 | setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, (const char *)&one, sizeof(one)); |
| 368 | /* An explicit SO_SNDBUF also disables Windows dynamic send buffering, |
| 369 | * which otherwise grows kernel-side queuing past any fixed payload and |
| 370 | * removes the backpressure point the deadline/interrupt tests rely on. */ |
| 371 | if (send_buffer > 0) |
| 372 | setsockopt(cfd, SOL_SOCKET, SO_SNDBUF, (const char *)&send_buffer, sizeof(send_buffer)); |
| 373 | #ifdef SO_NOSIGPIPE |
| 374 | setsockopt(cfd, SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof(one)); |
| 375 | #endif |
| 376 | |
| 377 | if (!socket_set_nonblocking(cfd)) { |
| 378 | cbm_sock_close(cfd); |
| 379 | return NULL; |
| 380 | } |
| 381 | |
| 382 | cbm_http_conn_t *c = calloc(1, sizeof(*c)); |
| 383 | if (!c) { |
| 384 | cbm_sock_close(cfd); |
| 385 | return NULL; |
| 386 | } |
| 387 | c->fd = cfd; |
| 388 | c->owner = d; |
| 389 | c->send_deadline_ms = d->send_deadline_for_test_ms > 0 ? d->send_deadline_for_test_ms |
| 390 | : CBM_HTTP_SEND_DEADLINE_MS; |
| 391 | atomic_init(&c->response_started, false); |
| 392 | |
| 393 | cbm_mutex_lock(&d->active_mutex); |
| 394 | if (d->interrupted || d->active) { |
| 395 | cbm_mutex_unlock(&d->active_mutex); |
| 396 | cbm_sock_close(cfd); |
| 397 | free(c); |
| 398 | return NULL; |
| 399 | } |
| 400 | c->recv_deadline_ms = d->recv_deadline_ms; |
| 401 | d->active = c; |
| 402 | cbm_mutex_unlock(&d->active_mutex); |
| 403 | return c; |
| 404 | } |
| 405 | |
| 406 | void cbm_httpd_conn_close(cbm_http_conn_t *c) { |
| 407 | if (!c) |