int ConnReadRaw(TConn *c, char *buffer, uint32 len, uint32 timeout, int *readLen) { int bufflen = 0; uint32 toread = len; /* sanity */ if (!c || !buffer || !readLen) { return FALSE; } bufflen = c->buffersize - c->bufferpos; if (bufflen > 0) { if (toread > (size_t)bufflen) toread = bufflen; memcpy(buffer, c->buffer + c->bufferpos, toread); c->bufferpos += toread; *readLen = toread; return TRUE; } else { /* we have to read from the socket */ if (SocketWait(&(c->socket),TRUE,FALSE,timeout*1000)==1) { uint32 avail=SocketAvailableReadBytes(&(c->socket)); if (avail < toread) toread = avail; *readLen = SocketRead(&(c->socket), buffer, toread); if (*readLen > 0) return TRUE; } *readLen = 0; return FALSE; } }
abyss_bool ConnRead(TConn * const connectionP, uint32_t const timeout) { /*---------------------------------------------------------------------------- Read some stuff on connection *connectionP from the socket. Don't wait more than 'timeout' seconds for data to arrive. Fail if nothing arrives within that time. -----------------------------------------------------------------------------*/ time_t const deadline = time(NULL) + timeout; abyss_bool cantGetData; abyss_bool gotData; cantGetData = FALSE; gotData = FALSE; while (!gotData && !cantGetData) { int const timeLeft = deadline - time(NULL); if (timeLeft <= 0) cantGetData = TRUE; else { int rc; rc = SocketWait(connectionP->socketP, TRUE, FALSE, timeLeft * 1000); if (rc != 1) cantGetData = TRUE; else { uint32_t bytesAvail; bytesAvail = SocketAvailableReadBytes(connectionP->socketP); if (bytesAvail <= 0) cantGetData = TRUE; else { uint32_t const bytesToRead = MIN(bytesAvail, bufferSpace(connectionP)-1); uint32_t bytesRead; bytesRead = SocketRead( connectionP->socketP, (unsigned char*)connectionP->buffer + connectionP->buffersize, bytesToRead); if (bytesRead > 0) { traceSocketRead(connectionP, bytesRead); connectionP->inbytes += bytesRead; connectionP->buffersize += bytesRead; connectionP->buffer[connectionP->buffersize] = '\0'; gotData = TRUE; } } } } } if (gotData) return TRUE; else return FALSE; }