Commit ef84774203fd3434bfb83859ad78a27fd969cfba

Authored by Nodeduino
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 52  
53 53 // Resolve the server address and port
54 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 58 throw(std::runtime_error("winHttpHandler: getaddrinfo failed"));
60 59 }
61 60  
... ... @@ -74,8 +73,7 @@ std::string winHttpHandler::send(const std::string &amp; msg, const std::string &amp; ad
74 73 }
75 74  
76 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 78 closesocket(connect_socket);
81 79 connect_socket = INVALID_SOCKET;
... ... @@ -95,8 +93,7 @@ std::string winHttpHandler::send(const std::string &amp; msg, const std::string &amp; ad
95 93 }
96 94  
97 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 98 std::cerr << "winHttpHandler: send failed: " << WSAGetLastError() << std::endl;
102 99 throw(std::runtime_error("winHttpHandler: send failed"));
... ... @@ -104,8 +101,7 @@ std::string winHttpHandler::send(const std::string &amp; msg, const std::string &amp; ad
104 101  
105 102 // shutdown the connection for sending since no more data will be sent
106 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 106 closesocket(connect_socket);
111 107 std::cerr << "winHttpHandler: shutdown failed: " << WSAGetLastError() << std::endl;
... ... @@ -117,6 +113,7 @@ std::string winHttpHandler::send(const std::string &amp; msg, const std::string &amp; ad
117 113  
118 114 // Receive data until the server closes the connection
119 115 std::string response;
  116 + int res;
120 117 do
121 118 {
122 119 res = recv(connect_socket, recvbuf, recvbuflen, 0);
... ... @@ -148,10 +145,9 @@ std::vector&lt;std::string&gt; winHttpHandler::sendMulticast(const std::string &amp; msg,
148 145  
149 146 // Resolve the server address and port
150 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 151 throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed"));
156 152 }
157 153  
... ... @@ -213,8 +209,7 @@ std::vector&lt;std::string&gt; winHttpHandler::sendMulticast(const std::string &amp; msg,
213 209  
214 210 // shutdown the connection for sending since no more data will be sent
215 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 214 closesocket(connect_socket);
220 215 std::cerr << "winHttpHandler: sendMulticast: shutdown failed: " << WSAGetLastError() << std::endl;
... ... @@ -224,6 +219,7 @@ std::vector&lt;std::string&gt; winHttpHandler::sendMulticast(const std::string &amp; msg,
224 219 std::string response;
225 220 const int recvbuflen = 2048;
226 221 char recvbuf[recvbuflen] = {};
  222 + int res;
227 223 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
228 224 while (std::chrono::steady_clock::now() - start < std::chrono::seconds(timeout))
229 225 {
... ...