Unix Network Programming Episode 94

The kqueue interface includes the following two functions and macro:

#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
int kqueue(void);
int kevent(int kq, const struct kevent *changelist, int nchanges, struct kevent *eventlist, int nevents, const struct timespec *timeout) ;
void EV_SET(struct kevent *kev, uintptr_t ident, short filter, u_short flags, u_int fflags, intptr_t data, void *udata);

The kevent structure is defined by including the <sys/event.h> header.

struct kevent {
	uintptr_t ident; /* identifier (e.g., file descriptor) */
	short filter; /* filter type (e.g., EVFILT_READ) */
	u_short flags; /* action flags (e.g., EV_ADD) */
	u_int fflags; /* filter-specific flags */
	intptr_t data; /* filter-specific data */
	void *udata; /* opaque user data */
};
#include "unp.h"

void str_client(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),EVFILE_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)
            {
                if((n=Read(sockfd, buf, MAXLINE))==0)
                {
                    if(stdineof==1)
                        return;
                    else
                        err_quit("str_cli: server terminated prematurely");
                }

                Write(fileno(stdout), buf, n);
            }

            if(kev[i].ident==fileno(fp))
            {
                if(n>0)
                    Writen(sockfd, buf, n);
                
                if(n==0||(isfile&&n==kev[i].data))
                {
                    stdineof=1;
                    Shutdown(sockfd, SHUT_WR);
                    kev[i].flags=EV_DELETE;
                    Kevent(kq, &kev[i], 1, NULL, 0, &ts);

                    continue;
                }
            }
        }
    }
}

str_cli function using kqueue.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值