From 3750781bce2d5cce6446f27c502f8939693caf58 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Wed, 26 Jun 2019 16:25:07 -0700 Subject: [PATCH] simplify IXDNSLookup --- ixwebsocket/IXDNSLookup.cpp | 23 +++++++---------------- ixwebsocket/IXDNSLookup.h | 6 +----- ixwebsocket/IXSocket.cpp | 6 +++--- 3 files changed, 11 insertions(+), 24 deletions(-) diff --git a/ixwebsocket/IXDNSLookup.cpp b/ixwebsocket/IXDNSLookup.cpp index f7e767c1..bbb4991f 100644 --- a/ixwebsocket/IXDNSLookup.cpp +++ b/ixwebsocket/IXDNSLookup.cpp @@ -15,12 +15,13 @@ namespace ix const int64_t DNSLookup::kDefaultWait = 10; // ms DNSLookup::DNSLookup(const std::string& hostname, int port, int64_t wait) : + _hostname(hostname), _port(port), _wait(wait), _res(nullptr), _done(false) { - setHostname(hostname); + ; } struct addrinfo* DNSLookup::getAddrInfo(const std::string& hostname, @@ -66,7 +67,7 @@ namespace ix return nullptr; } - return getAddrInfo(getHostname(), _port, errMsg); + return getAddrInfo(_hostname, _port, errMsg); } struct addrinfo* DNSLookup::resolveAsync(std::string& errMsg, @@ -89,7 +90,9 @@ namespace ix auto ptr = shared_from_this(); std::weak_ptr self(ptr); - _thread = std::thread(&DNSLookup::run, this, self, getHostname(), _port); + int port = _port; + std::string hostname(_hostname); + _thread = std::thread(&DNSLookup::run, this, self, hostname, port); _thread.detach(); std::unique_lock lock(_conditionVariableMutex); @@ -123,7 +126,7 @@ namespace ix return getRes(); } - void DNSLookup::run(std::weak_ptr self, const std::string& hostname, int port) // thread runner + void DNSLookup::run(std::weak_ptr self, std::string hostname, int port) // thread runner { // We don't want to read or write into members variables of an object that could be // gone, so we use temporary variables (res) or we pass in by copy everything that @@ -142,18 +145,6 @@ namespace ix } } - void DNSLookup::setHostname(const std::string& hostname) - { - std::lock_guard lock(_hostnameMutex); - _hostname = hostname; - } - - const std::string& DNSLookup::getHostname() - { - std::lock_guard lock(_hostnameMutex); - return _hostname; - } - void DNSLookup::setErrMsg(const std::string& errMsg) { std::lock_guard lock(_errMsgMutex); diff --git a/ixwebsocket/IXDNSLookup.h b/ixwebsocket/IXDNSLookup.h index 4268a992..60becaca 100644 --- a/ixwebsocket/IXDNSLookup.h +++ b/ixwebsocket/IXDNSLookup.h @@ -42,10 +42,7 @@ namespace ix int port, std::string& errMsg); - void run(std::weak_ptr self, const std::string& hostname, int port); // thread runner - - void setHostname(const std::string& hostname); - const std::string& getHostname(); + void run(std::weak_ptr self, std::string hostname, int port); // thread runner void setErrMsg(const std::string& errMsg); const std::string& getErrMsg(); @@ -54,7 +51,6 @@ namespace ix struct addrinfo* getRes(); std::string _hostname; - std::mutex _hostnameMutex; int _port; int64_t _wait; diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index d7de278c..b4982b41 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -253,14 +253,14 @@ namespace ix bool Socket::writeBytes(const std::string& str, const CancellationRequest& isCancellationRequested) { - char* buffer = const_cast(str.c_str()); + int offset = 0; int len = (int) str.size(); while (true) { if (isCancellationRequested && isCancellationRequested()) return false; - ssize_t ret = send(buffer, len); + ssize_t ret = send((char*)&str[offset], len); // We wrote some bytes, as needed, all good. if (ret > 0) @@ -271,7 +271,7 @@ namespace ix } else { - buffer += ret; + offset += ret; len -= ret; continue; }