The main functions of the communication server and client under the 08libevent library

The main functions of the communication server and client under the 08libevent library

以下是关于libevent学习的相关文章:
01 Download and installation of the libevent library and test whether the installation is successful
02 The overall framework idea of ​​the libevent library
03 The main function of
communication
under the
libevent Area features introduction
07libevent library related functions of the bufferevent event
08libevent library communication server and client main functions
09libevent library server and client TCP communication process and code examples

1 The main function of the server
1) evconnlistener_new(), this function can be understood, its function is equivalent to a new listener object, there is no actual package, you can see 6fd to know, you still need the return of your own socket() Value pass parameters. So mainly use the following one.

     struct evconnlistener * evconnlistener_new( 
               struct event_base *base,                 
               evconnlistener_cb cb,		//监听到有连接过来后,触发该回调函数
               void *ptr,					//回调函数参数,一般传base,因为回调中要使用,但是却没有该base
               unsigned flags, 				//可识别的标志位
               int backlog, 				//相当于listen函数的监听数,-1默认使用最大值
               evutil_socket_t fd			//需要socket()的返回值,所以该函数基本很少用,因为封装后没省略多少步骤。
);

//flags:
//LEV_OPT_CLOSE_ON_FREE :BEV_OPT_CLOSE_ON_FREE 释放 bufferevent 时关闭底层传输端口,这将关闭底层套接字,释放bufferevent对象等。对比bufferevent_socket_new的参数3enum bufferevent_options options

//LEV_OPT_REUSEABLE:端口复用,选用多个flags时,可使用 "|"

2) evconnlistener_new_bind(), this function is equivalent to the four functions of socket, bind, listen and accpet, which is more convenient than the above.

    struct evconnlistener *evconnlistener_new_bind(
                struct event_base *base, 
                evconnlistener_cb cb, 		//监听到有连接过来后,触发该回调函数
                void *ptr,					//回调函数参数,一般传base,因为回调中要使用,但是却没有该base
                unsigned flags,				//看上面
                int backlog,				
                const struct sockaddr *sa, 	//服务端地址
                int socklen
    );

3) The callback function type of the above two functions

        typedef void (*evconnlistener_cb)( 
                struct evconnlistener *listener, 	//上面函数的监听者返回值
                evutil_socket_t sock,				//用于通信的套接字cfd,不需要你管,libevent会帮你传参,你只需要在该回调函数中使用即可
                struct sockaddr *addr,				//上面的服务器地址
                int len,
                void *ptr							//evconnlistener_new_bind的参数3,实际上这个回调函数的最后三个参数都是evconnlistener_new_bind的参数
);    

4) Release of evconnlistener_free listener event object

void evconnlistener_free(struct evconnlistener *lev);

5) Manage the monitoring of the listener and set whether the listener is enabled or disabled. Through the cooperation of these two functions, you can turn off the monitoring in a certain place that may trigger a danger, and then start the monitoring after the danger is over.

        int evconnlistener_enable(struct evconnlistener *lev);
        int evconnlistener_disable(struct evconnlistener *lev);

6) Reset the callback function and its parameters of the listener event.

        void evconnlistener_set_cb( 
            struct evconnlistener *lev,
            evconnlistener_cb cb,
            void *arg 
);

2 The main function of the client
1) bufferevent_socket_new, in fact, the previous article has already mentioned this function.

    struct bufferevent *bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);

2) bufferevent_socket_connect(), which is exactly the same as the connect function. The following notes explain the function of this function in detail.

         int bufferevent_socket_connect( 
         				 struct bufferevent *bev,	//上面的bufferevent对象
                         struct sockaddr *address,  //服务端地址
                         int addrlen 
        );

//1 bufferevent 若未设置套接字,该函数将为其分配一个新的流套接字, 并且设置状态为非阻塞

//2 bufferevent 已设置套接字,调用 bufferevent_socket_connect() 将告知libevent 套接字还未连接,直到连接成功之前不应该对其进行读取或者写入操作。更好的维护了bufferevent缓冲的操作权限。

//3 连接完成之后可以向输出缓冲区添加数据

The function of the client is relatively simple, it is through these two functions to communicate.

Guess you like

Origin blog.csdn.net/weixin_44517656/article/details/108781638