author | Tero Marttila <terom@fixme.fi> |
Fri, 29 Aug 2008 23:31:17 +0300 | |
changeset 48 | 1c67f512779b |
parent 47 | a5c09677ca6f |
permissions | -rw-r--r-- |
47
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
1 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
2 |
// splice |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
3 |
#define _GNU_SOURCE |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
4 |
#include <fcntl.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
5 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
6 |
#include <unistd.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
7 |
#include <stdlib.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
8 |
#include <signal.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
9 |
#include <errno.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
10 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
11 |
#include <event2/util.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
12 |
#include <event2/event.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
13 |
#include <event2/event_compat.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
14 |
#include <event2/event_struct.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
15 |
#include <pcl.h> |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
16 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
17 |
#include "common.h" |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
18 |
#include "config.h" |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
19 |
#include "socket.h" |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
20 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
21 |
// what port to listen on |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
22 |
#define LISTEN_PORT 10559 |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
23 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
24 |
// stack size to use for coroutines |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
25 |
#define CLIENT_STACK_SIZE 128 * 1024 |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
26 |
#define PIPELINE_STACK_SIZE 128 * 1024 |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
27 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
28 |
// this should be equal to the system page size |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
29 |
#define PIPELINE_CHUNKSIZE 4096 |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
30 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
31 |
struct co_connect_info { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
32 |
// what co this is running as |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
33 |
coroutine_t co; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
34 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
35 |
// set to the value of errno in case of an error, zero otherwise |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
36 |
int error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
37 |
}; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
38 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
39 |
static void _co_connect_write (evutil_socket_t fd, short what, void *arg) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
40 |
struct co_connect_info *ctx = arg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
41 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
42 |
// retreive the error code |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
43 |
// XXX: this function was lost in a hg-fuckup |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
44 |
if (socket_connect_error(fd, &ctx->error)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
45 |
ctx->error = -1; // mark it as failed |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
46 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
47 |
// resume the co |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
48 |
co_call(ctx->co); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
49 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
50 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
51 |
// coroutine-connect |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
52 |
int co_connect (struct config_endpoint *endpoint, int sock_type, int *sock) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
53 |
// our state |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
54 |
struct co_connect_info ctx; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
55 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
56 |
// the event for the connect |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
57 |
struct event ev; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
58 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
59 |
// clean it |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
60 |
memset(&ctx, 0, sizeof(ctx)); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
61 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
62 |
// initiate the connect() |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
63 |
if ((*sock = socket_connect_async(endpoint, sock_type)) == -1) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
64 |
goto error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
65 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
66 |
// set up the event |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
67 |
event_set(&ev, *sock, EV_WRITE, &_co_connect_write, &ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
68 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
69 |
if (event_add(&ev, NULL)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
70 |
goto error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
71 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
72 |
// store our co handle in there |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
73 |
ctx.co = co_current(); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
74 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
75 |
// wait for the result |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
76 |
co_resume(); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
77 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
78 |
// return the error code |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
79 |
return ctx.error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
80 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
81 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
82 |
return -1; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
83 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
84 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
85 |
struct co_splice_info { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
86 |
int fd_in, fd_out; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
87 |
size_t len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
88 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
89 |
short wait; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
90 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
91 |
struct event ev_read, ev_write; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
92 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
93 |
int error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
94 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
95 |
coroutine_t co; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
96 |
}; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
97 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
98 |
void _co_splice_ev (int fd, short what, void *arg) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
99 |
struct co_splice_info *ctx = arg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
100 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
101 |
ctx->wait &= ~what; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
102 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
103 |
if (!ctx->wait) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
104 |
// both ends are readable/writeable! |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
105 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
106 |
long ret; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
107 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
108 |
if ((ret = splice(ctx->fd_in, NULL, ctx->fd_out, NULL, ctx->len, SPLICE_F_MOVE | SPLICE_F_NONBLOCK)) == -1) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
109 |
if (errno == EAGAIN) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
110 |
ctx->error = 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
111 |
ctx->len = 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
112 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
113 |
} else { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
114 |
PERROR("splice"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
115 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
116 |
} else { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
117 |
ctx->error = 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
118 |
ctx->len = ret; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
119 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
120 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
121 |
// let the co do it's thing |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
122 |
co_call(ctx->co); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
123 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
124 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
125 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
126 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
127 |
// ok, done succesfully |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
128 |
return; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
129 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
130 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
131 |
// let the co handle this error |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
132 |
ctx->error = errno; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
133 |
co_call(ctx->co); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
134 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
135 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
136 |
// coroutine-splice |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
137 |
int co_splice (int fd_in, int fd_out, size_t *len, int *error) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
138 |
// first, try and splice directly |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
139 |
// XXX: ensure all sockets are O_NONBLOCK! |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
140 |
long ret; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
141 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
142 |
if ((ret = splice(fd_in, NULL, fd_out, NULL, *len, SPLICE_F_MOVE | SPLICE_F_NONBLOCK)) == -1) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
143 |
if (errno == EAGAIN) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
144 |
// wait for in/out to become readable/writeable |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
145 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
146 |
// our state |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
147 |
ZINIT(struct co_splice_info, ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
148 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
149 |
ctx.fd_in = fd_in; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
150 |
ctx.fd_out = fd_out; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
151 |
ctx.len = *len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
152 |
ctx.wait = EV_READ | EV_WRITE; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
153 |
ctx.co = co_current(); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
154 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
155 |
// two events |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
156 |
event_set(&ctx.ev_read, fd_in, EV_READ, &_co_splice_ev, &ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
157 |
event_set(&ctx.ev_write, fd_out, EV_WRITE, &_co_splice_ev, &ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
158 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
159 |
// add both |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
160 |
if ( |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
161 |
event_add(&ctx.ev_read, NULL) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
162 |
|| event_add(&ctx.ev_write, NULL) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
163 |
) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
164 |
ERROR("event_add"); // XXX: cleanup |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
165 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
166 |
// and wait... |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
167 |
co_resume(); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
168 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
169 |
// error |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
170 |
if (ctx.error) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
171 |
*error = ctx.error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
172 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
173 |
return -1; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
174 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
175 |
} else { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
176 |
*len = ctx.len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
177 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
178 |
return 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
179 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
180 |
} else |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
181 |
PERROR("splice"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
182 |
} else { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
183 |
// success! |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
184 |
*len = ret; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
185 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
186 |
return 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
187 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
188 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
189 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
190 |
return -1; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
191 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
192 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
193 |
/* |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
194 |
struct co_pipeline_info { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
195 |
int sock_read, sock_write; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
196 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
197 |
int pipe_read, pipe_write; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
198 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
199 |
/ * |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
200 |
* Two coroutines: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
201 |
* * co_write waits for co_read to indicate that there is enough data in the pipe to send, whereupon it will |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
202 |
* attempt to splice data from pipe_read to sock_write. |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
203 |
* * co_read waits for data to become available on sock_src, splices it into pipe_write, and notifies co_write |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
204 |
* that there is data avilable for sending. |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
205 |
* / |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
206 |
coroutine_t co_read, co_write; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
207 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
208 |
char co_read_stack[PIPELINE_STACK_SIZE], co_write_stack[PIPELINE_STACK_SIZE]; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
209 |
}; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
210 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
211 |
void _co_pipeline_read (void *arg) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
212 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
213 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
214 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
215 |
void _co_pipeline_write (void *arg) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
216 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
217 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
218 |
*/ |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
219 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
220 |
int co_pipeline (int sock_read, int sock_write) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
221 |
// ZINIT(struct co_pipeline_info, ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
222 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
223 |
// create our pipe |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
224 |
int pipefds[2]; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
225 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
226 |
if (pipe(pipefds)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
227 |
PERROR("pipe"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
228 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
229 |
// copy over the fds into ctx |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
230 |
int pipe_read = pipefds[0]; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
231 |
int pipe_write = pipefds[1]; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
232 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
233 |
// start shuffling data around |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
234 |
size_t pipe_size = 0, read_len, write_len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
235 |
int error = 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
236 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
237 |
while (1) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
238 |
read_len = PIPELINE_CHUNKSIZE; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
239 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
240 |
if (co_splice(sock_read, pipe_write, &read_len, &error)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
241 |
PERROR("co_splice(sock_read, pipe_write, %zu)", read_len); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
242 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
243 |
if (!read_len) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
244 |
// EOF |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
245 |
break; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
246 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
247 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
248 |
pipe_size += read_len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
249 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
250 |
if (!pipe_size) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
251 |
continue; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
252 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
253 |
while (pipe_size > 0) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
254 |
write_len = pipe_size; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
255 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
256 |
if (co_splice(pipe_read, sock_write, &write_len, &error)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
257 |
PERROR("co_splice(pipe_read, sock_write, %zu", write_len); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
258 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
259 |
pipe_size -= write_len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
260 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
261 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
262 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
263 |
/* |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
264 |
// create the two coros |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
265 |
if ( |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
266 |
(ctx.co_read = co_create(&_co_pipeline_read, &ctx, ctx.co_read_stack, PIPELINE_STACK_SIZE)) == NULL |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
267 |
|| (ctx.co_write = co_create(&_co_pipeline_write, &ctx, ctx.co_write_stack, PIPELINE_STACK_SIZE)) == NULL |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
268 |
) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
269 |
ERROR("co_create"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
270 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
271 |
// first start off the read one, which then eventually calls the write one |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
272 |
co_call(ctx.co_read); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
273 |
*/ |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
274 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
275 |
// success |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
276 |
return 0; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
277 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
278 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
279 |
/* |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
280 |
if (ctx.co_read) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
281 |
co_delete(ctx.co_read); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
282 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
283 |
if (ctx.co_write) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
284 |
co_delete(ctx.co_write); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
285 |
*/ |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
286 |
return -1; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
287 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
288 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
289 |
/* |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
290 |
* State needed to handle a client |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
291 |
*/ |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
292 |
struct client_info { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
293 |
// the client socket |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
294 |
evutil_socket_t socket; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
295 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
296 |
// the connect target endpoint |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
297 |
struct config_endpoint *connect_target; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
298 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
299 |
// the coroutine that handles it |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
300 |
coroutine_t co; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
301 |
}; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
302 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
303 |
void client_co (void *arg) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
304 |
struct client_info *ctx = arg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
305 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
306 |
// the outbound socket |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
307 |
int conn_sock = -1; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
308 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
309 |
// connect |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
310 |
if (co_connect(ctx->connect_target, SOCK_STREAM, &conn_sock)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
311 |
goto error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
312 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
313 |
// pipe data around \o/ |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
314 |
if (co_pipeline(ctx->socket, conn_sock)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
315 |
goto error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
316 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
317 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
318 |
// close the sockets |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
319 |
if (conn_sock != -1) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
320 |
if (close(conn_sock)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
321 |
PWARNING("close connect_socket"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
322 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
323 |
if (close(ctx->socket)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
324 |
PWARNING("close listen_socket"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
325 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
326 |
// free the ctx |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
327 |
free(ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
328 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
329 |
// exit |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
330 |
co_exit(); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
331 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
332 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
333 |
static void handle_accept (evutil_socket_t fd, short event, void *arg) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
334 |
// the new client's client_info |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
335 |
struct client_info *ctx = NULL; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
336 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
337 |
// the socket from accept() |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
338 |
evutil_socket_t socket = -1; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
339 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
340 |
// not used |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
341 |
struct sockaddr_storage addr; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
342 |
socklen_t addr_len; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
343 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
344 |
// arg is NULL and unused |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
345 |
(void) arg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
346 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
347 |
// accept the connection |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
348 |
addr_len = sizeof(struct sockaddr_storage); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
349 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
350 |
if ((socket = accept(fd, (struct sockaddr *) &addr, &addr_len)) == -1) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
351 |
PERROR("accept"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
352 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
353 |
// alloc a new client_info |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
354 |
if (!(ctx = calloc(1, sizeof(*ctx)))) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
355 |
ERROR("calloc"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
356 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
357 |
// store the socket |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
358 |
ctx->socket = socket; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
359 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
360 |
// and the endpoint |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
361 |
ctx->connect_target = arg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
362 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
363 |
// create the coroutine |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
364 |
if ((ctx->co = co_create(&client_co, ctx, NULL, CLIENT_STACK_SIZE)) == NULL) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
365 |
ERROR("co_create"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
366 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
367 |
// we can start up the coroutine right away |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
368 |
co_call(ctx->co); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
369 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
370 |
// done handling this accept |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
371 |
return; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
372 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
373 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
374 |
if (ctx) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
375 |
if (ctx->co) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
376 |
co_delete(ctx->co); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
377 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
378 |
free(ctx); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
379 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
380 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
381 |
else if (socket != -1) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
382 |
close(socket); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
383 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
384 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
385 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
386 |
int main (int argc, char **argv) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
387 |
// libevent init |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
388 |
struct event_base *ev_base = event_init(); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
389 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
390 |
if (!ev_base) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
391 |
FATAL("event_init"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
392 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
393 |
// process command-line arguments |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
394 |
int opt; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
395 |
const char *listen_spec = NULL, *connect_spec = NULL; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
396 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
397 |
while ((opt = getopt(argc, argv, "hl:c:")) != -1) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
398 |
switch (opt) { |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
399 |
case 'l': |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
400 |
if (listen_spec) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
401 |
FATAL("only specify -l once"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
402 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
403 |
listen_spec = optarg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
404 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
405 |
break; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
406 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
407 |
case 'c': |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
408 |
if (connect_spec) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
409 |
FATAL("only specify -c once"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
410 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
411 |
connect_spec = optarg; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
412 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
413 |
break; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
414 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
415 |
case 'h': |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
416 |
default: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
417 |
err_exit("Usage: %s [-h] [-l listen] -c <connect>", argv[0]); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
418 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
419 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
420 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
421 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
422 |
if (!connect_spec) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
423 |
err_exit("Must specify -c; see `%s -h`", argv[0]); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
424 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
425 |
// ignore SIGPIPE |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
426 |
struct sigaction sigpipe; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
427 |
memset(&sigpipe, 0, sizeof(sigpipe)); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
428 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
429 |
sigpipe.sa_handler = SIG_IGN; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
430 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
431 |
sigaction(SIGPIPE, &sigpipe, NULL); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
432 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
433 |
// endpoints |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
434 |
struct config_endpoint listen_endpoint, connect_endpoint; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
435 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
436 |
endpoint_init(&listen_endpoint, LISTEN_PORT); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
437 |
endpoint_init(&connect_endpoint, 0); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
438 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
439 |
if ( |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
440 |
endpoint_parse(&listen_endpoint, listen_spec) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
441 |
|| endpoint_parse(&connect_endpoint, connect_spec) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
442 |
) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
443 |
goto error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
444 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
445 |
// the listen socket |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
446 |
int listen_sock; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
447 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
448 |
if ((listen_sock = socket_listen(&listen_endpoint, SOCK_STREAM)) == -1) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
449 |
goto error; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
450 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
451 |
// create the listen event |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
452 |
struct event listen_ev; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
453 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
454 |
event_set(&listen_ev, listen_sock, EV_READ | EV_PERSIST, &handle_accept, &connect_endpoint); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
455 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
456 |
if (event_add(&listen_ev, NULL)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
457 |
PERROR("event_add"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
458 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
459 |
// we shall now run |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
460 |
INFO("run"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
461 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
462 |
// run the libevent mainloop |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
463 |
if (event_base_dispatch(ev_base)) |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
464 |
WARNING("event_dispatch"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
465 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
466 |
INFO("shutdown"); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
467 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
468 |
// cleanup |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
469 |
event_base_free(ev_base); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
470 |
close(listen_sock); |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
471 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
472 |
return EXIT_SUCCESS; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
473 |
|
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
474 |
error: |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
475 |
return EXIT_FAILURE; |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
476 |
} |
a5c09677ca6f
add the coro_test.c file, and update .hgignore a bit
Tero Marttila <terom@fixme.fi>
parents:
diff
changeset
|
477 |