Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

227 lines
7.3 KiB

  1. /*
  2. * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
  3. * Copyright (c) 2008 Marc Alexander Lehmann <libev@schmorp.de>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #ifdef _WIN32
  32. # define WIN32_LEAN_AND_MEAN
  33. # include <windows.h>
  34. # undef WIN32_LEAN_AND_MEAN
  35. typedef unsigned char u_char;
  36. typedef unsigned short u_short;
  37. #else
  38. # include <sys/types.h>
  39. # include <sys/time.h>
  40. # include <inttypes.h>
  41. #endif
  42. #include <stdarg.h>
  43. /* Fix so that ppl dont have to run with <sys/queue.h> */
  44. #ifndef TAILQ_ENTRY
  45. #define _EVENT_DEFINED_TQENTRY
  46. #define TAILQ_ENTRY(type) \
  47. struct { \
  48. struct type *tqe_next; /* next element */ \
  49. struct type **tqe_prev; /* address of previous next element */ \
  50. }
  51. #endif /* !TAILQ_ENTRY */
  52. #ifndef RB_ENTRY
  53. #define _EVENT_DEFINED_RBENTRY
  54. #define RB_ENTRY(type) \
  55. struct { \
  56. struct type *rbe_left; /* left element */ \
  57. struct type *rbe_right; /* right element */ \
  58. struct type *rbe_parent; /* parent element */ \
  59. int rbe_color; /* node color */ \
  60. }
  61. #endif /* !RB_ENTRY */
  62. /*
  63. * Key-Value pairs. Can be used for HTTP headers but also for
  64. * query argument parsing.
  65. */
  66. struct evkeyval {
  67. TAILQ_ENTRY(evkeyval) next;
  68. char *key;
  69. char *value;
  70. };
  71. #ifdef _EVENT_DEFINED_TQENTRY
  72. #undef TAILQ_ENTRY
  73. struct event_list;
  74. struct evkeyvalq;
  75. #undef _EVENT_DEFINED_TQENTRY
  76. #else
  77. TAILQ_HEAD (event_list, event);
  78. TAILQ_HEAD (evkeyvalq, evkeyval);
  79. #endif /* _EVENT_DEFINED_TQENTRY */
  80. #ifdef _EVENT_DEFINED_RBENTRY
  81. #undef RB_ENTRY
  82. #undef _EVENT_DEFINED_RBENTRY
  83. #endif /* _EVENT_DEFINED_RBENTRY */
  84. struct eventop {
  85. char *name;
  86. void *(*init)(struct event_base *);
  87. int (*add)(void *, struct event *);
  88. int (*del)(void *, struct event *);
  89. int (*recalc)(struct event_base *, void *, int);
  90. int (*dispatch)(struct event_base *, void *, struct timeval *);
  91. void (*dealloc)(struct event_base *, void *);
  92. };
  93. /* These functions deal with buffering input and output */
  94. struct evbuffer {
  95. u_char *buffer;
  96. u_char *orig_buffer;
  97. size_t misalign;
  98. size_t totallen;
  99. size_t off;
  100. void (*cb)(struct evbuffer *, size_t, size_t, void *);
  101. void *cbarg;
  102. };
  103. /* Just for error reporting - use other constants otherwise */
  104. #define EVBUFFER_READ 0x01
  105. #define EVBUFFER_WRITE 0x02
  106. #define EVBUFFER_EOF 0x10
  107. #define EVBUFFER_ERROR 0x20
  108. #define EVBUFFER_TIMEOUT 0x40
  109. struct bufferevent;
  110. typedef void (*evbuffercb)(struct bufferevent *, void *);
  111. typedef void (*everrorcb)(struct bufferevent *, short what, void *);
  112. struct event_watermark {
  113. size_t low;
  114. size_t high;
  115. };
  116. struct bufferevent {
  117. struct event ev_read;
  118. struct event ev_write;
  119. struct evbuffer *input;
  120. struct evbuffer *output;
  121. struct event_watermark wm_read;
  122. struct event_watermark wm_write;
  123. evbuffercb readcb;
  124. evbuffercb writecb;
  125. everrorcb errorcb;
  126. void *cbarg;
  127. int timeout_read; /* in seconds */
  128. int timeout_write; /* in seconds */
  129. short enabled; /* events that are currently enabled */
  130. };
  131. struct bufferevent *bufferevent_new(int fd,
  132. evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
  133. int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
  134. int bufferevent_priority_set(struct bufferevent *bufev, int pri);
  135. void bufferevent_free(struct bufferevent *bufev);
  136. int bufferevent_write(struct bufferevent *bufev, const void *data, size_t size);
  137. int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
  138. size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
  139. int bufferevent_enable(struct bufferevent *bufev, short event);
  140. int bufferevent_disable(struct bufferevent *bufev, short event);
  141. void bufferevent_settimeout(struct bufferevent *bufev,
  142. int timeout_read, int timeout_write);
  143. #define EVBUFFER_LENGTH(x) (x)->off
  144. #define EVBUFFER_DATA(x) (x)->buffer
  145. #define EVBUFFER_INPUT(x) (x)->input
  146. #define EVBUFFER_OUTPUT(x) (x)->output
  147. struct evbuffer *evbuffer_new(void);
  148. void evbuffer_free(struct evbuffer *);
  149. int evbuffer_expand(struct evbuffer *, size_t);
  150. int evbuffer_add(struct evbuffer *, const void *, size_t);
  151. int evbuffer_remove(struct evbuffer *, void *, size_t);
  152. char *evbuffer_readline(struct evbuffer *);
  153. int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
  154. int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...);
  155. int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
  156. void evbuffer_drain(struct evbuffer *, size_t);
  157. int evbuffer_write(struct evbuffer *, int);
  158. int evbuffer_read(struct evbuffer *, int, int);
  159. u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
  160. void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
  161. /*
  162. * Marshaling tagged data - We assume that all tags are inserted in their
  163. * numeric order - so that unknown tags will always be higher than the
  164. * known ones - and we can just ignore the end of an event buffer.
  165. */
  166. void evtag_init(void);
  167. void evtag_marshal(struct evbuffer *evbuf, uint32_t tag, const void *data,
  168. uint32_t len);
  169. void encode_int(struct evbuffer *evbuf, uint32_t number);
  170. void evtag_marshal_int(struct evbuffer *evbuf, uint32_t tag, uint32_t integer);
  171. void evtag_marshal_string(struct evbuffer *buf, uint32_t tag,
  172. const char *string);
  173. void evtag_marshal_timeval(struct evbuffer *evbuf, uint32_t tag,
  174. struct timeval *tv);
  175. int evtag_unmarshal(struct evbuffer *src, uint32_t *ptag, struct evbuffer *dst);
  176. int evtag_peek(struct evbuffer *evbuf, uint32_t *ptag);
  177. int evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength);
  178. int evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength);
  179. int evtag_consume(struct evbuffer *evbuf);
  180. int evtag_unmarshal_int(struct evbuffer *evbuf, uint32_t need_tag,
  181. uint32_t *pinteger);
  182. int evtag_unmarshal_fixed(struct evbuffer *src, uint32_t need_tag, void *data,
  183. size_t len);
  184. int evtag_unmarshal_string(struct evbuffer *evbuf, uint32_t need_tag,
  185. char **pstring);
  186. int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint32_t need_tag,
  187. struct timeval *ptv);
  188. #ifdef __cplusplus
  189. }
  190. #endif