zoukankan      html  css  js  c++  java
  • 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.
    ");
                }
                event.data.fd = conn_sock;
                event.events = EPOLLIN|EPOLLET;
                printf("v5cli process new client(%d)
    ",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);
            }
        }
  • 相关阅读:
    gearman任务分发改进
    gearman实现任务分发
    BeanStalkd 做队列服务
    Tomcat各种日志的关系与catalina.out文件的分割
    数据库系统原理-关系数据库的规范化理论总结
    MySQL配置参数innodb_flush_log_at_trx_commit
    gRPC快速入门
    使用vagrant和kubeadm搭建k8s集群
    VS项目属性中的C/C++运行库:MT、MTd、MD、MDd
    消除C++中警告代码
  • 原文地址:https://www.cnblogs.com/SaraMoring/p/10410750.html
Copyright © 2011-2022 走看看