Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
238 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...