1 /* 2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 3 * Copyright (c) 2007-2011 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 module deimos.event2.listener; 28 29 extern (C): 30 nothrow: 31 32 public import deimos.event2.event; 33 import deimos.event2._d_util; 34 35 struct sockaddr; 36 struct evconnlistener; 37 38 /** 39 A callback that we invoke when a listener has a new connection. 40 41 @param listener The evconnlistener 42 @param fd The new file descriptor 43 @param addr The source address of the connection 44 @param socklen The length of addr 45 @param user_arg the pointer passed to evconnlistener_new() 46 */ 47 alias ExternC!(void function(evconnlistener*, evutil_socket_t, sockaddr*, int socklen, void*)) evconnlistener_cb; 48 49 /** 50 A callback that we invoke when a listener encounters a non-retriable error. 51 52 @param listener The evconnlistener 53 @param user_arg the pointer passed to evconnlistener_new() 54 */ 55 alias ExternC!(void function(evconnlistener*, void*)) evconnlistener_errorcb; 56 57 /** Flag: Indicates that we should not make incoming sockets nonblocking 58 * before passing them to the callback. */ 59 enum LEV_OPT_LEAVE_SOCKETS_BLOCKING = (1u<<0); 60 /** Flag: Indicates that freeing the listener should close the underlying 61 * socket. */ 62 enum LEV_OPT_CLOSE_ON_FREE = (1u<<1); 63 /** Flag: Indicates that we should set the close-on-exec flag, if possible */ 64 enum LEV_OPT_CLOSE_ON_EXEC = (1u<<2); 65 /** Flag: Indicates that we should disable the timeout (if any) between when 66 * this socket is closed and when we can listen again on the same port. */ 67 enum LEV_OPT_REUSEABLE = (1u<<3); 68 /** Flag: Indicates that the listener should be locked so it's safe to use 69 * from multiple threadcs at once. */ 70 enum LEV_OPT_THREADSAFE = (1u<<4); 71 72 /** 73 Allocate a new evconnlistener object to listen for incoming TCP connections 74 on a given file descriptor. 75 76 @param base The event base to associate the listener with. 77 @param cb A callback to be invoked when a new connection arrives. If the 78 callback is NULL, the listener will be treated as disabled until the 79 callback is set. 80 @param ptr A user-supplied pointer to give to the callback. 81 @param flags Any number of LEV_OPT_* flags 82 @param backlog Passed to the listen() call to determine the length of the 83 acceptable connection backlog. Set to -1 for a reasonable default. 84 Set to 0 if the socket is already listening. 85 @param fd The file descriptor to listen on. It must be a nonblocking 86 file descriptor, and it should already be bound to an appropriate 87 port and address. 88 */ 89 evconnlistener* evconnlistener_new(event_base* base, 90 evconnlistener_cb cb, void* ptr, uint flags, int backlog, 91 evutil_socket_t fd); 92 /** 93 Allocate a new evconnlistener object to listen for incoming TCP connections 94 on a given address. 95 96 @param base The event base to associate the listener with. 97 @param cb A callback to be invoked when a new connection arrives. If the 98 callback is NULL, the listener will be treated as disabled until the 99 callback is set. 100 @param ptr A user-supplied pointer to give to the callback. 101 @param flags Any number of LEV_OPT_* flags 102 @param backlog Passed to the listen() call to determine the length of the 103 acceptable connection backlog. Set to -1 for a reasonable default. 104 @param addr The address to listen for connections on. 105 @param socklen The length of the address. 106 */ 107 evconnlistener* evconnlistener_new_bind(event_base* base, 108 evconnlistener_cb cb, void* ptr, uint flags, int backlog, 109 const(sockaddr)* sa, int socklen); 110 /** 111 Disable and deallocate an evconnlistener. 112 */ 113 void evconnlistener_free(evconnlistener* lev); 114 /** 115 Re-enable an evconnlistener that has been disabled. 116 */ 117 int evconnlistener_enable(evconnlistener* lev); 118 /** 119 Stop listening for connections on an evconnlistener. 120 */ 121 int evconnlistener_disable(evconnlistener* lev); 122 123 /** Return an evconnlistener's associated event_base. */ 124 event_base* evconnlistener_get_base(evconnlistener* lev); 125 126 /** Return the socket that an evconnlistner is listening on. */ 127 evutil_socket_t evconnlistener_get_fd(evconnlistener* lev); 128 129 /** Change the callback on the listener to cb and its user_data to arg. 130 */ 131 void evconnlistener_set_cb(evconnlistener* lev, 132 evconnlistener_cb cb, void* arg); 133 134 /** Set an evconnlistener's error callback. */ 135 void evconnlistener_set_error_cb(evconnlistener* lev, 136 evconnlistener_errorcb errorcb);