equal
deleted
inserted
replaced
5 #include <fcntl.h> |
5 #include <fcntl.h> |
6 #include <unistd.h> |
6 #include <unistd.h> |
7 #include <assert.h> |
7 #include <assert.h> |
8 |
8 |
9 /** |
9 /** |
10 * Our pseudo-transport_type |
|
11 */ |
|
12 static struct transport_type transport_fd_type; |
|
13 |
|
14 /** |
|
15 * Our libevent callback |
10 * Our libevent callback |
16 */ |
11 */ |
17 static void transport_fd_on_event (evutil_socket_t _fd, short what, void *arg) |
12 static void transport_fd_on_event (evutil_socket_t _fd, short ev_what, void *arg) |
18 { |
13 { |
19 struct transport_fd *fd = arg; |
14 struct transport_fd *fd = arg; |
20 |
15 |
21 (void) _fd; |
16 (void) _fd; |
|
17 |
|
18 short what = 0; |
|
19 |
|
20 // build flags |
|
21 if (ev_what & EV_READ) |
|
22 what |= TRANSPORT_READ; |
|
23 |
|
24 if (ev_what & EV_WRITE) |
|
25 what |= TRANSPORT_WRITE; |
22 |
26 |
23 // invoke user callback |
27 // invoke user callback |
24 fd->cb_func(fd, what, fd->cb_arg); |
28 fd->cb_func(fd, what, fd->cb_arg); |
25 } |
29 } |
26 |
30 |
29 */ |
33 */ |
30 err_t transport_fd_methods_read (transport_t *transport, void *buf, size_t *len, error_t *err) |
34 err_t transport_fd_methods_read (transport_t *transport, void *buf, size_t *len, error_t *err) |
31 { |
35 { |
32 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
36 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
33 int ret; |
37 int ret; |
|
38 |
|
39 RESET_ERROR(err); |
34 |
40 |
35 // read(), and detect non-EAGAIN or EOF |
41 // read(), and detect non-EAGAIN or EOF |
36 if ((ret = read(fd->fd, buf, *len)) < 0 && errno != EAGAIN) |
42 if ((ret = read(fd->fd, buf, *len)) < 0 && errno != EAGAIN) |
37 // unexpected error |
43 // unexpected error |
38 RETURN_SET_ERROR_ERRNO(err, ERR_READ); |
44 RETURN_SET_ERROR_ERRNO(err, ERR_READ); |
58 err_t transport_fd_methods_write (transport_t *transport, const void *buf, size_t *len, struct error_info *err) |
64 err_t transport_fd_methods_write (transport_t *transport, const void *buf, size_t *len, struct error_info *err) |
59 { |
65 { |
60 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
66 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
61 int ret; |
67 int ret; |
62 |
68 |
|
69 RESET_ERROR(err); |
|
70 |
63 // write(), and detect non-EAGAIN or EOF |
71 // write(), and detect non-EAGAIN or EOF |
64 if ((ret = write(fd->fd, buf, *len)) < 0 && errno != EAGAIN) |
72 if ((ret = write(fd->fd, buf, *len)) < 0 && errno != EAGAIN) |
65 // unexpected error |
73 // unexpected error |
66 RETURN_SET_ERROR_ERRNO(err, ERR_WRITE); |
74 RETURN_SET_ERROR_ERRNO(err, ERR_WRITE); |
67 |
75 |
85 } |
93 } |
86 |
94 |
87 return SUCCESS; |
95 return SUCCESS; |
88 } |
96 } |
89 |
97 |
90 err_t transport_fd_methods_events (transport_t *transport, short mask, error_t *err) |
98 err_t transport_fd_methods_events (transport_t *transport, short ev_mask, error_t *err) |
91 { |
99 { |
92 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
100 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
93 |
101 |
94 short _mask = 0; |
102 short mask = 0; |
95 |
103 |
96 // enable read as requested |
104 // enable read as requested |
97 if (mask & TRANSPORT_READ) |
105 if (ev_mask & TRANSPORT_READ) |
98 _mask |= TRANSPORT_READ; |
106 mask |= TRANSPORT_READ; |
99 |
107 |
100 // enable write if requested and it's currently enabled |
108 // enable write if requested and it's currently enabled |
101 if ((mask & TRANSPORT_WRITE) && event_pending(fd->ev_write, EV_WRITE, NULL)) |
109 if ((ev_mask & TRANSPORT_WRITE) && event_pending(fd->ev_write, EV_WRITE, NULL)) |
102 _mask |= TRANSPORT_WRITE; |
110 mask |= TRANSPORT_WRITE; |
103 |
111 |
104 // set |
112 // set |
105 return (ERROR_CODE(err) = transport_fd_events(fd, mask)); |
113 return (ERROR_CODE(err) = transport_fd_events(fd, mask)); |
106 } |
114 } |
107 |
115 |
110 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
118 struct transport_fd *fd = transport_check(transport, &transport_fd_type); |
111 |
119 |
112 transport_fd_destroy(fd); |
120 transport_fd_destroy(fd); |
113 } |
121 } |
114 |
122 |
115 const struct transport_methods transport_fd_methods = { |
123 const struct transport_type transport_fd_type = { |
116 .read = transport_fd_methods_read, |
124 .parent = NULL, |
117 .write = transport_fd_methods_write, |
125 .methods = { |
118 .events = transport_fd_methods_events, |
126 .read = transport_fd_methods_read, |
119 .destroy = _transport_fd_destroy |
127 .write = transport_fd_methods_write, |
|
128 .events = transport_fd_methods_events, |
|
129 .destroy = _transport_fd_destroy |
|
130 } |
120 }; |
131 }; |
121 |
132 |
122 /** |
133 /** |
123 * Dummy callbacks |
134 * Dummy callbacks |
124 */ |
135 */ |