WriteLen

programming/C_C++ 2017. 12. 5. 14:44
반응형

 


/*** nRet = WriteLen(nFd, vPtrMsg, nExpectLen, &nPtrWriteLen); ***/

int WriteLen(int nFd, void *vPtrMsg, size_t nExpectLen, int *nPtrWriteLen)
{
    int        nRemainLen = 0;
    int        nWriteOneLen = 0;
    int        nWriteTotalLen = 0;

    unsigned char *ptr = NULL;

    /* init_value */
    nRemainLen = nExpectLen;
    nWriteTotalLen = 0;

    ptr = vPtrMsg;

    while (nRemainLen > 0) {

        nWriteOneLen = write(nFd, ptr, nRemainLen);

        if ( nWriteOneLen <= 0) {

            if (errno == EINTR) {  /* Interrupted system call */

                errno = 0;

                continue;  /* and call write() again */

            }
            else if ( (errno == EWOULDBLOCK) || (errno == EAGAIN) ) {  /* Operation would block */

                *nPtrWriteLen = nWriteTotalLen;

                return (-2);

            }
            else {

                fprintf(stderr, "Writen() error : nFd[%d] errno[%d][%s]", nFd, strerror(errno));

                *nPtrWriteLen = nWriteTotalLen;

                return (-1);  /* error */

            }

        }

        nWriteTotalLen += nWriteOneLen;
        nRemainLen -= nWriteOneLen;

        ptr   += nWriteOneLen;

    }

    *nPtrWriteLen = nWriteTotalLen;

    return nWriteTotalLen;

}

 

반응형
Posted by 공간사랑
,