libevent源码分析:listener

Posted 冷冰若水

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了libevent源码分析:listener相关的知识,希望对你有一定的参考价值。

listener是libevent封装的一个方便生成监听者的一组结构和函数,其中包括:

  1 /*
  2  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
  3  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
  4  *
  5  * Redistribution and use in source and binary forms, with or without
  6  * modification, are permitted provided that the following conditions
  7  * are met:
  8  * 1. Redistributions of source code must retain the above copyright
  9  *    notice, this list of conditions and the following disclaimer.
 10  * 2. Redistributions in binary form must reproduce the above copyright
 11  *    notice, this list of conditions and the following disclaimer in the
 12  *    documentation and/or other materials provided with the distribution.
 13  * 3. The name of the author may not be used to endorse or promote products
 14  *    derived from this software without specific prior written permission.
 15  *
 16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS\'\' AND ANY EXPRESS OR
 17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26  */
 27 #ifndef EVENT2_LISTENER_H_INCLUDED_
 28 #define EVENT2_LISTENER_H_INCLUDED_
 29 
 30 #include <event2/visibility.h>
 31 
 32 #ifdef __cplusplus
 33 extern "C" {
 34 #endif
 35 
 36 #include <event2/event.h>
 37 
 38 struct sockaddr;
 39 struct evconnlistener;
 40 
 41 /**
 42    A callback that we invoke when a listener has a new connection.
 43 
 44    @param listener The evconnlistener
 45    @param fd The new file descriptor
 46    @param addr The source address of the connection
 47    @param socklen The length of addr
 48    @param user_arg the pointer passed to evconnlistener_new()
 49  */
 50 typedef void (*evconnlistener_cb)(struct evconnlistener *, evutil_socket_t, struct sockaddr *, int socklen, void *);
 51 
 52 /**
 53    A callback that we invoke when a listener encounters a non-retriable error.
 54 
 55    @param listener The evconnlistener
 56    @param user_arg the pointer passed to evconnlistener_new()
 57  */
 58 typedef void (*evconnlistener_errorcb)(struct evconnlistener *, void *);
 59 
 60 /** Flag: Indicates that we should not make incoming sockets nonblocking
 61  * before passing them to the callback. */
 62 #define LEV_OPT_LEAVE_SOCKETS_BLOCKING    (1u<<0)
 63 /** Flag: Indicates that freeing the listener should close the underlying
 64  * socket. */
 65 #define LEV_OPT_CLOSE_ON_FREE        (1u<<1)
 66 /** Flag: Indicates that we should set the close-on-exec flag, if possible */
 67 #define LEV_OPT_CLOSE_ON_EXEC        (1u<<2)
 68 /** Flag: Indicates that we should disable the timeout (if any) between when
 69  * this socket is closed and when we can listen again on the same port. */
 70 #define LEV_OPT_REUSEABLE        (1u<<3)
 71 /** Flag: Indicates that the listener should be locked so it\'s safe to use
 72  * from multiple threadcs at once. */
 73 #define LEV_OPT_THREADSAFE        (1u<<4)
 74 /** Flag: Indicates that the listener should be created in disabled
 75  * state. Use evconnlistener_enable() to enable it later. */
 76 #define LEV_OPT_DISABLED        (1u<<5)
 77 /** Flag: Indicates that the listener should defer accept() until data is
 78  * available, if possible.  Ignored on platforms that do not support this.
 79  *
 80  * This option can help performance for protocols where the client transmits
 81  * immediately after connecting.  Do not use this option if your protocol
 82  * _doesn\'t_ start out with the client transmitting data, since in that case
 83  * this option will sometimes cause the kernel to never tell you about the
 84  * connection.
 85  *
 86  * This option is only supported by evconnlistener_new_bind(): it can\'t
 87  * work with evconnlistener_new_fd(), since the listener needs to be told
 88  * to use the option before it is actually bound.
 89  */
 90 #define LEV_OPT_DEFERRED_ACCEPT        (1u<<6)
 91 /** Flag: Indicates that we ask to allow multiple servers (processes or
 92  * threads) to bind to the same port if they each set the option. 
 93  * 
 94  * SO_REUSEPORT is what most people would expect SO_REUSEADDR to be, however
 95  * SO_REUSEPORT does not imply SO_REUSEADDR.
 96  *
 97  * This is only available on Linux and kernel 3.9+
 98  */
 99 #define LEV_OPT_REUSEABLE_PORT        (1u<<7)
100 
101 /**
102    Allocate a new evconnlistener object to listen for incoming TCP connections
103    on a given file descriptor.
104 
105    @param base The event base to associate the listener with.
106    @param cb A callback to be invoked when a new connection arrives.  If the
107       callback is NULL, the listener will be treated as disabled until the
108       callback is set.
109    @param ptr A user-supplied pointer to give to the callback.
110    @param flags Any number of LEV_OPT_* flags
111    @param backlog Passed to the listen() call to determine the length of the
112       acceptable connection backlog.  Set to -1 for a reasonable default.
113       Set to 0 if the socket is already listening.
114    @param fd The file descriptor to listen on.  It must be a nonblocking
115       file descriptor, and it should already be bound to an appropriate
116       port and address.
117 */
118 EVENT2_EXPORT_SYMBOL
119 struct evconnlistener *evconnlistener_new(struct event_base *base,
120     evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
121     evutil_socket_t fd);
122 /**
123    Allocate a new evconnlistener object to listen for incoming TCP connections
124    on a given address.
125 
126    @param base The event base to associate the listener with.
127    @param cb A callback to be invoked when a new connection arrives. If the
128       callback is NULL, the listener will be treated as disabled until the
129       callback is set.
130    @param ptr A user-supplied pointer to give to the callback.
131    @param flags Any number of LEV_OPT_* flags
132    @param backlog Passed to the listen() call to determine the length of the
133       acceptable connection backlog.  Set to -1 for a reasonable default.
134    @param addr The address to listen for connections on.
135    @param socklen The length of the address.
136  */
137 EVENT2_EXPORT_SYMBOL
138 struct evconnlistener *evconnlistener_new_bind(struct event_base *base,
139     evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
140     const struct sockaddr *sa, int socklen);
141 /**
142    Disable and deallocate an evconnlistener.
143  */
144 EVENT2_EXPORT_SYMBOL
145 void evconnlistener_free(struct evconnlistener *lev);
146 /**
147    Re-enable an evconnlistener that has been disabled.
148  */
149 EVENT2_EXPORT_SYMBOL
150 int evconnlistener_enable(struct evconnlistener *lev);
151 /**
152    Stop listening for connections on an evconnlistener.
153  */
154 EVENT2_EXPORT_SYMBOL
155 int evconnlistener_disable(struct evconnlistener *lev);
156 
157 /** Return an evconnlistener\'s associated event_base. */
158 EVENT2_EXPORT_SYMBOL
159 struct event_base *evconnlistener_get_base(struct evconnlistener *lev);
160 
161 /** Return the socket that an evconnlistner is listening on. */
162 EVENT2_EXPORT_SYMBOL
163 evutil_socket_t evconnlistener_get_fd(struct evconnlistener *lev);
164 
165 /** Change the callback on the listener to cb and its user_data to arg.
166  */
167 EVENT2_EXPORT_SYMBOL
168 void evconnlistener_set_cb(struct evconnlistener *lev,
169     evconnlistener_cb cb, void *arg);
170 
171 /** Set an evconnlistener\'s error callback. */
172 EVENT2_EXPORT_SYMBOL
173 void evconnlistener_set_error_cb(struct evconnlistener *lev,
174     evconnlistener_errorcb errorcb);
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif
View Code

定义的函数有以下几个:

evconnlistener_cb:函数指针类型,当有一个新连接到来时被回调。

evconnlistener_errorcb:函数指针类型,当有一个错误发生时被回调。

evconnlistener_new:基于一个给定的socket套接字描述符,返回一个struct evconnlistener对象,(要求描述符已经调用bind)

evconnlistener_new_bind:基于一个给定的sockaddr地址,返回一个struct evconnlistener对象;该函数会调用上面的函数。

evconnlistener_free:释放一个struct evconnlistener对象。

evconnlistener_enable:激活一个struct evconnlistener对象。

evconnlistener_disable:关闭一个struct evconnlistener对象。

evconnlistener_get_base:返回与该listener关联的event_base对象。

evconnlistener_get_fd:返回与该listener关联的socket描述符。

evconnlistener_set_cb:设置新连接到来时的回调函数。

evconnlistener_set_error_cb:设置发生错误时的回调函数。

数据结构:

 1 struct evconnlistener {
 2     const struct evconnlistener_ops *ops;
 3     void *lock;
 4     evconnlistener_cb cb;
 5     evconnlistener_errorcb errorcb;
 6     void *user_data;
 7     unsigned flags;
 8     short refcnt;
 9     int accept4_flags;
10     unsigned enabled : 1;
11 };
12 
13 struct evconnlistener_event {
14     struct evconnlistener base;
15     struct event listener;
16 };

函数的实现:

我认为其中最需要花时间理解的函数只有两个:evconnlistener_new、listener_read_cb。

 1 struct evconnlistener *
 2 evconnlistener_new(struct event_base *base,
 3     evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
 4     evutil_socket_t fd)
 5 {
 6     struct evconnlistener_event *lev;
 7 
 8 #ifdef _WIN32
 9     if (base && event_base_get_iocp_(base)) {
10         const struct win32_extension_fns *ext =
11             event_get_win32_extension_fns_();
12         if (ext->AcceptEx && ext->GetAcceptExSockaddrs)
13             return evconnlistener_new_async(base, cb, ptr, flags,
14                 backlog, fd);
15     }
16 #endif
17 
18     if (backlog > 0) {
19         if (listen(fd, backlog) < 0)
20             return NULL;
21     } else if (backlog < 0) {
22         if (listen(fd, 128) < 0)
23             return NULL;
24     }
25 
26     lev = mm_calloc(1, sizeof(struct evconnlistener_event));
27     if (!lev)
28         return NULL;
29 
30     lev->base.ops = &evconnlistener_event_ops;
31     lev->base.cb = cb;
32     lev->base.user_data = ptr;
33     lev->base.flags = flags;
34     lev->base.refcnt = 1;
35 
36     lev->base.accept4_flags = 0;
37     if (!(flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
38         lev->base.accept4_flags |= EVUTIL_SOCK_NONBLOCK;
39     if (flags & LEV_OPT_CLOSE_ON_EXEC)
40         lev->base.accept4_flags |= EVUTIL_SOCK_CLOEXEC;
41 
42     if (flags & LEV_OPT_THREADSAFE) {
43         EVTHREAD_ALLOC_LOCK(lev->base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
44     }
45 
46     event_assign(&lev->listener, base, fd, EV_READ|EV_PERSIST,
47         listener_read_cb, lev);
48 
49     if (!(flags & LEV_OPT_DISABLED))
50         evconnlistener_enable(&lev->base);
51 
52     return &lev->base;
53 }

总结起来这个函数就做个两件事:分配一个evconnlistener_event对象、初始化base成员和listener成员并激活该事件(evconnlistener_enable)。

这里需要注意到,初始化listener时传入的回调函数是listener_read_cb,而这个函数是在listener中定义的,而我们调用evconnlistener_new时传入的回调函数只是被赋值给了base.cb了,那么这个函数是怎么在新连接到来时被调用的呢?秘密就在listener_read_cb函数中,下面来看一下它的实现:

 1 static void
 2 listener_read_cb(evutil_socket_t fd, short what, void *p)
 3 {
 4     struct evconnlistener *lev = p;
 5     int err;
 6     evconnlistener_cb cb;
 7     evconnlistener_errorcb errorcb;
 8     void *user_data;
 9     LOCK(lev);
10     while (1) {
11         struct sockaddr_storage ss;
12         ev_socklen_t socklen = sizeof(ss);
13         evutil_socket_t new_fd = evutil_accept4_(fd, (struct sockaddr*)&ss, &socklen, lev->accept4_flags);
14         if (new_fd < 0)
15             break;
16         if (socklen == 0) {
17             /* This can happen with some older linux kernels in
18              * response to nmap. */
19             evutil_closesocket(new_fd);
20             continue;
21         }
22 
23         if (lev->cb == NULL) {
24             evutil_closesocket(new_fd);
25             UNLOCK(lev);
26             return;
27         }
28         ++lev->refcnt;
29         cb = lev->cb;
30         user_data = lev->user_data;
31         UNLOCK(lev);
32         cb(lev, new_fd, (struct sockaddr*)&ss, (int)socklen,
33             user_data);
34         LOCK(lev);
35         if (lev->refcnt == 1) {
36             int freed = listener_decref_and_unlock(lev);
37             EVUTIL_ASSERT(freed);
38 
39             evutil_closesocket(new_fd);
40             return;
41         }
42         --lev->refcnt;
43     }
44     err = evutil_socket_geterror(fd);
45     if (EVUTIL_ERR_ACCEPT_RETRIABLE(err)) {
46         UNLOCK(lev);
47         return;
48     }
49     if (lev->errorcb != NULL) {
50         ++lev->refcnt;
51         errorcb = lev->errorcb;
52         user_data = lev->user_data;
53         UNLOCK(lev);
54         errorcb(lev, user_data);
55         LOCK(lev);
56         listener_decref_and_unlock(lev);
57     } else {
58         event_sock_warn(fd, "Error from accept() call");
59         UNLOCK(lev);
60     }
61 }

首先可以从L29-L32看到,我们在调用evconnlistener_new时传入的回调函数被调用了,这里就解开了上面我说的那个秘密,这里其实是libevent对于回调加了一层处理,通过listener_read_cb间接回调我们设置的回调函数,这样可以在listener_read_cb中处理一些异常情况,例如:当异常发生时回调errorcb。

到这里listener就分析完了,上面说需要理解的就这两个函数是因为其他的函数都是功能函数,实现不复杂,而evconnlistener_new_bind函数其实是调用了evconnlistener_new函数的。

以上是关于libevent源码分析:listener的主要内容,如果未能解决你的问题,请参考以下文章

libevent源码分析--(转)

libevent源码分析:epoll后端实现

Libevent源码分析--- 时间管理

Libevent源码分析--- 时间管理

Libevent源码分析--- libevent事件机制

Libevent源码分析--- libevent事件机制