C++ libevent:为什么在他们的示例中evutil_使_socket_非阻塞调用了两次

C++ libevent:为什么在他们的示例中evutil_使_socket_非阻塞调用了两次,c++,libevent,C++,Libevent,我正在查看上的LibEvent示例-为什么在该示例中,evutil\u make\u socket\u nonblocking(make socket non blocking)函数被调用两次 她的代码(我只是从LibEvent页面复制的): 中的sockaddr\u的/**/ #包括 /*用于套接字函数*/ #包括 /*对于fcntl*/ #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #定义最大值线16384 void do_read(evutil_socke

我正在查看上的LibEvent示例-为什么在该示例中,
evutil\u make\u socket\u nonblocking
(make socket non blocking)函数被调用两次

她的代码(我只是从LibEvent页面复制的):

中的sockaddr\u的
/**/
#包括
/*用于套接字函数*/
#包括
/*对于fcntl*/
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大值线16384
void do_read(evutil_socket_t fd,短事件,void*arg);
void do_write(evutil_socket_t fd,短事件,void*arg);
烧焦
rot13_字符(字符c)
{
/*我们不想在这里使用isalpha;设置区域设置会改变
*哪些字符按字母顺序排列*/
如果((c>='a'&&c='a'&&c='n'&&c='n'&&c=最大线){
/*太长;只需处理存在的内容并继续,以便缓冲区
*不会无限长*/
char-buf[1024];
while(evbuffer_get_length(输入)){
int n=evbuffer_remove(输入,buf,sizeof(buf));
对于(i=0;ifd_设置大小){
关闭(fd);
}否则{
结构缓冲事件*bev;
evutil_使_插座_非阻塞(fd);
bev=bufferevent\u socket\u new(基本、fd、bev\u OPT\u关闭\u开启\u空闲);
bufferevent_setcb(bev、readcb、NULL、errorcb、NULL);
bufferevent_设置水印(bev、EV_读取、0、最大线);
bufferevent_enable(bev、EV_读、EV_写);
}
}
无效的
运行(无效)
{
evutil_套接字_t侦听器;
sin中的结构sockaddr_;
结构事件库*base;
结构事件*侦听器事件;
base=事件\基础\新();
如果(!base)
返回;/*xxxer*/
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=0;
sin.sin_port=htons(40713);
侦听器=socket(AF_INET,SOCK_STREAM,0);
evutil_使_套接字_非阻塞(侦听器);
#ifndef WIN32
{
int-one=1;
setsockopt(监听器,SOL_套接字,SO_REUSEADDR,&one,sizeof(one));
}
#恩迪夫
if(bind(listener,(struct sockaddr*)&sin,sizeof(sin))<0){
佩罗(“绑定”);
返回;
}

if(listen(listener,16)为侦听套接字调用一次,为每个接受的套接字调用一次

/* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>

#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define MAX_LINE 16384

void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);

char
rot13_char(char c)
{
    /* We don't want to use isalpha here; setting the locale would change
     * which characters are considered alphabetical. */
    if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
        return c + 13;
    else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
        return c - 13;
    else
        return c;
}

void
readcb(struct bufferevent *bev, void *ctx)
{
    struct evbuffer *input, *output;
    char *line;
    size_t n;
    int i;
    input = bufferevent_get_input(bev);
    output = bufferevent_get_output(bev);

    while ((line = evbuffer_readln(input, &n, EVBUFFER_EOL_LF))) {
        for (i = 0; i < n; ++i)
            line[i] = rot13_char(line[i]);
        evbuffer_add(output, line, n);
        evbuffer_add(output, "\n", 1);
        free(line);
    }

    if (evbuffer_get_length(input) >= MAX_LINE) {
        /* Too long; just process what there is and go on so that the buffer
         * doesn't grow infinitely long. */
        char buf[1024];
        while (evbuffer_get_length(input)) {
            int n = evbuffer_remove(input, buf, sizeof(buf));
            for (i = 0; i < n; ++i)
                buf[i] = rot13_char(buf[i]);
            evbuffer_add(output, buf, n);
        }
        evbuffer_add(output, "\n", 1);
    }
}

void
errorcb(struct bufferevent *bev, short error, void *ctx)
{
    if (error & BEV_EVENT_EOF) {
        /* connection has been closed, do any clean up here */
        /* ... */
    } else if (error & BEV_EVENT_ERROR) {
        /* check errno to see what error occurred */
        /* ... */
    } else if (error & BEV_EVENT_TIMEOUT) {
        /* must be a timeout event handle, handle it */
        /* ... */
    }
    bufferevent_free(bev);
}

void
do_accept(evutil_socket_t listener, short event, void *arg)
{
    struct event_base *base = arg;
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr*)&ss, &slen);
    if (fd < 0) {
        perror("accept");
    } else if (fd > FD_SETSIZE) {
        close(fd);
    } else {
        struct bufferevent *bev;
        evutil_make_socket_nonblocking(fd);
        bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
        bufferevent_setcb(bev, readcb, NULL, errorcb, NULL);
        bufferevent_setwatermark(bev, EV_READ, 0, MAX_LINE);
        bufferevent_enable(bev, EV_READ|EV_WRITE);
    }
}

void
run(void)
{
    evutil_socket_t listener;
    struct sockaddr_in sin;
    struct event_base *base;
    struct event *listener_event;

    base = event_base_new();
    if (!base)
        return; /*XXXerr*/

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(40713);

    listener = socket(AF_INET, SOCK_STREAM, 0);
    evutil_make_socket_nonblocking(listener);

#ifndef WIN32
    {
        int one = 1;
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    }
#endif

    if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        perror("bind");
        return;
    }

    if (listen(listener, 16)<0) {
        perror("listen");
        return;
    }

    listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    /*XXX check it */
    event_add(listener_event, NULL);

    event_base_dispatch(base);
}

int
main(int c, char **v)
{
    setvbuf(stdout, NULL, _IONBF, 0);

    run();
    return 0;
}