| 667 | } |
| 668 | |
| 669 | void Stop() { |
| 670 | g_running = false; |
| 671 | if (g_listenFd >= 0) { |
| 672 | shutdown(g_listenFd, SHUT_RDWR); |
| 673 | close(g_listenFd); |
| 674 | g_listenFd = -1; |
| 675 | } |
| 676 | if (g_serverThread.joinable()) |
| 677 | g_serverThread.join(); |
| 678 | { |
| 679 | std::lock_guard<std::mutex> lock(g_clientFdMutex); |
| 680 | for (int fd : g_clientFds) { |
| 681 | shutdown(fd, SHUT_RDWR); |
| 682 | } |
| 683 | } |
| 684 | { |
| 685 | std::lock_guard<std::mutex> lock(g_clientMutex); |
| 686 | for (auto& client : g_clientThreads) { |
| 687 | if (client.thread.joinable()) client.thread.join(); |
| 688 | } |
| 689 | g_clientThreads.clear(); |
| 690 | } |
| 691 | g_port = 0; |
| 692 | } |
| 693 | |
| 694 | uint16_t GetPort() { |
| 695 | return g_port; |