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.2 KiB

  1. /*
  2. * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
  3. * All rights reserved.
  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. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #ifdef _WIN32
  31. # define WIN32_LEAN_AND_MEAN
  32. # include <windows.h>
  33. # undef WIN32_LEAN_AND_MEAN
  34. typedef unsigned char u_char;
  35. typedef unsigned short u_short;
  36. #else
  37. # include <sys/types.h>
  38. # include <sys/time.h>
  39. # include <inttypes.h>
  40. #endif
  41. #include <stdarg.h>
  42. /* Fix so that ppl dont have to run with <sys/queue.h> */
  43. #ifndef TAILQ_ENTRY
  44. #define _EVENT_DEFINED_TQENTRY
  45. #define TAILQ_ENTRY(type) \
  46. struct { \
  47. struct type *tqe_next; /* next element */ \
  48. struct type **tqe_prev; /* address of previous next element */ \
  49. }
  50. #endif /* !TAILQ_ENTRY */
  51. #ifndef RB_ENTRY
  52. #define _EVENT_DEFINED_RBENTRY
  53. #define RB_ENTRY(type) \
  54. struct { \
  55. struct type *rbe_left; /* left element */ \
  56. struct type *rbe_right; /* right element */ \
  57. struct type *rbe_parent; /* parent element */ \
  58. int rbe_color; /* node color */ \
  59. }
  60. #endif /* !RB_ENTRY */
  61. /*
  62. * Key-Value pairs. Can be used for HTTP headers but also for
  63. * query argument parsing.
  64. */
  65. struct evkeyval {
  66. TAILQ_ENTRY(evkeyval) next;
  67. char *key;
  68. char *value;
  69. };
  70. #ifdef _EVENT_DEFINED_TQENTRY
  71. #undef TAILQ_ENTRY
  72. struct event_list;
  73. struct evkeyvalq;
  74. #undef _EVENT_DEFINED_TQENTRY
  75. #else
  76. TAILQ_HEAD (event_list, event);
  77. TAILQ_HEAD (evkeyvalq, evkeyval);
  78. #endif /* _EVENT_DEFINED_TQENTRY */
  79. #ifdef _EVENT_DEFINED_RBENTRY
  80. #undef RB_ENTRY
  81. #undef _EVENT_DEFINED_RBENTRY
  82. #endif /* _EVENT_DEFINED_RBENTRY */
  83. struct eventop {
  84. char *name;
  85. void *(*init)(struct event_base *);
  86. int (*add)(void *, struct event *);
  87. int (*del)(void *, struct event *);
  88. int (*recalc)(struct event_base *, void *, int);
  89. int (*dispatch)(struct event_base *, void *, struct timeval *);
  90. void (*dealloc)(struct event_base *, void *);
  91. };
  92. /* These functions deal with buffering input and output */
  93. struct evbuffer {
  94. u_char *buffer;
  95. u_char *orig_buffer;
  96. size_t misalign;
  97. size_t totallen;
  98. size_t off;
  99. void (*cb)(struct evbuffer *, size_t, size_t, void *);
  100. void *cbarg;
  101. };
  102. /* Just for error reporting - use other constants otherwise */
  103. #define EVBUFFER_READ 0x01
  104. #define EVBUFFER_WRITE 0x02
  105. #define EVBUFFER_EOF 0x10
  106. #define EVBUFFER_ERROR 0x20
  107. #define EVBUFFER_TIMEOUT 0x40
  108. struct bufferevent;
  109. typedef void (*evbuffercb)(struct bufferevent *, void *);
  110. typedef void (*everrorcb)(struct bufferevent *, short what, void *);
  111. struct event_watermark {
  112. size_t low;
  113. size_t high;
  114. };
  115. struct bufferevent {
  116. struct event ev_read;
  117. struct event ev_write;
  118. struct evbuffer *input;
  119. struct evbuffer *output;
  120. struct event_watermark wm_read;
  121. struct event_watermark wm_write;
  122. evbuffercb readcb;
  123. evbuffercb writecb;
  124. everrorcb errorcb;
  125. void *cbarg;
  126. int timeout_read; /* in seconds */
  127. int timeout_write; /* in seconds */
  128. short enabled; /* events that are currently enabled */
  129. };
  130. struct bufferevent *bufferevent_new(int fd,
  131. evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
  132. int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
  133. int bufferevent_priority_set(struct bufferevent *bufev, int pri);
  134. void bufferevent_free(struct bufferevent *bufev);
  135. int bufferevent_write(struct bufferevent *bufev, void *data, size_t size);
  136. int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
  137. size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
  138. int bufferevent_enable(struct bufferevent *bufev, short event);
  139. int bufferevent_disable(struct bufferevent *bufev, short event);
  140. void bufferevent_settimeout(struct bufferevent *bufev,
  141. int timeout_read, int timeout_write);
  142. #define EVBUFFER_LENGTH(x) (x)->off
  143. #define EVBUFFER_DATA(x) (x)->buffer
  144. #define EVBUFFER_INPUT(x) (x)->input
  145. #define EVBUFFER_OUTPUT(x) (x)->output
  146. struct evbuffer *evbuffer_new(void);
  147. void evbuffer_free(struct evbuffer *);
  148. int evbuffer_expand(struct evbuffer *, size_t);
  149. int evbuffer_add(struct evbuffer *, const void *, size_t);
  150. int evbuffer_remove(struct evbuffer *, void *, size_t);
  151. char *evbuffer_readline(struct evbuffer *);
  152. int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
  153. int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...);
  154. int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
  155. void evbuffer_drain(struct evbuffer *, size_t);
  156. int evbuffer_write(struct evbuffer *, int);
  157. int evbuffer_read(struct evbuffer *, int, int);
  158. u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
  159. void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
  160. /*
  161. * Marshaling tagged data - We assume that all tags are inserted in their
  162. * numeric order - so that unknown tags will always be higher than the
  163. * known ones - and we can just ignore the end of an event buffer.
  164. */
  165. void evtag_init(void);
  166. void evtag_marshal(struct evbuffer *evbuf, uint8_t tag, const void *data,
  167. uint32_t len);
  168. void encode_int(struct evbuffer *evbuf, uint32_t number);
  169. void evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer);
  170. void evtag_marshal_string(struct evbuffer *buf, uint8_t tag,
  171. const char *string);
  172. void evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag,
  173. struct timeval *tv);
  174. void evtag_test(void);
  175. int evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst);
  176. int evtag_peek(struct evbuffer *evbuf, uint8_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, uint8_t need_tag,
  181. uint32_t *pinteger);
  182. int evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data,
  183. size_t len);
  184. int evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag,
  185. char **pstring);
  186. int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag,
  187. struct timeval *ptv);
  188. #ifdef __cplusplus
  189. }
  190. #endif