-
Notifications
You must be signed in to change notification settings - Fork 8
/
coroutines_sockets.c
305 lines (234 loc) · 6.78 KB
/
coroutines_sockets.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* coroutines_sockets.c - simple coroutines for SimpleMail.
* Copyright (C) 2015 Sebastian Bauer
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file coroutines_sockets.c
*/
#include "coroutines_sockets.h"
#include <stdlib.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#ifndef __MORPHOS__
#include <sys/select.h>
#else
#include <proto/exec.h>
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#undef getpid
// libcrypto stubs
pid_t getpid()
{
return (pid_t) FindTask(NULL);
}
int sigaction(int a, const struct sigaction *b, struct sigaction *c)
{
return 0;
}
int tcsetattr(int a, int b, const struct termios *c)
{
return 0;
}
int tcgetattr(int a, struct termios *b)
{
return 0;
}
#endif
#include "coroutines_internal.h"
/*****************************************************************************/
#define MAX(a,b) ((a)>(b)?(a):(b))
/*****************************************************************************/
struct coroutine_scheduler_fd_data
{
/** Highest number of descriptors to wait for */
int nfds;
/** Set of read fds to wait for */
fd_set readfds;
/** Set of write fds to wait for */
fd_set writefds;
};
/**
* Prepare the set of fds for the given scheduler.
*
* @param scheduler
*/
static void coroutine_schedule_prepare_fds(coroutine_scheduler_t scheduler, struct coroutine_scheduler_fd_data *data)
{
coroutine_t cor, cor_next;
fd_set *readfds = &data->readfds;
fd_set *writefds = &data->writefds;
FD_ZERO(readfds);
FD_ZERO(writefds);
data->nfds = -1;
cor = coroutines_list_first(&scheduler->waiting_coroutines_list);
for (;cor;cor = cor_next)
{
cor_next = coroutines_next(cor);
if (cor->context->socket_fd >= 0)
{
data->nfds = MAX(cor->context->socket_fd, data->nfds);
if (cor->context->write_mode)
{
FD_SET(cor->context->socket_fd, writefds);
} else
{
FD_SET(cor->context->socket_fd, readfds);
}
}
}
}
/**
* Standard wait for event function using select().
*
* @param poll
* @param udata
*/
static int coroutine_wait_for_fd_event(coroutine_scheduler_t sched, int poll, void *udata)
{
struct coroutine_scheduler_fd_data *data = (struct coroutine_scheduler_fd_data *)udata;
struct timeval zero_timeout = {0};
coroutine_schedule_prepare_fds(sched, data);
if (data->nfds >= 0)
{
select(data->nfds+1, &data->readfds, &data->writefds, NULL, poll?&zero_timeout:NULL);
return 1;
}
return 0;
}
/*****************************************************************************/
int coroutine_is_fd_now_ready(coroutine_scheduler_t scheduler, coroutine_t cor)
{
struct coroutine_scheduler_fd_data *data = (struct coroutine_scheduler_fd_data *)scheduler->wait_for_event_udata;
if (cor->context->write_mode)
{
if (FD_ISSET(cor->context->socket_fd, &data->writefds))
return 1;
} else
{
if (FD_ISSET(cor->context->socket_fd, &data->readfds))
return 1;
}
return 0;
}
/*****************************************************************************/
void coroutine_await_socket(struct coroutine_basic_context *context, int socket_fd, int write)
{
context->socket_fd = socket_fd;
context->write_mode = write;
context->is_now_ready = coroutine_is_fd_now_ready;
}
/*****************************************************************************/
coroutine_scheduler_t coroutine_scheduler_new(void)
{
struct coroutine_scheduler_fd_data *data;
coroutine_scheduler_t sched;
if (!(data = (struct coroutine_scheduler_fd_data *)malloc(sizeof(*data))))
return NULL;
if (!(sched = coroutine_scheduler_new_custom(coroutine_wait_for_fd_event, data)))
{
free(data);
return NULL;
}
return sched;
}
/*****************************************************************************/
#ifdef TEST
#include <assert.h>
#include <stdio.h>
struct count_context
{
struct coroutine_basic_context basic_context;
int count;
};
static coroutine_return_t count(struct coroutine_basic_context *arg)
{
struct count_context *c = (struct count_context *)arg;
COROUTINE_BEGIN(c);
for (c->count=0; c->count < 10; c->count++)
{
printf("count = %d\n", c->count);
COROUTINE_YIELD(c);
}
COROUTINE_END(c);
}
struct example_context
{
struct coroutine_basic_context basic_context;
int fd;
struct sockaddr_in local_addr;
socklen_t local_addrlen;
struct sockaddr_in remote_addr;
socklen_t remote_addrlen;
struct count_context count_context;
};
static coroutine_return_t coroutines_test(struct coroutine_basic_context *arg)
{
struct example_context *c = (struct example_context *)arg;
COROUTINE_BEGIN(c);
c->local_addr.sin_family = AF_INET;
c->local_addr.sin_port = htons(1234);
c->local_addr.sin_addr.s_addr = INADDR_ANY;
c->local_addrlen = sizeof(c->local_addr);
c->remote_addrlen = sizeof(c->remote_addr);
c->fd = socket(AF_INET, SOCK_STREAM, 0);
assert(c->fd != 0);
int rc = bind(c->fd, (struct sockaddr*)&c->local_addr, c->local_addrlen);
assert(rc == 0);
rc = listen(c->fd, 50);
assert(rc == 0);
rc = fcntl(c->fd, F_SETFL, O_NONBLOCK);
assert(rc == 0);
COROUTINE_AWAIT_SOCKET(c, c->fd, 0);
printf("back again\n");
rc = accept(c->fd, (struct sockaddr*)&c->remote_addr, &c->remote_addrlen);
printf("%d\n",rc);
if (rc >= 0)
{
close(rc);
}
/* Now invoking the other coroutine */
c->count_context.basic_context.socket_fd = -1;
c->count_context.basic_context.scheduler = c->basic_context.scheduler;
coroutine_t other = coroutine_add(c->basic_context.scheduler, count, &c->count_context.basic_context);
assert(other != NULL);
COROUTINE_AWAIT_OTHER(c, other);
printf("now ending main coroutine\n");
COROUTINE_END(c);
}
int main(int argc, char **argv)
{
coroutine_scheduler_t sched;
coroutine_t example;
struct example_context example_context = {0};
if (!(sched = coroutine_scheduler_new()))
{
fprintf(stderr, "Couldn't allocate scheduler!\n");
return 1;
}
example_context.basic_context.socket_fd = -1;
/* TODO: It is not required to set the scheduler as this is part of coroutine_add() */
example_context.basic_context.scheduler = sched;
example = coroutine_add(sched, coroutines_test, &example_context.basic_context);
assert(example != 0);
while (coroutine_schedule(sched));
if (example_context.fd > 0)
close(example_context.fd);
return 0;
}
#endif