libevent源码学习6---链接监听器evconnlistener

libevent源码学习6---链接监听器evconnlistener

evconnlistener 机制提供了监听和接受 TCP 连接的方法。

c 复制代码
struct evconnlistener {
	const struct evconnlistener_ops *ops;
	void *lock;
	evconnlistener_cb cb;
	evconnlistener_errorcb errorcb;
	void *user_data;
	unsigned flags;
	short refcnt;
	int accept4_flags;
	unsigned enabled : 1;
};

1. 创建和释放evconnlistener

c 复制代码
/**
   Allocate a new evconnlistener object to listen for incoming TCP connections
   on a given file descriptor.

   @param base The event base to associate the listener with.
   @param cb A callback to be invoked when a new connection arrives.  If the
      callback is NULL, the listener will be treated as disabled until the
      callback is set.
   @param ptr A user-supplied pointer to give to the callback.
   @param flags Any number of LEV_OPT_* flags
   @param backlog Passed to the listen() call to determine the length of the
      acceptable connection backlog.  Set to -1 for a reasonable default.
      Set to 0 if the socket is already listening.
   @param fd The file descriptor to listen on.  It must be a nonblocking
      file descriptor, and it should already be bound to an appropriate
      port and address.
*/
struct evconnlistener *evconnlistener_new(struct event_base *base,
    evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
    evutil_socket_t fd);
c 复制代码
/**
   Allocate a new evconnlistener object to listen for incoming TCP connections
   on a given address.

   @param base The event base to associate the listener with.
   @param cb A callback to be invoked when a new connection arrives. If the
      callback is NULL, the listener will be treated as disabled until the
      callback is set.
   @param ptr A user-supplied pointer to give to the callback.
   @param flags Any number of LEV_OPT_* flags
   @param backlog Passed to the listen() call to determine the length of the
      acceptable connection backlog.  Set to -1 for a reasonable default.
   @param addr The address to listen for connections on.
   @param socklen The length of the address.
 */
struct evconnlistener *evconnlistener_new_bind(struct event_base *base,
    evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
    const struct sockaddr *sa, int socklen);

两个函数都分配和返回一个新的链接监听器对象。差异在于后面的参数,前者是根据给定文件描述符,后者是根据给定的地址。

c 复制代码
/**
   Disable and deallocate an evconnlistener.
 */
void evconnlistener_free(struct evconnlistener *lev);

1.1 可识别的标志

可以给 evconnlistener_new() 函数的 flags 参数传入一些标志。可以用或 运算任意连接下述几个常用的标志:

  • LEV_OPT_LEAVE_SOCKETS_BLOCKING
    默认情况下,连接监听器接收新套接字后,会将其设置为非阻塞的,以便将其用于 libevent。如果不想要这种行为,可以设置这个标志。
  • LEV_OPT_CLOSE_ON_FREE
    如果设置了这个选项,释放连接监听器会关闭底层套接字。
  • LEV_OPT_CLOSE_ON_EXEC
    如果设置了这个选项,连接监听器会为底层套接字设置 close-on-exec 标志。更多信息请查看 fcntl 和 FD_CLOEXEC 的平台文档。
  • LEV_OPT_REUSEABLE
    某些平台在默认情况下,关闭某监听套接字后,要过一会儿其他套接字才可以绑定到同一个端口。设置这个标志会让 libevent 标记套接字是可重用的,这样一旦关闭,可以立即打开其他套接字,在相同端口进行监听。
  • LEV_OPT_THREADSAFE
    指示侦听器应被锁定,以便可以安全地同时从多个线程使用

文档全部标志,感兴趣可以看看

c 复制代码
/** Flag: Indicates that we should not make incoming sockets nonblocking
 * before passing them to the callback. */
#define LEV_OPT_LEAVE_SOCKETS_BLOCKING	(1u<<0)
/** Flag: Indicates that freeing the listener should close the underlying
 * socket. */
#define LEV_OPT_CLOSE_ON_FREE		(1u<<1)
/** Flag: Indicates that we should set the close-on-exec flag, if possible */
#define LEV_OPT_CLOSE_ON_EXEC		(1u<<2)
/** Flag: Indicates that we should disable the timeout (if any) between when
 * this socket is closed and when we can listen again on the same port. */
#define LEV_OPT_REUSEABLE		(1u<<3)
/** Flag: Indicates that the listener should be locked so it's safe to use
 * from multiple threadcs at once. */
#define LEV_OPT_THREADSAFE		(1u<<4)
/** Flag: Indicates that the listener should be created in disabled
 * state. Use evconnlistener_enable() to enable it later. */
#define LEV_OPT_DISABLED		(1u<<5)
/** Flag: Indicates that the listener should defer accept() until data is
 * available, if possible.  Ignored on platforms that do not support this.
 *
 * This option can help performance for protocols where the client transmits
 * immediately after connecting.  Do not use this option if your protocol
 * _doesn't_ start out with the client transmitting data, since in that case
 * this option will sometimes cause the kernel to never tell you about the
 * connection.
 *
 * This option is only supported by evconnlistener_new_bind(): it can't
 * work with evconnlistener_new_fd(), since the listener needs to be told
 * to use the option before it is actually bound.
 */
#define LEV_OPT_DEFERRED_ACCEPT		(1u<<6)
/** Flag: Indicates that we ask to allow multiple servers (processes or
 * threads) to bind to the same port if they each set the option. 
 * 
 * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however
 * SO_REUSEPORT does not imply SO_REUSEADDR.
 *
 * This is only available on Linux and kernel 3.9+
 */
#define LEV_OPT_REUSEABLE_PORT		(1u<<7)
/** Flag: Indicates that the listener wants to work only in IPv6 socket.
 *
 * According to RFC3493 and most Linux distributions, default value is to
 * work in IPv4-mapped mode. If there is a requirement to bind same port
 * on same ip addresses but different handlers for both IPv4 and IPv6,
 * it is required to set IPV6_V6ONLY socket option to be sure that the
 * code works as expected without affected by bindv6only sysctl setting in
 * system.
 *
 * This socket option also supported by Windows.
 */
#define LEV_OPT_BIND_IPV6ONLY		(1u<<8)

1.2 链接监听器回调

c 复制代码
/**
   A callback that we invoke when a listener has a new connection.

   @param listener The evconnlistener
   @param fd The new file descriptor
   @param addr The source address of the connection
   @param socklen The length of addr
   @param user_arg the pointer passed to evconnlistener_new()
 */
typedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, 
			  struct sockaddr *, int socklen, void *);

2. 启用和禁用 evconnlistener

c 复制代码
int evconnlistener_disable(struct evconnlistener *lev);
int evconnlistener_enable(struct evconnlistener *lev);

这两个函数暂时禁止或者重新允许监听新连接。

3. 调整 evconnlistener 的回调函数

c 复制代码
/** Change the callback on the listener to cb and its user_data to arg.
 */
void evconnlistener_set_cb(struct evconnlistener *lev,
   						   evconnlistener_cb cb, void *arg);

4. 检测 evconnlistener

c 复制代码
/** Return the socket that an evconnlistner is listening on. */
evutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev);
c 复制代码
/** Return an evconnlistener's associated event_base. */
struct event_base *evconnlistener_get_base(struct evconnlistener *lev);

这两个函数分别返回监听器关联的套接字和 event_base。

5. 侦测错误

可以设置一个一旦监听器上的 accept()调用失败就被调用的错误回调函数。对于一个不解决就会锁定进程的错误条件,这很重要。

c 复制代码
/**
   A callback that we invoke when a listener encounters a non-retriable error.

   @param listener The evconnlistener
   @param user_arg the pointer passed to evconnlistener_new()
 */
typedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *);

/** Set an evconnlistener's error callback. */
void evconnlistener_set_error_cb(struct evconnlistener *lev,
    evconnlistener_errorcb errorcb);
相关推荐
风景依旧在24 天前
Libevent库-http通信不同请求方式的处理
c++·http·libevent
胡西风_foxww1 个月前
linux下编译安装memcached
linux·数据库·缓存·编译·安装·memcached·libevent
whowin2 个月前
C程序员应该知道的最好的8个C编程框架
qt·libevent·gtk·libuv·glib·cmocka·ncurses
一眼万里*e5 个月前
libevent入门篇
c++·libevent
Philosophy6 个月前
libevent之evbuffer
libevent
dvlinker8 个月前
开源事件通知库libevent及网络连接管理模块bufferevent详解
libevent·回调·数据读写·开源事件通知库·网络连接管理·bufferevent·心跳处理
一只特立独行的猪( ﹡ˆoˆ﹡9 个月前
Libevent的使用及reactor模型
reactor·libevent
小胖西瓜10 个月前
如何在 libevent 中读取超过 4096 字节的数据
libevent
shanql1 年前
使用libevent实现回显服务器
libevent
OopspoO1 年前
libevent学习——event_base
c语言·学习·libevent