0

I'm debugging in VS2010. BIO_do_connect() fails in the following code. What am I doing wrong?

(pBio is properly set up before use)

static const uint32_t kuSleepIntervalInMs = 50;

...
uint32_t uTimeTaken = 0;
...

BIO_set_nbio(pBio, 1);

for (;;)
{
    if (uTimeTaken > 10000)
        return ERR_CONNECTION_TIMED_OUT;

    if (BIO_do_connect(pBio) > 0)
        break;

    if (BIO_should_retry(pBio))
    {
        Sleep(kuSleepIntervalInMs);

        uTimeTaken += kuSleepIntervalInMs;

        continue;
    }

    BIO_free_all(pBio);

    return ERR_FAILED_TO_ESTABLISH_CONNECTION;
}

It appears that if I increase the sleep interval (for example to 500), BIO_do_connect works fine but I'd like to know why it fails with shorter interval values.

4

1 回答 1

0

Since posting my original question, I've switched to use select() so the problem is no longer valid.

Instead of doing

uTimeTaken += kuSleepIntervalInMs;

I'm now doing:

int nRet;
int fdSocket;
fd_set connectionfds;
struct timeval timeout;

BIO_set_nbio(pBio, 1);

nRet = BIO_do_connect(pBio);

if ((nRet <= 0) && !BIO_should_retry(pBio))
    // failed to establish connection.

if (BIO_get_fd(pBio, &fdSocket) <= 0)
    // failed to get fd.

if (nRet <= 0)
{
    FD_ZERO(&connectionfds);
    FD_SET(fdSocket, &connectionfds);

    timeout.tv_usec = 0;
    timeout.tv_sec = 10;

    nRet = select(fdSocket + 1, NULL, &connectionfds, NULL, &timeout);
    if (nRet == 0)
        // timeout has occurred.
}

See my other post.

于 2013-04-16T12:07:14.007 回答