Commit c8d0a5919e911ea61c8329b49531bd3bfe4554fb
Committed by
Moritz Wirger
1 parent
3f792d8c
clanup winHttpHandler
- also reduce ttl of multicast to 1 to stay in the local network, but could still be increased to 16
Showing
1 changed file
with
11 additions
and
18 deletions
hueplusplus/winHttpHandler.cpp
| ... | ... | @@ -45,14 +45,13 @@ winHttpHandler::~winHttpHandler() |
| 45 | 45 | |
| 46 | 46 | std::string winHttpHandler::send(const std::string & msg, const std::string & adr, int port) const |
| 47 | 47 | { |
| 48 | - struct addrinfo *result = nullptr, *ptr = nullptr, hints; | |
| 49 | - | |
| 50 | - ZeroMemory(&hints, sizeof(hints)); | |
| 48 | + struct addrinfo hints = {}; | |
| 51 | 49 | hints.ai_family = AF_INET; |
| 52 | 50 | hints.ai_socktype = SOCK_STREAM; |
| 53 | 51 | hints.ai_protocol = IPPROTO_TCP; |
| 54 | 52 | |
| 55 | 53 | // Resolve the server address and port |
| 54 | + struct addrinfo *result = nullptr; | |
| 56 | 55 | int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); |
| 57 | 56 | if (res != 0) |
| 58 | 57 | { |
| ... | ... | @@ -60,15 +59,12 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 60 | 59 | throw(std::runtime_error("winHttpHandler: getaddrinfo failed")); |
| 61 | 60 | } |
| 62 | 61 | |
| 63 | - SOCKET connect_socket = INVALID_SOCKET; | |
| 64 | - | |
| 65 | - | |
| 66 | 62 | // Attempt to connect to the first address returned by |
| 67 | 63 | // the call to getaddrinfo |
| 68 | - ptr = result; | |
| 64 | + struct addrinfo *ptr = result; | |
| 69 | 65 | |
| 70 | 66 | // Create a SOCKET for connecting to server |
| 71 | - connect_socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); | |
| 67 | + SOCKET connect_socket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); | |
| 72 | 68 | |
| 73 | 69 | if (connect_socket == INVALID_SOCKET) |
| 74 | 70 | { |
| ... | ... | @@ -121,7 +117,6 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 121 | 117 | |
| 122 | 118 | // Receive data until the server closes the connection |
| 123 | 119 | std::string response; |
| 124 | - int received = 0; | |
| 125 | 120 | do |
| 126 | 121 | { |
| 127 | 122 | res = recv(connect_socket, recvbuf, recvbuflen, 0); |
| ... | ... | @@ -146,15 +141,13 @@ std::string winHttpHandler::send(const std::string & msg, const std::string & ad |
| 146 | 141 | |
| 147 | 142 | std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, const std::string & adr, int port, int timeout) const |
| 148 | 143 | { |
| 149 | - struct addrinfo *result = nullptr, *ptr = nullptr, hints; | |
| 150 | - SOCKADDR_IN source_sin, dest_sin; | |
| 151 | - | |
| 152 | - ZeroMemory(&hints, sizeof(hints)); | |
| 144 | + struct addrinfo hints = {}; | |
| 153 | 145 | hints.ai_family = AF_INET; |
| 154 | 146 | hints.ai_socktype = SOCK_DGRAM; |
| 155 | 147 | hints.ai_protocol = IPPROTO_TCP; |
| 156 | 148 | |
| 157 | 149 | // Resolve the server address and port |
| 150 | + struct addrinfo *result = nullptr; | |
| 158 | 151 | int res = getaddrinfo(adr.c_str(), std::to_string(port).c_str(), &hints, &result); |
| 159 | 152 | if (res != 0) |
| 160 | 153 | { |
| ... | ... | @@ -162,14 +155,12 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 162 | 155 | throw(std::runtime_error("winHttpHandler: sendMulticast: getaddrinfo failed")); |
| 163 | 156 | } |
| 164 | 157 | |
| 165 | - SOCKET connect_socket = INVALID_SOCKET; | |
| 166 | - | |
| 167 | - | |
| 168 | 158 | // Attempt to connect to the first address returned by |
| 169 | 159 | // the call to getaddrinfo |
| 170 | - ptr = result; | |
| 160 | + struct addrinfo *ptr = result; | |
| 171 | 161 | |
| 172 | 162 | // Create a SOCKET for connecting to server |
| 163 | + SOCKET connect_socket; | |
| 173 | 164 | if ((connect_socket = socket(ptr->ai_family, ptr->ai_socktype, 0)) == INVALID_SOCKET) |
| 174 | 165 | { |
| 175 | 166 | freeaddrinfo(result); |
| ... | ... | @@ -178,6 +169,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 178 | 169 | } |
| 179 | 170 | |
| 180 | 171 | // Fill out source socket's address information. |
| 172 | + SOCKADDR_IN source_sin; | |
| 181 | 173 | source_sin.sin_family = AF_INET; |
| 182 | 174 | source_sin.sin_port = htons(0); |
| 183 | 175 | source_sin.sin_addr.s_addr = htonl(INADDR_ANY); |
| ... | ... | @@ -197,7 +189,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 197 | 189 | setsockopt(connect_socket, SOL_SOCKET, SO_BROADCAST, (char *)&bOptVal, sizeof(bOptVal)); |
| 198 | 190 | |
| 199 | 191 | // Set the Time-to-Live of the multicast. |
| 200 | - int iOptVal = 64; | |
| 192 | + int iOptVal = 1; // for same subnet, but might be increased to 16 | |
| 201 | 193 | if (setsockopt(connect_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char FAR *)&iOptVal, sizeof(int)) == SOCKET_ERROR) |
| 202 | 194 | { |
| 203 | 195 | closesocket(connect_socket); |
| ... | ... | @@ -206,6 +198,7 @@ std::vector<std::string> winHttpHandler::sendMulticast(const std::string & msg, |
| 206 | 198 | } |
| 207 | 199 | |
| 208 | 200 | // Fill out the desination socket's address information. |
| 201 | + SOCKADDR_IN dest_sin; | |
| 209 | 202 | dest_sin.sin_family = AF_INET; |
| 210 | 203 | dest_sin.sin_port = htons(port); |
| 211 | 204 | dest_sin.sin_addr.s_addr = inet_addr((const char*)ptr->ai_addr); | ... | ... |