Loading src/posix/tcp.cpp +28 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "socket.h" #include "exception.h" #include "error.h" #include "eventapi.h" #include <cstdio> #include <cstring> Loading Loading @@ -372,6 +373,33 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) throw exception; } void tcp::connectTimeout(const std::string& addr, int port, int timeout_ms) { try { connect(addr, port, true); return; // connected immediately } catch (NetException &e) { if (e.getErrorType() != NetException::Note) throw; } netplus::socketwait sw; if (!sw.waitWrite(*this, timeout_ms)) { NetException ne; ne[NetException::Error] << "tcp::connectTimeout: timed out connecting to " << addr << ":" << port; throw ne; } int err = 0; socklen_t errlen = sizeof(err); if (::getsockopt(fd(), SOL_SOCKET, SO_ERROR, &err, &errlen) != 0 || err != 0) { NetException ne; ne[NetException::Error] << "tcp::connectTimeout: could not connect to " << addr << ":" << port << " (" << strerror(err) << ")"; throw ne; } } void tcp::getAddress(std::string& addr) { char buf[INET6_ADDRSTRLEN]{}; Loading src/socket.h +7 −0 Original line number Diff line number Diff line Loading @@ -297,6 +297,13 @@ namespace netplus { void connect(const std::string& addr, int port, bool nonblock = false) override; // Non-blocking connect bounded by timeout_ms, instead of the OS // default (which can be minutes) or blocking forever -- for callers // that need a configurable ceiling on how long an unreachable host // (dead service, wrong address, dropped SYNs) can stall them. // Throws NetException on failure or timeout; returns once connected. void connectTimeout(const std::string& addr, int port, int timeout_ms); void getAddress(std::string& addr) override; private: Loading Loading
src/posix/tcp.cpp +28 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "socket.h" #include "exception.h" #include "error.h" #include "eventapi.h" #include <cstdio> #include <cstring> Loading Loading @@ -372,6 +373,33 @@ void netplus::tcp::connect(const std::string& addr, int port, bool nonblock) throw exception; } void tcp::connectTimeout(const std::string& addr, int port, int timeout_ms) { try { connect(addr, port, true); return; // connected immediately } catch (NetException &e) { if (e.getErrorType() != NetException::Note) throw; } netplus::socketwait sw; if (!sw.waitWrite(*this, timeout_ms)) { NetException ne; ne[NetException::Error] << "tcp::connectTimeout: timed out connecting to " << addr << ":" << port; throw ne; } int err = 0; socklen_t errlen = sizeof(err); if (::getsockopt(fd(), SOL_SOCKET, SO_ERROR, &err, &errlen) != 0 || err != 0) { NetException ne; ne[NetException::Error] << "tcp::connectTimeout: could not connect to " << addr << ":" << port << " (" << strerror(err) << ")"; throw ne; } } void tcp::getAddress(std::string& addr) { char buf[INET6_ADDRSTRLEN]{}; Loading
src/socket.h +7 −0 Original line number Diff line number Diff line Loading @@ -297,6 +297,13 @@ namespace netplus { void connect(const std::string& addr, int port, bool nonblock = false) override; // Non-blocking connect bounded by timeout_ms, instead of the OS // default (which can be minutes) or blocking forever -- for callers // that need a configurable ceiling on how long an unreachable host // (dead service, wrong address, dropped SYNs) can stall them. // Throws NetException on failure or timeout; returns once connected. void connectTimeout(const std::string& addr, int port, int timeout_ms); void getAddress(std::string& addr) override; private: Loading