| 211 | #endif |
| 212 | |
| 213 | static int readSocket (SocketHandle handle, |
| 214 | void* destBuffer, int maxBytesToRead, |
| 215 | std::atomic<bool>& connected, |
| 216 | bool blockUntilSpecifiedAmountHasArrived, |
| 217 | CriticalSection& readLock, |
| 218 | String* senderIP = nullptr, |
| 219 | int* senderPort = nullptr) noexcept |
| 220 | { |
| 221 | #if ! JUCE_WINDOWS |
| 222 | if (blockUntilSpecifiedAmountHasArrived != getSocketBlockingState (handle)) |
| 223 | #endif |
| 224 | setSocketBlockingState (handle, blockUntilSpecifiedAmountHasArrived); |
| 225 | |
| 226 | int bytesRead = 0; |
| 227 | |
| 228 | while (bytesRead < maxBytesToRead) |
| 229 | { |
| 230 | long bytesThisTime = -1; |
| 231 | auto buffer = static_cast<char*> (destBuffer) + bytesRead; |
| 232 | auto numToRead = (juce_recvsend_size_t) (maxBytesToRead - bytesRead); |
| 233 | |
| 234 | { |
| 235 | // avoid race-condition |
| 236 | CriticalSection::ScopedTryLockType lock (readLock); |
| 237 | |
| 238 | if (lock.isLocked()) |
| 239 | { |
| 240 | if (senderIP == nullptr || senderPort == nullptr) |
| 241 | { |
| 242 | bytesThisTime = ::recv (handle, buffer, numToRead, 0); |
| 243 | } |
| 244 | else |
| 245 | { |
| 246 | sockaddr_in client; |
| 247 | socklen_t clientLen = sizeof (sockaddr); |
| 248 | |
| 249 | bytesThisTime = ::recvfrom (handle, buffer, numToRead, 0, (sockaddr*) &client, &clientLen); |
| 250 | |
| 251 | *senderIP = String::fromUTF8 (inet_ntoa (client.sin_addr), 16); |
| 252 | *senderPort = ntohs (client.sin_port); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if (bytesThisTime <= 0 || ! connected) |
| 258 | { |
| 259 | if (bytesRead == 0 && blockUntilSpecifiedAmountHasArrived) |
| 260 | bytesRead = -1; |
| 261 | |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | bytesRead = static_cast<int> (bytesRead + bytesThisTime); |
| 266 | |
| 267 | if (! blockUntilSpecifiedAmountHasArrived) |
| 268 | break; |
| 269 | } |
| 270 |
no test coverage detected