示例#1
0
static void
readFromChannel(TConn *       const connectionP,
                bool *        const eofP,
                const char ** const errorP) {
/*----------------------------------------------------------------------------
   Read some data from the channel of Connection *connectionP.

   Iff there is none available to read, return *eofP == true.
-----------------------------------------------------------------------------*/
    uint32_t bytesRead;
    bool readError;

    ChannelRead(connectionP->channelP,
                connectionP->buffer.b + connectionP->buffersize,
                bufferSpace(connectionP) - 1,
                &bytesRead, &readError);

    if (readError)
        xmlrpc_asprintf(errorP, "Error reading from channel");
    else {
        *errorP = NULL;
        if (bytesRead > 0) {
            *eofP = FALSE;
            traceChannelRead(connectionP, bytesRead);
            connectionP->inbytes += bytesRead;
            connectionP->buffersize += bytesRead;
            connectionP->buffer.t[connectionP->buffersize] = '\0';
        } else
            *eofP = TRUE;
    }
}
示例#2
0
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;
}