socke+epoll

读:

    #define V5CLI_READ_MSG_LEN 1024
    char readMsg[V5CLI_READ_MSG_LEN];
    int n = 0;
    int nread = 0;
    while((nread = read(eventfd, readMsg + n, V5CLI_READ_MSG_LEN-1)) > 0){                    
        n += nread;                
    }//读到EAGAIN,说明读完了
    if(nread < 0 && errno != EAGAIN) {
        V5CLI_LOG_ERROR("v5cli process read err(%d)",errno);
        close(eventfd);
        eventList[loop].data.fd = -1;
        continue;
    }

写:

    unsigned long data_size = VOS_StrLen(pszString);
    int n = data_size;
    int nwrite = 0;
    while (n > 0) {
        nwrite = write(connect_fd, (char *)(pszString + data_size-n), n);
        if (nwrite < n) {
            if (nwrite == -1 && errno != EAGAIN) {
                perror("write error");
            }
            break;
        }
        n -= nwrite;
    }

accept:

    if(eventList[loop].data.fd == serverfd){
        clientLen = sizeof(struct sockaddr_un);
        memset(&client, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un));
        while((conn_sock = accept(serverfd, (struct sockaddr*)&client, &clientLen)) > 0)
        {
            /* 将socket设置为非阻塞模式 */
            if(nonBlockSocket(conn_sock) != V5CLI_OK) {
                printf("v5cli process nonblock failed.\n");
            }
            event.data.fd = conn_sock;
            event.events = EPOLLIN|EPOLLET;
            printf("v5cli process new client(%d)\r\n",conn_sock);
            epoll_ctl(epollfd, EPOLL_CTL_ADD, conn_sock,&event); //将新的conn_sock添加到epoll的监听队列中
            memset(&client, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un));
            
            /*if(nonBlockSocket(conn_sock) != V5CLI_OK) {
                printf("v5cli process set non block fail");
                goto END_PRO;
            }*/
        }
        if(conn_sock < 0 && errno != EAGAIN) {
            printf("v5cli process accept err %d", conn_sock);
        }
    }

 

你可能感兴趣的:(socke+epoll)