src/server/daemon/event_linux.c

Mon, 06 Mar 2017 17:32:26 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Mar 2017 17:32:26 +0100
changeset 179
ef6827505bd2
parent 162
b169992137a8
child 193
aa8393527b1e
permissions
-rw-r--r--

merge srvctrl into default branch

41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <errno.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <sys/epoll.h>
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../util/systhr.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../util/atomic.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "event.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include "event_linux.h"
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 event_handler_t* evhandler_create(int numthreads) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 event_handler_t *ev = malloc(sizeof(event_handler_t));
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 if(ev == NULL) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 return NULL;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 ev->ep = calloc(numthreads, sizeof(int));
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 if(ev->ep == NULL) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 free(ev);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 return NULL;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 ev->nep = numthreads;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 ev->lep = 0;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 /* create ports event threads */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 for(int i=0;i<numthreads;i++) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 /* create port */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 ev->ep[i] = epoll_create(64);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 if(ev->ep[i] == 0) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 free(ev->ep);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 free(ev);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 return NULL;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 /*
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 * start a new handler thread
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 * the thread needs the event port and a pointer to the event handler
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 ev_thr_conf_t *conf = malloc(sizeof(ev_thr_conf_t));
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if(conf == NULL) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 free(ev->ep);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 free(ev);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 return NULL;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 conf->handler = ev;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 conf->ep = ev->ep[i];
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
79 systhread_start(0, 0, (thrstartfunc)ev_handle_events, conf);
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 /* TODO: error handling */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 return ev;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 void ev_handle_events(ev_thr_conf_t *conf) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 event_handler_t *ev = conf->handler;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 int ep = conf->ep;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 free(conf);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 //port_event_t events[16];
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 struct epoll_event events[16];
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 for(;;) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 /* wait for events */
48
37a512d7b8f6 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 46
diff changeset
98 int ret = epoll_wait(ep, events, 16, 100000);
126
631aaa01b2b5 fixes chunked transfer encoding
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
99 if(ret == -1 && errno != EINTR) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
100 log_ereport(LOG_FAILURE, "epoll_wait failed: %s", strerror(errno));
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 continue;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 for(int i=0;i<ret;i++) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 event_t *event = events[i].data.ptr;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 if(event->fn) {
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
107 int saved_ev = event->poll;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 if(!event->fn(ev, event)) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 // event fn returned 0 -> remove event from epoll
43
8ac56edb4e94 fixed solaris build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 41
diff changeset
110 if(epoll_ctl(ep, EPOLL_CTL_DEL, event->object, NULL)) {
162
b169992137a8 improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 161
diff changeset
111 event->error = 1;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
112 log_ereport(
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
113 LOG_FAILURE,
162
b169992137a8 improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 161
diff changeset
114 "epoll_ctl failed: fd: %d error: %s",
b169992137a8 improves cgi error handling and allows requests with empty headers
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 161
diff changeset
115 event->object,
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
116 strerror(errno));
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
46
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
118
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
119 // if set, execute event->finish
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
120 if(event->finish) {
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
121 event->finish(ev, event);
636e05eb48f6 cleaning up resources after requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
122 }
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
123 } else {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
124 if(saved_ev != event->poll) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
125 // event type changed
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
126 struct epoll_event epev;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
127 epev.events = EPOLLET;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
128 epev.data.ptr = event;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
129
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
130 // adjust epoll events
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
131 if((event->poll & EVENT_POLLIN) == EVENT_POLLIN) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
132 epev.events |= EPOLLIN;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
133 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
134 if((event->poll & EVENT_POLLOUT) == EVENT_POLLOUT) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
135 epev.events |= EPOLLOUT;
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
136 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
137
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
138 if(epoll_ctl(ep, EPOLL_CTL_MOD, event->object, NULL)) {
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
139 log_ereport(
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
140 LOG_FAILURE,
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
141 "epoll_wait failed: %s",
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
142 strerror(errno));
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
143 }
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
144 }
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 /* returns a event handler port */
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 int ev_get_port(event_handler_t *h) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 int nps = h->nep;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 if(nps == 1) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 return h->ep[0];
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 int cp = h->lep % nps;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 ws_atomic_inc32(&h->lep);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 return h->ep[cp];
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 int ev_pollin(event_handler_t *h, int fd, event_t *event) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 event->object = (intptr_t)fd;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
166 event->poll = EVENT_POLLIN;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 struct epoll_event epev;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
168 epev.events = EPOLLIN | EPOLLET; // input event, edge triggered
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 epev.data.ptr = event;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 return epoll_ctl(ev_get_port(h), EPOLL_CTL_ADD, fd, &epev);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 int ev_pollout(event_handler_t *h, int fd, event_t *event) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 event->object = (intptr_t)fd;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
175 event->poll = EVENT_POLLOUT;
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 struct epoll_event epev;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
177 epev.events = EPOLLOUT | EPOLLET; // input event, edge triggered
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 epev.data.ptr = event;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 return epoll_ctl(ev_get_port(h), EPOLL_CTL_ADD, fd, &epev);
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 }
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 int evt_send(event_handler_t *h, event_t *event) {
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 event->object = 0;
133
87b405d61f64 improves event handler and ssl error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 126
diff changeset
184 // TODO: implement using threadpool or eventfd
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 fprintf(stderr, "Warning: evt_send not implemented\n");
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 return 0;
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }

mercurial