This commit is contained in:
J. Nick Koston
2025-05-27 10:35:37 -05:00
parent 456a475cea
commit 600b2e6d78
3 changed files with 34 additions and 23 deletions

View File

@@ -55,17 +55,21 @@ class BSDSocketImpl : public Socket {
}
int connect(const struct sockaddr *addr, socklen_t addrlen) override { return ::connect(fd_, addr, addrlen); }
std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
int fd = ::accept(fd_, addr, addrlen);
if (fd == -1)
return {};
return make_unique<BSDSocketImpl>(fd);
return accept_impl(addr, addrlen, false);
}
std::unique_ptr<Socket> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) override {
return accept_impl(addr, addrlen, true);
}
private:
std::unique_ptr<Socket> accept_impl(struct sockaddr *addr, socklen_t *addrlen, bool loop_monitored) {
int fd = ::accept(fd_, addr, addrlen);
if (fd == -1)
return {};
return make_unique<BSDSocketImpl>(fd, true); // Monitored for incoming data
return make_unique<BSDSocketImpl>(fd, loop_monitored);
}
public:
int bind(const struct sockaddr *addr, socklen_t addrlen) override { return ::bind(fd_, addr, addrlen); }
int close() override {
if (!closed_) {
@@ -153,18 +157,20 @@ class BSDSocketImpl : public Socket {
bool closed_ = false;
};
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
// Helper to create a socket with optional monitoring
static std::unique_ptr<Socket> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
int ret = ::socket(domain, type, protocol);
if (ret == -1)
return nullptr;
return std::unique_ptr<Socket>{new BSDSocketImpl(ret)};
return std::unique_ptr<Socket>{new BSDSocketImpl(ret, loop_monitored)};
}
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
return create_socket(domain, type, protocol, false);
}
std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
int ret = ::socket(domain, type, protocol);
if (ret == -1)
return nullptr;
return std::unique_ptr<Socket>{new BSDSocketImpl(ret, true)};
return create_socket(domain, type, protocol, true);
}
} // namespace socket

View File

@@ -48,17 +48,21 @@ class LwIPSocketImpl : public Socket {
}
int connect(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_connect(fd_, addr, addrlen); }
std::unique_ptr<Socket> accept(struct sockaddr *addr, socklen_t *addrlen) override {
int fd = lwip_accept(fd_, addr, addrlen);
if (fd == -1)
return {};
return make_unique<LwIPSocketImpl>(fd); // Default: not monitored
return accept_impl(addr, addrlen, false);
}
std::unique_ptr<Socket> accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen) override {
return accept_impl(addr, addrlen, true);
}
private:
std::unique_ptr<Socket> accept_impl(struct sockaddr *addr, socklen_t *addrlen, bool loop_monitored) {
int fd = lwip_accept(fd_, addr, addrlen);
if (fd == -1)
return {};
return make_unique<LwIPSocketImpl>(fd, true); // Monitored for incoming data
return make_unique<LwIPSocketImpl>(fd, loop_monitored);
}
public:
int bind(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_bind(fd_, addr, addrlen); }
int close() override {
if (!closed_) {
@@ -125,18 +129,20 @@ class LwIPSocketImpl : public Socket {
bool closed_ = false;
};
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
// Helper to create a socket with optional monitoring
static std::unique_ptr<Socket> create_socket(int domain, int type, int protocol, bool loop_monitored = false) {
int ret = lwip_socket(domain, type, protocol);
if (ret == -1)
return nullptr;
return std::unique_ptr<Socket>{new LwIPSocketImpl(ret)};
return std::unique_ptr<Socket>{new LwIPSocketImpl(ret, loop_monitored)};
}
std::unique_ptr<Socket> socket(int domain, int type, int protocol) {
return create_socket(domain, type, protocol, false);
}
std::unique_ptr<Socket> socket_loop_monitored(int domain, int type, int protocol) {
int ret = lwip_socket(domain, type, protocol);
if (ret == -1)
return nullptr;
return std::unique_ptr<Socket>{new LwIPSocketImpl(ret, true)};
return create_socket(domain, type, protocol, true);
}
} // namespace socket

View File

@@ -573,7 +573,6 @@ class Application {
uint32_t loop_component_start_time_{0};
// Socket select management
// WARNING: These variables are NOT protected by locks and must only be modified from the main loop
std::set<int> socket_fds_; // Set of all monitored socket file descriptors
bool socket_fds_changed_{false}; // Flag to rebuild base_read_fds_ when socket_fds_ changes
int max_fd_{-1}; // Highest file descriptor number for select()