| 454 | } |
| 455 | |
| 456 | SRTSOCKET srt::CUDTUnited::generateSocketID(bool for_group) |
| 457 | { |
| 458 | ScopedLock guard(m_IDLock); |
| 459 | |
| 460 | int sockval = m_SocketIDGenerator - 1; |
| 461 | |
| 462 | // First problem: zero-value should be avoided by various reasons. |
| 463 | |
| 464 | if (sockval <= 0) |
| 465 | { |
| 466 | // We have a rollover on the socket value, so |
| 467 | // definitely we haven't made the Columbus mistake yet. |
| 468 | m_SocketIDGenerator = MAX_SOCKET_VAL; |
| 469 | sockval = MAX_SOCKET_VAL; |
| 470 | } |
| 471 | |
| 472 | // Check all sockets if any of them has this value. |
| 473 | // Socket IDs are begin created this way: |
| 474 | // |
| 475 | // Initial random |
| 476 | // | |
| 477 | // | |
| 478 | // | |
| 479 | // | |
| 480 | // ... |
| 481 | // The only problem might be if the number rolls over |
| 482 | // and reaches the same value from the opposite side. |
| 483 | // This is still a valid socket value, but this time |
| 484 | // we have to check, which sockets have been used already. |
| 485 | if (sockval == m_SocketIDGenerator_init) |
| 486 | { |
| 487 | // Mark that since this point on the checks for |
| 488 | // whether the socket ID is in use must be done. |
| 489 | m_SocketIDGenerator_init = 0; |
| 490 | } |
| 491 | |
| 492 | // This is when all socket numbers have been already used once. |
| 493 | // This may happen after many years of running an application |
| 494 | // constantly when the connection breaks and gets restored often. |
| 495 | if (m_SocketIDGenerator_init == 0) |
| 496 | { |
| 497 | int startval = sockval; |
| 498 | for (;;) // Roll until an unused value is found |
| 499 | { |
| 500 | enterCS(m_GlobControlLock); |
| 501 | const bool exists = |
| 502 | #if ENABLE_BONDING |
| 503 | for_group |
| 504 | ? m_Groups.count(sockval | SRTGROUP_MASK) |
| 505 | : |
| 506 | #endif |
| 507 | m_Sockets.count(sockval); |
| 508 | leaveCS(m_GlobControlLock); |
| 509 | |
| 510 | if (exists) |
| 511 | { |
| 512 | // The socket value is in use. |
| 513 | --sockval; |
no test coverage detected