diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2023-11-24 13:53:36 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2023-11-24 16:08:31 +0300 |
commit | b3c1e629e62ae48761728546187ffebf5c643d8b (patch) | |
tree | 3561388ce7f36b44d1d08e1023efb1563780cef1 | |
parent | 1070d24aa6a3a569e77618ddbddddca90e0b6d0e (diff) | |
download | libmicrohttpd-b3c1e629e62ae48761728546187ffebf5c643d8b.tar.gz libmicrohttpd-b3c1e629e62ae48761728546187ffebf5c643d8b.zip |
test_upgrade: merged similar code paths
-rw-r--r-- | src/microhttpd/test_upgrade.c | 101 |
1 files changed, 37 insertions, 64 deletions
diff --git a/src/microhttpd/test_upgrade.c b/src/microhttpd/test_upgrade.c index d48c2977..ddc47653 100644 --- a/src/microhttpd/test_upgrade.c +++ b/src/microhttpd/test_upgrade.c | |||
@@ -1284,7 +1284,9 @@ ahc_upgrade (void *cls, | |||
1284 | 1284 | ||
1285 | 1285 | ||
1286 | /** | 1286 | /** |
1287 | * Run the MHD external event loop using select. | 1287 | * Run the MHD external event loop using select or epoll. |
1288 | * | ||
1289 | * select/epoll modes are used automatically based on daemon's flags. | ||
1288 | * | 1290 | * |
1289 | * @param daemon daemon to run it for | 1291 | * @param daemon daemon to run it for |
1290 | */ | 1292 | */ |
@@ -1292,8 +1294,30 @@ static void | |||
1292 | run_mhd_select_loop (struct MHD_Daemon *daemon) | 1294 | run_mhd_select_loop (struct MHD_Daemon *daemon) |
1293 | { | 1295 | { |
1294 | const time_t start_time = time (NULL); | 1296 | const time_t start_time = time (NULL); |
1297 | const union MHD_DaemonInfo *pdinfo; | ||
1295 | bool connection_was_accepted; | 1298 | bool connection_was_accepted; |
1296 | bool connection_has_finished; | 1299 | bool connection_has_finished; |
1300 | bool use_epoll = false; | ||
1301 | #ifdef EPOLL_SUPPORT | ||
1302 | int ep = -1; | ||
1303 | |||
1304 | pdinfo = MHD_get_daemeon_info (daemon, | ||
1305 | MHD_DAEMON_INFO_FLAGS); | ||
1306 | if (NULL == pdinfo) | ||
1307 | mhdErrorExitDesc ("MHD_get_daemon_info() failed"); | ||
1308 | else | ||
1309 | use_epoll = (0 != (pdinfo->flags & MHD_USE_EPOLL)); | ||
1310 | if (use_epoll) | ||
1311 | { | ||
1312 | pdinfo = MHD_get_daemeon_info (daemon, | ||
1313 | MHD_DAEMON_INFO_EPOLL_FD); | ||
1314 | if (NULL == pdinfo) | ||
1315 | mhdErrorExitDesc ("MHD_get_daemon_info() failed"); | ||
1316 | ep = pdinfo->listen_fd; | ||
1317 | if (0 > ep) | ||
1318 | mhdErrorExitDesc ("Invalid epoll FD value"); | ||
1319 | } | ||
1320 | #endif /* EPOLL_SUPPORT */ | ||
1297 | 1321 | ||
1298 | connection_was_accepted = false; | 1322 | connection_was_accepted = false; |
1299 | connection_has_finished = false; | 1323 | connection_has_finished = false; |
@@ -1306,7 +1330,6 @@ run_mhd_select_loop (struct MHD_Daemon *daemon) | |||
1306 | struct timeval tv; | 1330 | struct timeval tv; |
1307 | uint64_t to64; | 1331 | uint64_t to64; |
1308 | bool has_mhd_timeout; | 1332 | bool has_mhd_timeout; |
1309 | const union MHD_DaemonInfo *pdinfo; | ||
1310 | 1333 | ||
1311 | FD_ZERO (&rs); | 1334 | FD_ZERO (&rs); |
1312 | FD_ZERO (&ws); | 1335 | FD_ZERO (&ws); |
@@ -1338,6 +1361,17 @@ run_mhd_select_loop (struct MHD_Daemon *daemon) | |||
1338 | &es, | 1361 | &es, |
1339 | &max_fd)) | 1362 | &max_fd)) |
1340 | mhdErrorExitDesc ("MHD_get_fdset() failed"); | 1363 | mhdErrorExitDesc ("MHD_get_fdset() failed"); |
1364 | |||
1365 | #ifdef EPOLL_SUPPORT | ||
1366 | if (use_epoll) | ||
1367 | { | ||
1368 | if (ep != max_fd) | ||
1369 | mhdErrorExitDesc ("Wrong 'max_fd' value"); | ||
1370 | if (! FD_ISSET (&rs, ep)) | ||
1371 | mhdErrorExitDesc ("Epoll FD is NOT set in read fd_set"); | ||
1372 | } | ||
1373 | #endif /* EPOLL_SUPPORT */ | ||
1374 | |||
1341 | has_mhd_timeout = (MHD_NO != MHD_get_timeout64 (daemon, | 1375 | has_mhd_timeout = (MHD_NO != MHD_get_timeout64 (daemon, |
1342 | &to64)); | 1376 | &to64)); |
1343 | if (has_mhd_timeout) | 1377 | if (has_mhd_timeout) |
@@ -1411,67 +1445,6 @@ run_mhd_poll_loop (struct MHD_Daemon *daemon) | |||
1411 | #endif /* HAVE_POLL */ | 1445 | #endif /* HAVE_POLL */ |
1412 | 1446 | ||
1413 | 1447 | ||
1414 | #ifdef EPOLL_SUPPORT | ||
1415 | /** | ||
1416 | * Run the MHD external event loop using select. | ||
1417 | * | ||
1418 | * @param daemon daemon to run it for | ||
1419 | */ | ||
1420 | static void | ||
1421 | run_mhd_epoll_loop (struct MHD_Daemon *daemon) | ||
1422 | { | ||
1423 | const union MHD_DaemonInfo *di; | ||
1424 | MHD_socket ep; | ||
1425 | fd_set rs; | ||
1426 | uint64_t to64; | ||
1427 | struct timeval tv; | ||
1428 | int ret; | ||
1429 | |||
1430 | di = MHD_get_daemon_info (daemon, | ||
1431 | MHD_DAEMON_INFO_EPOLL_FD); | ||
1432 | if (NULL == di) | ||
1433 | mhdErrorExitDesc ("MHD_get_daemon_info() failed"); | ||
1434 | ep = di->listen_fd; | ||
1435 | while (! client_done) | ||
1436 | { | ||
1437 | FD_ZERO (&rs); | ||
1438 | to64 = 1000; | ||
1439 | |||
1440 | FD_SET (ep, &rs); | ||
1441 | (void) MHD_get_timeout64 (daemon, | ||
1442 | &to64); | ||
1443 | if (1000 < to64) | ||
1444 | to64 = 1000; | ||
1445 | #if ! defined(_WIN32) || defined(__CYGWIN__) | ||
1446 | tv.tv_sec = (time_t) (to64 / 1000); | ||
1447 | #else /* Native W32 */ | ||
1448 | tv.tv_sec = (long) (to64 / 1000); | ||
1449 | #endif /* Native W32 */ | ||
1450 | tv.tv_usec = (int) (1000 * (to64 % 1000)); | ||
1451 | ret = select (ep + 1, | ||
1452 | &rs, | ||
1453 | NULL, | ||
1454 | NULL, | ||
1455 | &tv); | ||
1456 | if (0 > ret) | ||
1457 | { | ||
1458 | #ifdef MHD_POSIX_SOCKETS | ||
1459 | if (EINTR != errno) | ||
1460 | externalErrorExitDesc ("Unexpected select() error"); | ||
1461 | #else | ||
1462 | if ((WSAEINVAL != WSAGetLastError ()) || | ||
1463 | (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) | ||
1464 | externalErrorExitDesc ("Unexpected select() error"); | ||
1465 | Sleep ((DWORD) (tv.tv_sec * 1000 + tv.tv_usec / 1000)); | ||
1466 | #endif | ||
1467 | } | ||
1468 | MHD_run (daemon); | ||
1469 | } | ||
1470 | } | ||
1471 | |||
1472 | |||
1473 | #endif /* EPOLL_SUPPORT */ | ||
1474 | |||
1475 | /** | 1448 | /** |
1476 | * Run the MHD external event loop using select. | 1449 | * Run the MHD external event loop using select. |
1477 | * | 1450 | * |
@@ -1489,7 +1462,7 @@ run_mhd_loop (struct MHD_Daemon *daemon, | |||
1489 | #endif /* HAVE_POLL */ | 1462 | #endif /* HAVE_POLL */ |
1490 | #ifdef EPOLL_SUPPORT | 1463 | #ifdef EPOLL_SUPPORT |
1491 | else if (0 != (flags & MHD_USE_EPOLL)) | 1464 | else if (0 != (flags & MHD_USE_EPOLL)) |
1492 | run_mhd_epoll_loop (daemon); | 1465 | run_mhd_select_loop (daemon); |
1493 | #endif | 1466 | #endif |
1494 | else | 1467 | else |
1495 | externalErrorExitDesc ("Wrong 'flags' value"); | 1468 | externalErrorExitDesc ("Wrong 'flags' value"); |