zoukankan      html  css  js  c++  java
  • 使用kqueue的str_cli函数

    void str_cli(FILE *fp, int sockfd)
    {
        int              kq, i, n, nev, stdineof = 0, isfile;
        char             buf[MAXLINE];
        struct kevent    kev[2];
        struct timespec  ts;
        struct stat      st;
    
        isfile = ((fstat(fileno(fp), &st) == 0) &&
                  (st.st_mode & S_IFMT) == S_IFREG);
    
        EV_SET(&kev[0], fileno(fp), EVFILT_READ, EV_ADD, 0, 0, NULL);
        EV_SET(&kev[1], sockfd, EVFILT_READ, EV_ADD, 0, 0, NULL);
    
        kq = kqueue();
        ts.tv_sec = ts.tv_nsec = 0;
        kevent(kq, kev, 2, NULL, 0, &ts);
    
        for ( ; ; ) {
            nev = kevent(kq, NULL, 0, kev, 2, NULL);
    
            for (i = 0; i < nev; i++) {
                if (kev[i].ident == sockfd) {    /* socket is readable */
                    if ( (n = read(sockfd, buf, MAXLINE)) == 0) {
                        if (stdineof == 1)
                            return;              /* normal termination */
                        else
                            err_quit("str_cli: server terminated prematurely");
                    }
    
                    write(fileno(stdout), buf, n);
                }
    
                if (kev[i].ident == fileno(fp)) {    /* input is readable */
                    n = read(fileno(fp), buf, MAXLINE);
                    if (n > 0)
                        writen(sockfd, buf, n);
    
                    if (n == 0 || (isfile && n == kev[i].data)) {
                        stdineof = 1;
                        shutdown(sockfd, SHUT_WR);    /* send FIN */
                        kev[i].flags = EV_DELETE;
                        kevent(kq, &kev[i], 1, NULL, 0, &ts);    /* remove kevent */
                        continue;
                    }
                }
            }
        }
    }
  • 相关阅读:
    问题:charles开启Charles-Proxy-macOS Proxy 时报错
    通关中级测评师
    20210104 递归
    20201231-3 字符编码转换详解1
    20201231-2 元组
    20201231-1 购物车程序练习实例
    20201230-3 bytes数据类型
    20201230 python数据类型
    20201230 pyc是什么
    20201230-1 网络基础知识
  • 原文地址:https://www.cnblogs.com/soldierback/p/10764828.html
Copyright © 2011-2022 走看看