//////////////////////////////////////////////////////////////// Function name : Socket::IsReadDataAvailable Argument : int secondsWait -- Seconds part of how long to wait for data in secs (0 is default) Argument : int millisecondssecondsWait -- Milliseconds part of how long to wait for data in usecs (0 is default, must be < 1000) Return type : bool Description : Returns true
| 271 | // |
| 272 | ///////////////////////////////////////////////////////////////////// |
| 273 | bool Socket::IsReadDataAvailable(int secondsWait, int millisecondsWait) |
| 274 | { |
| 275 | assert(millisecondsWait < 1000 && "specified milliseconds must be less than 1000"); |
| 276 | |
| 277 | CTDEBUG_ENTER_METHOD("Socket::IsReadDataAvailable"); |
| 278 | |
| 279 | SOCKET hSock = m_hSocket ; |
| 280 | |
| 281 | if (!hSock) |
| 282 | { |
| 283 | if (m_bTraceCommunications) |
| 284 | { |
| 285 | sml::PrintDebug("Error: Can't check for read data because this socket is closed") ; |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | fd_set set ; |
| 291 | FD_ZERO(&set) ; |
| 292 | |
| 293 | ////// |
| 294 | // This _MSC_VER test is legit, for a warning C4127: conditional expression is constant in a |
| 295 | // windows-defined FD_SET macro below |
| 296 | #ifdef _MSC_VER |
| 297 | #pragma warning(push, 3) |
| 298 | #endif |
| 299 | // Add hSock to the set of descriptors to check |
| 300 | // This generates a warning on level 4 in VC++ 2005. |
| 301 | FD_SET(hSock, &set) ; |
| 302 | #ifdef _MSC_VER |
| 303 | #pragma warning(pop) |
| 304 | #endif |
| 305 | ////// |
| 306 | |
| 307 | // Wait for milliseconds for select to return (can be 0 to just poll) |
| 308 | TIMEVAL zero ; |
| 309 | zero.tv_sec = secondsWait ; |
| 310 | zero.tv_usec = millisecondsWait * 1000 ; |
| 311 | |
| 312 | // Check if anything is waiting to be read |
| 313 | int res = select(static_cast<int>(hSock) + 1, &set, NULL, NULL, &zero) ; |
| 314 | |
| 315 | // Did an error occur? |
| 316 | if (res == SOCKET_ERROR) |
| 317 | { |
| 318 | if (m_bTraceCommunications) |
| 319 | { |
| 320 | sml::PrintDebug("Error: Error checking if data is available to be read") ; |
| 321 | } |
| 322 | sml::ReportSystemErrorMessage() ; |
| 323 | Close() ; // If the socket has an error we'll shut it down |
| 324 | return false ; |
| 325 | } |
| 326 | |
| 327 | bool bIsSet = FD_ISSET(hSock, &set) ? true : false ; |
| 328 | /* |
| 329 | if (bIsSet) |
| 330 | sml::PrintDebug("Read data IS available") ; |