Ejemplo n.º 1
0
void LocalClient::readMore()
{
    enum { BufSize = 1024, MaxBufferSize = 1024 * 1024 * 16 };

#ifdef HAVE_NOSIGNAL
    const int recvflags = MSG_NOSIGNAL;
#else
    const int recvflags = 0;
#endif
    char buf[BufSize];
    int read = 0;
    bool wasDisconnected = false;
    for (;;) {
        int r;
        eintrwrap(r, ::recv(mFd, buf, BufSize, recvflags));

        if (r == -1) {
            break;
        } else if (!r) {
            wasDisconnected = true;
            break;
        }
        read += r;
        mReadBuffer.resize(r + mReadBuffer.size());
        memcpy(mReadBuffer.data() + mReadBuffer.size() - r, buf, r);
        if (mReadBuffer.size() + r >= MaxBufferSize) {
            if (mReadBuffer.size() + r - mReadBufferPos < MaxBufferSize) {
                mReadBuffer.remove(0, mReadBufferPos);
                mReadBufferPos = 0;
            } else {
                error("Buffer exhausted (%d), dropping on the floor", mReadBuffer.size());
                mReadBuffer.clear();
            }
        }
    }

    if (read && !mReadBuffer.isEmpty())
        mDataAvailable(this);
    if (wasDisconnected)
        disconnect();
}
Ejemplo n.º 2
0
void SocketClient::readMore()
{
    enum { BufSize = 1024, MaxBufferSize = 1024 * 1024 * 128 };

#ifdef HAVE_NOSIGNAL
    const int recvflags = MSG_NOSIGNAL;
#else
    const int recvflags = 0;
#endif
    char buf[BufSize];
    int read = 0;
    bool wasDisconnected = false;

    sockaddr_in addr_in;
    socklen_t len_in;

    sockaddr* addr;
    socklen_t* len;
    if (mMode == Udp) {
        addr = reinterpret_cast<sockaddr*>(&addr_in);
        len = &len_in;
    } else {
        addr = 0;
        len = 0;
    }

    for (;;) {
        int r;
        if (addr) {
            memset(addr, 0, sizeof(sockaddr_in));
            *len = sizeof(sockaddr_in);
        }
        eintrwrap(r, ::recvfrom(mFd, buf, BufSize, recvflags, addr, len));

        if (r == -1) {
            break;
        } else if (!r && mMode != Udp) {
            wasDisconnected = true;
            break;
        }
        read += r;
        mReadBuffer.resize(r + mReadBuffer.size());
        memcpy(mReadBuffer.data() + mReadBuffer.size() - r, buf, r);
        if (mReadBuffer.size() + r >= MaxBufferSize) {
            if (mReadBuffer.size() + r - mReadBufferPos < MaxBufferSize) {
                mReadBuffer.remove(0, mReadBufferPos);
                mReadBufferPos = 0;
            } else {
                error("Buffer exhausted (%d), dropping on the floor", mReadBuffer.size());
                mReadBuffer.clear();
            }
        }

        if (mMode == Udp && !mReadBuffer.isEmpty()) {
            mUdpDataAvailable(this, getname(&addr_in), ntohs(addr_in.sin_port), mReadBuffer);
            mReadBuffer.clear();
            read = 0;
        }
    }

    if (read && mMode != Udp && !mReadBuffer.isEmpty())
        mDataAvailable(this);
    if (wasDisconnected)
        disconnect();
}