anetRead needs to handle again and eintr when read return -1 ?

Comment From: ljluestc


#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>

int anetRead(int fd, char *buf, int count) {
    int nread, totlen = 0;
    while(totlen < count) {
        nread = read(fd, buf + totlen, count - totlen);
        if (nread == 0) return totlen; // End of file
        if (nread == -1) {
            if (errno == EINTR) {
                /* Interrupted by a signal, try again */
                continue;
            } else if (errno == EAGAIN) {
                /* Resource temporarily unavailable; in non-blocking mode, this means 
                   we should return what we've read so far if any, or handle accordingly */
                if (totlen > 0) return totlen; // Return partial read if any
                return -1; // Or handle as error if no data was read
            } else {
                /* Other errors */
                return -1;
            }
        }
        totlen += nread;
    }
    return totlen;
}