简体   繁体   English

C ++非阻塞阅读

[英]C++ non-blocking reading

All I need to do is just to read all available bytes from socket. 我需要做的只是从套接字读取所有可用字节。 But there is one condition: there is a method which reads n bytes from non-blocking socket which I have to use for implementation of another method which is going to read all available bytes. 但是有一个条件:存在一种从非阻塞套接字读取n个字节的方法,我必须使用该方法来实现另一种方法,该方法将读取所有可用字节。

Non-blocking reading of some data with defined size: 无阻塞读取某些定义大小的数据:

ssize_t read(void* buf, size_t len)
{
    ssize_t bytesRead = 0;
    ssize_t bytesTotallyRead = 0;
    size_t bytesLeftToRead = len;
    while (bytesTotallyRead < len)
    {
        bytesRead = ::recv(handle, (char*)buf+bytesTotallyRead, bytesLeftToRead, 0);
        if (bytesRead > 0)
        {
            bytesTotallyRead += bytesRead;
            bytesLeftToRead -= bytesRead;
        }
        else if (bytesRead == 0)
        {
            break;
        }
        else if (bytesRead < 0)
        {
            if (errno == EINTR)
            {
                continue;
            }
            else if (errno == EWOULDBLOCK)
            {
                int selectStatus(waitForIncomingData(500));
                if (selectStatus > 0)
                {
                    continue;
                }
                else
                {
                    break;
                }
            }
            else
            {
                break;
            }
        }
    }
    return (bytesRead==-1)?-1:bytesTotallyRead;
}

So I have to use this method to implement something like ssize_t readAllAvailable(std::vector<uint8_t> &data) which uses method above for it's implementation. 因此,我必须使用此方法来实现类似ssize_t readAllAvailable(std::vector<uint8_t> &data) ,该实现使用上面的方法。

The problem here, as you see, that read() uses select() for waiting for incoming data so using of buffer of fixed size (in loop) might lead to 500 ms (in this concrete case) waiting after 1st iteration and this is definitely not the way it should be done. 如您所见,这里的问题是read()使用select()等待输入数据,因此使用固定大小的缓冲区(循环中)可能导致第一次迭代后等待500毫秒(在这种情况下),这是绝对不是应该做的方式。

Is it possible at all? 有可能吗? I mean some efficient ways. 我的意思是一些有效的方法。

there is a method which reads n bytes from socket asynchronously 有一种方法可以从套接字异步读取n个字节

No there isn't. 不,没有。 There is a method which uses non-blocking mode and select() to reimplement blocking mode. 有一种使用非阻塞模式和select()重新实现阻塞模式的方法。 There is nothing 'asynchronous' about it whatsoever. 没有任何“异步”的地方。 From the point of view of the caller it is blocking-mode. 从调用者的角度来看,它是阻止模式。 It is poorly, indeed incorrectly, conceived, and should be thrown away. 它的构思很差,实际上是不正确的,应该扔掉。 The code you have posted is completely and utterly pointless. 您发布的代码是完全毫无意义的。 It's just a complicated way of implementing a blocking read loop. 这只是实现阻塞读取循环的一种复杂方法。 Use blocking mode. 使用阻止模式。 In effect, you already are, but with more system calls. 实际上,您已经是,但是有更多的系统调用。 And lose the select() call and EAGAIN/EWOUDLBLOCK stuff. 并失去select()调用和EAGAIN / EWOUDLBLOCK之类的东西。

which I have to use for implementation of another method which is going to read all available bytes. 我必须使用它来实现另一种方法,该方法将读取所有可用字节。 ... The problem here, as you see, that read() uses select() for waiting for incoming data so using of buffer of fixed size (in loop) might lead to 500 ms (in this concrete case) waiting after 1st iteration …如您所见,这里的问题是read()使用select()等待输入数据,因此使用固定大小的缓冲区(循环中)可能导致第一次迭代后等待500毫秒(在这种情况下)

No it isn't. 不,不是。 select() will return immediately data becomes available on the socket. select()将立即返回套接字上可用的数据。 No 500ms unless there is no data. 除非没有数据,否则没有500ms。 And 'using a buffer of fixed size' has exactly nothing to do with it. 而“使用固定大小的缓冲区”与它完全无关。

and this is definitely not the way it should be done. 这绝对不是应该做的方式。

It isn't the way it is done. 这不是完成的方式。

There is no problem here to solve. 这里没有问题可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM