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