Commit ef84774203fd3434bfb83859ad78a27fd969cfba
Committed by
Moritz Wirger
1 parent
c8d0a591
cleanup winHttpHandler
- reduce variable scope of res, because its value was never really used
Showing
1 changed file
with
10 additions
and
14 deletions
hueplusplus/winHttpHandler.cpp
| @@ -52,10 +52,9 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -52,10 +52,9 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 52 | 52 | ||
| 53 | // Resolve the server address and port | 53 | // Resolve the server address and port |
| 54 | struct addrinfo *result = nullptr; | 54 | struct addrinfo *result = nullptr; |
| 55 | - int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); | ||
| 56 | - if (res != 0) | 55 | + if (getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result) != 0) |
| 57 | { | 56 | { |
| 58 | - std::cerr << "winHttpHandler: getaddrinfo failed: " << res << std::endl; | 57 | + std::cerr << "winHttpHandler: getaddrinfo failed: " << WSAGetLastError() << std::endl; |
| 59 | throw(std::runtime_error("winHttpHandler: getaddrinfo failed")); | 58 | throw(std::runtime_error("winHttpHandler: getaddrinfo failed")); |
| 60 | } | 59 | } |
| 61 | 60 | ||
| @@ -74,8 +73,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -74,8 +73,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 74 | } | 73 | } |
| 75 | 74 | ||
| 76 | // Connect to server. | 75 | // Connect to server. |
| 77 | - res = connect(connect_socket, ptr->ai_addr, (int)ptr->ai_addrlen); | ||
| 78 | - if (res == SOCKET_ERROR) | 76 | + if (connect(connect_socket, ptr->ai_addr, (int)ptr->ai_addrlen) == SOCKET_ERROR) |
| 79 | { | 77 | { |
| 80 | closesocket(connect_socket); | 78 | closesocket(connect_socket); |
| 81 | connect_socket = INVALID_SOCKET; | 79 | connect_socket = INVALID_SOCKET; |
| @@ -95,8 +93,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -95,8 +93,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 95 | } | 93 | } |
| 96 | 94 | ||
| 97 | // Send an initial buffer | 95 | // Send an initial buffer |
| 98 | - res = ::send(connect_socket, msg.c_str(), msg.size(), 0); | ||
| 99 | - if (res == SOCKET_ERROR) | 96 | + if (::send(connect_socket, msg.c_str(), msg.size(), 0) == SOCKET_ERROR) |
| 100 | { | 97 | { |
| 101 | std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; | 98 | std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl; |
| 102 | throw(std::runtime_error("winHttpHandler: send failed")); | 99 | throw(std::runtime_error("winHttpHandler: send failed")); |
| @@ -104,8 +101,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -104,8 +101,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 104 | 101 | ||
| 105 | // shutdown the connection for sending since no more data will be sent | 102 | // shutdown the connection for sending since no more data will be sent |
| 106 | // the client can still use the ConnectSocket for receiving data | 103 | // the client can still use the ConnectSocket for receiving data |
| 107 | - res = shutdown(connect_socket, SD_SEND); | ||
| 108 | - if (res == SOCKET_ERROR) | 104 | + if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR) |
| 109 | { | 105 | { |
| 110 | closesocket(connect_socket); | 106 | closesocket(connect_socket); |
| 111 | std::cerr << "winHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl; | 107 | std::cerr << "winHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl; |
| @@ -117,6 +113,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | @@ -117,6 +113,7 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad | ||
| 117 | 113 | ||
| 118 | // Receive data until the server closes the connection | 114 | // Receive data until the server closes the connection |
| 119 | std::string response; | 115 | std::string response; |
| 116 | + int res; | ||
| 120 | do | 117 | do |
| 121 | { | 118 | { |
| 122 | res = recv(connect_socket, recvbuf, recvbuflen, 0); | 119 | res = recv(connect_socket, recvbuf, recvbuflen, 0); |
| @@ -148,10 +145,9 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | @@ -148,10 +145,9 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | ||
| 148 | 145 | ||
| 149 | // Resolve the server address and port | 146 | // Resolve the server address and port |
| 150 | struct addrinfo *result = nullptr; | 147 | struct addrinfo *result = nullptr; |
| 151 | - int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); | ||
| 152 | - if (res != 0) | 148 | + if (getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result) != 0) |
| 153 | { | 149 | { |
| 154 | - std::cerr << "winHttpHandler: sendMulticast: getaddrinfo failed: " << res << std::endl; | 150 | + std::cerr << "winHttpHandler: sendMulticast: getaddrinfo failed: " << WSAGetLastError() << std::endl; |
| 155 | throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed")); | 151 | throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed")); |
| 156 | } | 152 | } |
| 157 | 153 | ||
| @@ -213,8 +209,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | @@ -213,8 +209,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | ||
| 213 | 209 | ||
| 214 | // shutdown the connection for sending since no more data will be sent | 210 | // shutdown the connection for sending since no more data will be sent |
| 215 | // the client can still use the ConnectSocket for receiving data | 211 | // the client can still use the ConnectSocket for receiving data |
| 216 | - res = shutdown(connect_socket, SD_SEND); | ||
| 217 | - if (res == SOCKET_ERROR) | 212 | + if (shutdown(connect_socket, SD_SEND) == SOCKET_ERROR) |
| 218 | { | 213 | { |
| 219 | closesocket(connect_socket); | 214 | closesocket(connect_socket); |
| 220 | std::cerr << "winHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; | 215 | std::cerr << "winHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl; |
| @@ -224,6 +219,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | @@ -224,6 +219,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, | ||
| 224 | std::string response; | 219 | std::string response; |
| 225 | const int recvbuflen = 2048; | 220 | const int recvbuflen = 2048; |
| 226 | char recvbuf[recvbuflen] = {}; | 221 | char recvbuf[recvbuflen] = {}; |
| 222 | + int res; | ||
| 227 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | 223 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); |
| 228 | while (std::chrono::steady_clock::now() - start < std::chrono::seconds(timeout)) | 224 | while (std::chrono::steady_clock::now() - start < std::chrono::seconds(timeout)) |
| 229 | { | 225 | { |