* Initializes the socket * @return result of the creation operation. */
| 57 | * @return result of the creation operation. |
| 58 | */ |
| 59 | void ServerSocket::registerCallback(std::function<bool()> accept_function, std::function<void(io::BaseStream *)> handler) { |
| 60 | auto fx = [this](std::function<bool()> /*accept_function*/, std::function<void(io::BaseStream *stream)> handler) { |
| 61 | while (running_) { |
| 62 | int fd = select_descriptor(1000); |
| 63 | if (fd >= 0) { |
| 64 | io::DescriptorStream stream(fd); |
| 65 | handler(&stream); |
| 66 | close_fd(fd); |
| 67 | } |
| 68 | } |
| 69 | }; |
| 70 | server_read_thread_ = std::thread(fx, std::move(accept_function), std::move(handler)); |
| 71 | } |
| 72 | |
| 73 | void ServerSocket::close_fd(int fd) { |
| 74 | std::lock_guard<std::recursive_mutex> guard(selection_mutex_); |