This commit is contained in:
J. Nick Koston
2025-05-27 15:20:44 -05:00
parent f04b4b3f40
commit 723e22341f
4 changed files with 17 additions and 1 deletions

View File

@@ -42,6 +42,7 @@ std::string format_sockaddr(const struct sockaddr_storage &storage) {
class BSDSocketImpl : public Socket {
public:
BSDSocketImpl(int fd, bool monitor_loop = false) : fd_(fd) {
#ifdef USE_SOCKET_SELECT_SUPPORT
// Register new socket with the application for select() if monitoring requested
if (monitor_loop && fd_ >= 0) {
// Only set loop_monitored_ to true if registration succeeds
@@ -49,6 +50,10 @@ class BSDSocketImpl : public Socket {
} else {
loop_monitored_ = false;
}
#else
// Without select support, ignore monitor_loop parameter
(void) monitor_loop;
#endif
}
~BSDSocketImpl() override {
if (!closed_) {
@@ -75,10 +80,12 @@ class BSDSocketImpl : public Socket {
int bind(const struct sockaddr *addr, socklen_t addrlen) override { return ::bind(fd_, addr, addrlen); }
int close() override {
if (!closed_) {
#ifdef USE_SOCKET_SELECT_SUPPORT
// Unregister from select() before closing if monitored
if (loop_monitored_) {
App.unregister_socket_fd(fd_);
}
#endif
int ret = ::close(fd_);
closed_ = true;
return ret;

View File

@@ -35,6 +35,7 @@ std::string format_sockaddr(const struct sockaddr_storage &storage) {
class LwIPSocketImpl : public Socket {
public:
LwIPSocketImpl(int fd, bool monitor_loop = false) : fd_(fd) {
#ifdef USE_SOCKET_SELECT_SUPPORT
// Register new socket with the application for select() if monitoring requested
if (monitor_loop && fd_ >= 0) {
// Only set loop_monitored_ to true if registration succeeds
@@ -42,6 +43,10 @@ class LwIPSocketImpl : public Socket {
} else {
loop_monitored_ = false;
}
#else
// Without select support, ignore monitor_loop parameter
(void) monitor_loop;
#endif
}
~LwIPSocketImpl() override {
if (!closed_) {
@@ -68,10 +73,12 @@ class LwIPSocketImpl : public Socket {
int bind(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_bind(fd_, addr, addrlen); }
int close() override {
if (!closed_) {
#ifdef USE_SOCKET_SELECT_SUPPORT
// Unregister from select() before closing if monitored
if (loop_monitored_) {
App.unregister_socket_fd(fd_);
}
#endif
int ret = lwip_close(fd_);
closed_ = true;
return ret;

View File

@@ -12,12 +12,12 @@ namespace socket {
Socket::~Socket() {}
bool Socket::ready() const {
#ifdef USE_SOCKET_SELECT_SUPPORT
if (!loop_monitored_) {
// Non-monitored sockets always return true (assume data may be available)
return true;
}
#ifdef USE_SOCKET_SELECT_SUPPORT
// For loop-monitored sockets, check with the Application's select() results
int fd = this->get_fd();
if (fd < 0) {

View File

@@ -59,7 +59,9 @@ class Socket {
bool ready() const;
protected:
#ifdef USE_SOCKET_SELECT_SUPPORT
bool loop_monitored_{false}; ///< Whether this socket is monitored by the event loop
#endif
};
/// Create a socket of the given domain, type and protocol.