-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathol_socket.c
More file actions
363 lines (308 loc) · 9.23 KB
/
ol_socket.c
File metadata and controls
363 lines (308 loc) · 9.23 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* Copyright (c) 2009, Maxim Kharchenko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the author nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Maxim Kharchenko ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Maxim Kharchenko BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "outlet.h"
#include "scheduler.h"
#include "binary.h"
#include "buffer.h"
#include "proc.h"
#include "atom.h"
#define SOCK_INBUF_LEN 4096
#define SOCK_OUTBUF_LEN 4096
typedef struct ol_socket_data_t ol_socket_data_t;
struct ol_socket_data_t {
apr_socket_t *sock;
int is_connecting;
int is_closing;
buffer_t *in_buf;
buffer_t *out_buf;
int packet_expected;
int expected_size;
int space_required;
int required_size;
};
int ol_socket_want_read(outlet_t *self);
int ol_socket_want_write(outlet_t *self);
int ol_socket_is_socket(outlet_t *self);
apr_socket_t *ol_socket_get_socket(outlet_t *self);
apr_status_t ol_socket_do_readable(outlet_t *self);
apr_status_t ol_socket_do_writable(outlet_t *self);
apr_status_t ol_socket_send(outlet_t *self, term_t io);
apr_status_t ol_socket_set_option(outlet_t *self, term_t opt, term_t value);
apr_status_t ol_socket_close(outlet_t *self);
apr_status_t ol_socket_close0(outlet_t *self);
outlet_t *ol_socket_make(apr_socket_t *sock, int is_connecting)
{
ol_socket_data_t *data;
apr_pool_t *pool = apr_socket_pool_get(sock);
outlet_t *outlet = apr_palloc(pool, sizeof(outlet_t));
outlet->serial = (uint)-1;
outlet->owner_in = outlet->owner_out = 0;
outlet->pool = pool;
outlet->want_read = ol_socket_want_read;
outlet->want_write = ol_socket_want_write;
outlet->is_socket = ol_socket_is_socket;
outlet->get_socket = ol_socket_get_socket;
outlet->is_file = 0;
outlet->get_file = 0;
outlet->try_read = 0;
outlet->try_write = 0;
outlet->do_readable = ol_socket_do_readable;
outlet->do_writable = ol_socket_do_writable;
outlet->send = ol_socket_send;
outlet->read = 0;
outlet->write = 0;
outlet->set_option = ol_socket_set_option;
outlet->close = ol_socket_close;
outlet->close0 = ol_socket_close0;
data = apr_palloc(pool, sizeof(ol_socket_data_t));
data->sock = sock;
data->is_connecting = is_connecting;
data->is_closing = 0;
data->in_buf = buffer_make(pool, SOCK_INBUF_LEN);
data->out_buf = buffer_make(pool, SOCK_OUTBUF_LEN);
data->packet_expected = 0;
data->expected_size = 0;
data->space_required = 0;
data->required_size = 0;
outlet->data = data;
return outlet;
}
int ol_socket_want_read(outlet_t *self)
{
ol_socket_data_t *data = self->data;
if (data->is_closing || data->is_connecting)
return 0;
return buffer_available(data->in_buf) > 0;
}
int ol_socket_want_write(outlet_t *self)
{
ol_socket_data_t *data = self->data;
if (data->is_closing || data->is_connecting)
return 1;
return buffer_len(data->out_buf) > 0;
}
int ol_socket_is_socket(outlet_t *self)
{
return 1;
}
apr_socket_t *ol_socket_get_socket(outlet_t *self)
{
ol_socket_data_t *data = self->data;
return data->sock;
}
apr_status_t ol_socket_do_readable(outlet_t *self)
{
apr_status_t rs;
ol_socket_data_t *data = self->data;
rs = buffer_socket_recv(data->in_buf, data->sock);
if (rs == 0)
{
if (data->packet_expected)
{
int len = buffer_len(data->in_buf);
if (data->expected_size == 0 ||
(data->expected_size > 0 && len >= data->expected_size))
{
proc_t *proc = self->owner_in;
if (data->expected_size > 0)
len = data->expected_size;
if (proc != 0)
{
term_t bin = heap_binary(proc->heap, len*8, buffer_ptr(data->in_buf));
term_t msg = heap_tuple3(proc->heap, A_TCP, outlet_id(self), bin);
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
buffer_consume(data->in_buf, len);
}
data->packet_expected = 0;
data->expected_size = 0;
}
}
}
return rs;
}
apr_status_t ol_socket_do_writable(outlet_t *self)
{
ol_socket_data_t *data = self->data;
if (data->is_connecting)
{
proc_t *proc = self->owner_in;
if (proc)
{
term_t msg = heap_tuple2(proc->heap, A_TCP_CONNECTED, outlet_id(self));
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
}
data->is_connecting = 0;
}
if (buffer_len(data->out_buf) > 0)
{
apr_status_t rs;
rs = buffer_socket_send(data->out_buf, data->sock);
if (rs != 0)
return rs;
}
if (data->is_closing && buffer_len(data->out_buf) == 0)
return APR_EOF; //make poll close0 the socket
if (data->space_required && buffer_available(data->out_buf) >= data->required_size)
{
proc_t *proc = self->owner_out;
int avail = buffer_available(data->out_buf);
term_t msg = heap_tuple3(proc->heap, A_TCP_SPACE, outlet_id(self), tag_int(avail));
//TODO: insure that only owner can send to socket
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
data->space_required = 0;
}
return APR_SUCCESS;
}
static apr_status_t outlet_buffer_send(buffer_t *buf, term_t io)
{
int avail;
if (is_nil(io))
return 0;
avail = buffer_available(buf);
if (is_int(io))
{
if (avail < 1)
return APR_EINCOMPLETE;
else
buffer_put_byte(buf, int_value(io));
}
else if (is_binary(io))
{
//TODO: odd-trailer binaries
term_box_t *bb = peel(io);
int size = BIN_BYTE_SIZE(bb->binary.bit_size);
if (size > avail)
{
buffer_put_data(buf, bb->binary.data, avail);
return APR_EINCOMPLETE;
}
else
buffer_put_data(buf, bb->binary.data, size);
}
else if (is_list(io))
{
while (is_cons(io))
{
term_box_t *cb = peel(io);
apr_status_t rs;
rs = outlet_buffer_send(buf, cb->cons.head);
if (rs != 0)
return rs;
io = cb->cons.tail;
}
}
else
return APR_BADARG;
return APR_SUCCESS;
}
apr_status_t ol_socket_send(outlet_t *self, term_t io)
{
ol_socket_data_t *data = self->data;
if (data->is_closing)
return APR_EOF;
return outlet_buffer_send(data->out_buf, io);
}
apr_status_t ol_socket_set_option(outlet_t *self, term_t opt, term_t value)
{
ol_socket_data_t *data = self->data;
if (opt == A_EXPECT)
{
if (!is_int(value))
return APR_BADARG;
data->expected_size = int_value(value);
if (data->expected_size < 0)
return APR_BADARG;
if (self->owner_in == 0)
return APR_ENOPROC;
//enough data may already be there
if ((data->expected_size == 0 && buffer_len(data->in_buf)) > 0 ||
(data->expected_size > 0 && buffer_len(data->in_buf) >= data->expected_size))
{
proc_t *proc = self->owner_in;
int len = (data->expected_size == 0)
?buffer_len(data->in_buf)
:data->expected_size;
term_t bin = heap_binary(proc->heap, len*8, buffer_ptr(data->in_buf));
term_t msg = heap_tuple3(proc->heap, A_TCP, outlet_id(self), bin);
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
buffer_consume(data->in_buf, len);
}
else
data->packet_expected = 1;
}
else if (opt == A_REQUIRE)
{
int size;
if (!is_int(value))
return APR_BADARG;
size = int_value(value);
if (size < 0 || size > SOCK_OUTBUF_LEN)
return APR_BADARG;
data->required_size = size;
if (buffer_available(data->out_buf) >= size)
{
proc_t *proc = self->owner_out;
int avail = buffer_available(data->out_buf);
term_t msg = heap_tuple3(proc->heap, A_TCP_SPACE, outlet_id(self), tag_int(avail));
//TODO: insure that only owner can send to socket
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
data->space_required = 0;
}
else
data->space_required = 1;
}
else
return APR_BADARG;
return APR_SUCCESS;
}
apr_status_t ol_socket_close(outlet_t *self)
{
ol_socket_data_t *data = self->data;
data->is_closing = 1;
return APR_SUCCESS;
}
apr_status_t ol_socket_close0(outlet_t *self)
{
ol_socket_data_t *data = self->data;
proc_t *proc = self->owner_in;
if (proc)
{
int len = buffer_len(data->in_buf);
term_t msg;
if (len > 0)
{
term_t bin = heap_binary(proc->heap, len*8, buffer_ptr(data->in_buf));
msg = heap_tuple3(proc->heap, A_TCP, outlet_id(self), bin);
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
buffer_clear(data->in_buf);
}
msg = heap_tuple2(proc->heap, A_TCP_CLOSED, outlet_id(self));
scheduler_new_local_mail(proc->teevm->scheduler, proc, msg);
}
return apr_socket_close(data->sock);
}
// EOF