Repository navigation
Expand file tree
/
Copy pathtx_aiocb.cpp
More file actions
283 lines (240 loc) · 5.49 KB
/
Copy pathtx_aiocb.cpp
File metadata and controls
283 lines (240 loc) · 5.49 KB
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
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <libtx/queue.h>
#if defined(WIN32)
#include <winsock2.h>
#else
#include <limits.h>
#include <sys/uio.h>
#include <sys/types.h>
#include <sys/socket.h>
#endif
#include "txall.h"
void tx_outcb_prepare(tx_aiocb *filp, tx_task_t *task, int flags)
{
return filp->tx_fops->op_active_out(filp, task);
}
void tx_outcb_cancel(tx_aiocb *filp, void *task)
{
return filp->tx_fops->op_cancel_out(filp, task);
}
void tx_outcb_update(tx_aiocb *filp, int len)
{
if (len >= 0) {
return;
}
#ifndef WIN32
if (errno == EAGAIN) {
filp->tx_flags &= ~TX_WRITABLE;
return;
}
#else
if (WSAGetLastError() == WSAEWOULDBLOCK) {
filp->tx_flags &= ~TX_WRITABLE;
return;
}
#endif
return;
}
int tx_outcb_write(tx_aiocb *filp, const void *buf, size_t len)
{
int n;
tx_poll_op *ops;
ops = filp->tx_poll->tx_ops;
if (ops->tx_sendout == NULL) {
n = write(filp->tx_fd, buf, len);
tx_outcb_update(filp, n);
} else {
len = (len < 8192? len: 8192);
n = ops->tx_sendout(filp, buf, len);
/* XXX */
}
return n;
}
int tx_outcb_xsend(tx_aiocb *filp, tx_aiobuf buf[], size_t count)
{
#ifndef WIN32
ssize_t n = 0;
struct iovec vec[IOV_MAX];
TX_ASSERT(count < IOV_MAX);
for (int i = 0; i < count; i++) {
vec[i].iov_len = buf[i].iob_len;
vec[i].iov_base = buf[i].iob_buf;
}
n = writev(filp->tx_fd, vec, count);
tx_outcb_update(filp, n);
return n;
#else
TX_CHECK(1, "not supported");
return -1;
#endif
}
void tx_aincb_active(tx_aiocb *filp, tx_task_t *task)
{
return filp->tx_fops->op_active_in(filp, task);
}
void tx_aincb_stop(tx_aiocb *filp, void *task)
{
return filp->tx_fops->op_cancel_in(filp, task);
}
void tx_aincb_update(tx_aiocb *filp, int len)
{
if (len >= 0) {
return;
}
#ifndef WIN32
if (errno == EAGAIN) {
filp->tx_flags &= ~TX_READABLE;
return;
}
#else
if (WSAGetLastError() == WSAEWOULDBLOCK) {
filp->tx_flags &= ~TX_READABLE;
return;
}
#endif
return;
}
static void generic_active_out(tx_aiocb *filp, tx_task_t *task)
{
tx_poll_op *ops;
if (tx_writable(filp)) {
TX_CHECK(filp->tx_filterout == NULL, "tx_filterout not null");
tx_task_active(task, filp);
return;
}
if (filp->tx_filterout != task) {
TX_CHECK(filp->tx_filterout == NULL, "tx_filterout not null");
filp->tx_filterout = task;
}
if (filp->tx_flags & TX_POLLOUT) {
/* XXXX */
return;
}
ops = filp->tx_poll->tx_ops;
ops->tx_pollout(filp);
return;
}
static void generic_cancel_out(tx_aiocb *filp, void *verify)
{
if (verify == filp->tx_filterout)
filp->tx_filterout = NULL;
return;
}
static void generic_active_in(tx_aiocb *filp, tx_task_t *task)
{
tx_poll_op *ops;
if (tx_readable(filp)) {
TX_CHECK(filp->tx_filterin == NULL, "tx_filterin not null");
tx_task_active(task, filp);
return;
}
if (filp->tx_filterin != task) {
TX_CHECK(filp->tx_filterin == NULL, "tx_filterout not null");
filp->tx_filterin = task;
}
if (filp->tx_flags & TX_POLLIN) {
/* XXXX */
return;
}
ops = filp->tx_poll->tx_ops;
ops->tx_pollin(filp);
return;
}
static void generic_cancel_in(tx_aiocb *filp, void *verify)
{
if (verify == filp->tx_filterin)
filp->tx_filterin = NULL;
return;
}
static tx_aiocb_op _generic_fops = {
#ifndef WINNT
.op_active_out = generic_active_out,
.op_cancel_out = generic_cancel_out,
.op_active_in = generic_active_in,
.op_cancel_in = generic_cancel_in
#else
op_active_out: generic_active_out,
op_cancel_out: generic_cancel_out,
op_active_in: generic_active_in,
op_cancel_in: generic_cancel_in
#endif
};
void tx_aiocb_init(tx_aiocb *filp, tx_poll_t *poll, int fd)
{
tx_poll_op *ops;
filp->tx_fd = fd;
filp->tx_flags = TX_WRITABLE;
filp->tx_poll = poll;
filp->tx_privp = NULL;
filp->tx_filterin = NULL;
filp->tx_filterout = NULL;
filp->tx_fops = &_generic_fops;
if (fd == -1) return;
ops = filp->tx_poll->tx_ops;
ops->tx_attach(filp);
return;
}
void tx_aiocb_init(tx_aiocb *filp, tx_loop_t *loop, int fd)
{
tx_poll_t *poll = tx_poll_get(loop);
TX_ASSERT(poll != NULL);
tx_aiocb_init(filp, poll, fd);
return;
}
void tx_listen_init(tx_aiocb *filp, tx_loop_t *loop, int fd)
{
tx_poll_t *poll = tx_poll_get(loop);
TX_ASSERT(poll != NULL);
tx_aiocb_init(filp, poll, fd);
filp->tx_flags |= TX_LISTEN;
return;
}
int tx_listen_accept(tx_aiocb *filp, struct sockaddr *sa, size_t *outlen)
{
#ifndef WIN32
socklen_t salen = (outlen? *outlen: 0);
int newfd = accept(filp->tx_fd, sa, outlen? &salen: NULL);
if (outlen != NULL) *outlen = salen;
tx_aincb_update(filp, newfd);
return newfd;
#else
tx_poll_op *ops = filp->tx_poll->tx_ops;
return ops->tx_accept(filp, sa, outlen);
#endif
}
int tx_aiocb_connect(tx_aiocb *filp, struct sockaddr *sa, size_t len, tx_task_t *t)
{
int error = 0;
#ifdef __FreeBSD__
if (sa->sa_family == AF_INET && len > sizeof(struct sockaddr_in)) {
len = sizeof(struct sockaddr_in);
} else if (sa->sa_family == AF_INET6 && len > sizeof(struct sockaddr_in6)) {
len = sizeof(struct sockaddr_in6);
}
#endif
#ifndef WIN32
error = connect(filp->tx_fd, sa, len);
if (error == -1 && errno == EINPROGRESS) {
filp->tx_flags &= ~(TX_WRITABLE| TX_READABLE);
generic_active_out(filp, t);
return -EINPROGRESS;
}
filp->tx_flags |= TX_WRITABLE;
if (error == -1) filp->tx_flags |= TX_READABLE;
tx_task_active(t, filp);
#else
tx_poll_op *ops = filp->tx_poll->tx_ops;
error = ops->tx_connect(filp, sa, len);
generic_active_out(filp, t);
#endif
return error;
}
void tx_aiocb_fini(tx_aiocb *filp)
{
if (filp->tx_fd == -1) return;
tx_poll_op *ops = filp->tx_poll->tx_ops;
ops->tx_detach(filp);
return;
}