/
socket.pmc
443 lines (291 loc) · 9.54 KB
/
socket.pmc
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
Copyright (C) 2008, Parrot Foundation.
=head1 NAME
src/pmc/socket.pmc - Socket PMC
=head1 DESCRIPTION
The Socket PMC performs network I/O operations.
=head2 Vtable Functions
=over 4
=cut
*/
#include "../src/io/io_private.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass Socket extends Handle provides socket auto_attrs {
ATTR PMC *local; /* Local addr */
ATTR PMC *remote; /* Remote addr */
ATTR STRING *buf;
/*
=item C<void init()>
Initializes a newly created Socket object.
=cut
*/
VTABLE void init() {
Parrot_Socket_attributes *data_struct =
(Parrot_Socket_attributes *) PMC_data(SELF);
data_struct->local = PMCNULL;
data_struct->remote = PMCNULL;
data_struct->buf = STRINGNULL;
Parrot_io_socket_initialize_handle(INTERP, SELF);
PObj_custom_mark_destroy_SETALL(SELF);
}
/*
=item C<PMC *clone()>
Create a copy of the socket handle.
=cut
*/
VTABLE PMC *clone() {
PMC * copy = SUPER();
Parrot_Socket_attributes * const old_struct = PARROT_SOCKET(SELF);
Parrot_Socket_attributes * const data_struct = PARROT_SOCKET(copy);
if (!PMC_IS_NULL(old_struct->local))
data_struct->local = VTABLE_clone(INTERP, old_struct->local);
if (!PMC_IS_NULL(old_struct->remote))
data_struct->remote = VTABLE_clone(INTERP, old_struct->remote);
return copy;
}
/*
=item C<void mark()>
Mark active socket handle data as live.
=cut
*/
VTABLE void mark() {
Parrot_Socket_attributes * const data = PARROT_SOCKET(SELF);
if (data) {
Parrot_gc_mark_PMC_alive(INTERP, data->local);
Parrot_gc_mark_PMC_alive(INTERP, data->remote);
Parrot_gc_mark_STRING_alive(INTERP, data->buf);
}
}
/*
=item C<void destroy()>
Free structures.
=cut
*/
VTABLE void destroy() {
if (PARROT_SOCKET(SELF)) {
Parrot_Socket_attributes *data_struct = PARROT_SOCKET(SELF);
if (data_struct->os_handle != PIO_INVALID_HANDLE)
Parrot_io_close_piohandle(INTERP, data_struct->os_handle);
data_struct->os_handle = PIO_INVALID_HANDLE;
}
}
/*
=item C<INTVAL get_bool()>
Returns whether the Socket is currently open.
=cut
*/
VTABLE INTVAL get_bool() {
return !Parrot_io_socket_is_closed(INTERP, SELF);
}
/*
=back
=head2 Methods
=over 4
=item C<socket(INTVAL fam, INTVAL type, INTVAL proto)>
=cut
*/
METHOD socket(INTVAL fam, INTVAL type, INTVAL proto) {
if (Parrot_io_socket(INTERP, SELF, fam, type, proto) < 0)
RETURN(PMC * PMCNULL);
RETURN(PMC * SELF);
}
/*
=item C<poll(INTVAL which, INTVAL sec, INTVAL usec)>
Watches the socket for C<sec> seconds and C<usec> milliseconds. C<which>
is a bitmask representing the states you want to watch for. Or together 1
for readable, two for writeable, and four for exceptions.
=cut
*/
METHOD poll(INTVAL which, INTVAL sec, INTVAL usec) {
INTVAL poll = Parrot_io_poll(INTERP, SELF, which, sec, usec);
RETURN(INTVAL poll);
}
/*
=item C<sockaddr(STRING * address, INTVAL port)>
C<sockaddr> returns an object representing a socket address, generated
from a port number (integer) and an address (string).
=cut
*/
METHOD sockaddr(STRING * address, INTVAL port) {
PMC * res = PIO_SOCKADDR_IN(INTERP, address, port);
RETURN(PMC * res);
}
/*
=item C<METHOD is_closed()>
Test if the socket is closed.
=cut
*/
METHOD is_closed() {
const INTVAL status = !VTABLE_get_bool(INTERP, SELF);
RETURN(INTVAL status);
}
/*
=item C<connect(PMC * address)>
Connects a socket object to an address.
The asynchronous version takes an additional final PMC callback
argument, and only returns a status object. When the socket operation is
complete, it invokes the callback, passing it a status object and the
socket object it was called on. [If you want notification when a connect
operation is completed, you probably want to do something with that
connected socket object.]
=cut
*/
METHOD connect(PMC * address) {
INTVAL res = Parrot_io_connect(INTERP, SELF, address);
RETURN(INTVAL res);
}
/*
=item C<recv()>
Receives a message from a connected socket object. It returns
the message in a string.
The asynchronous version takes an additional final PMC callback
argument, and only returns a status object. When the recv operation is
complete, it invokes the callback, passing it a status object and a
string containing the received message.
=cut
*/
METHOD recv() {
STRING * result;
GET_ATTR_buf(INTERP, SELF, result);
if (result != STRINGNULL && Parrot_str_length(INTERP, result) > 0) {
SET_ATTR_buf(INTERP, SELF, STRINGNULL);
}
else {
result = Parrot_io_reads(INTERP, SELF, 0);
}
RETURN(STRING * result);
}
/*
=item C<send(STRING *buf)>
Sends a message string to a connected socket object.
The asynchronous version takes an additional final PMC callback
argument, and only returns a status object. When the send operation is
complete, it invokes the callback, passing it a status object.
=cut
*/
METHOD send(STRING *buf) {
INTVAL res = Parrot_io_send(INTERP, SELF, buf);
RETURN(INTVAL res);
}
/*
=item C<bind(PMC *host)>
C<bind> binds a socket object to the port and address specified by an
address object (the packed result of C<sockaddr>).
The asynchronous version takes an additional final PMC callback
argument, and only returns a status object. When the bind operation is
complete, it invokes the callback, passing it a status object and the
socket object it was called on. [If you want notification when a bind
operation is completed, you probably want to do something with that
bound socket object.]
=cut
*/
METHOD bind(PMC *host) {
INTVAL res = Parrot_io_bind(INTERP, SELF, host);
RETURN(INTVAL res);
}
/*
=item C<listen(INTVAL backlog)>
C<listen> specifies that a socket object is willing to accept incoming
connections. The integer argument gives the maximum size of the queue
for pending connections.
There is no asynchronous version. C<listen> marks a set of attributes on
the socket object.
=cut
*/
METHOD listen(INTVAL backlog) {
INTVAL res = Parrot_io_listen(INTERP, SELF, backlog);
RETURN(INTVAL res);
}
/*
=item C<accept()>
C<accept> accepts a new connection on a given socket object, and returns
a newly created socket object for the connection.
The asynchronous version takes an additional final PMC callback
argument, and only returns a status object. When the accept operation
receives a new connection, it invokes the callback, passing it a status
object and a newly created socket object for the connection. [While the
synchronous C<accept> has to be called repeatedly in a loop (once for
each connection received), the asynchronous version is only called once,
but continues to send new connection events until the socket is closed.]
=cut
*/
METHOD accept() {
PMC * res = Parrot_io_accept(INTERP, SELF);
RETURN(PMC * res);
}
/*
=item C<METHOD read(INTVAL bytes)>
Read the given number of bytes from the socket and return them in a string.
=cut
*/
METHOD read(INTVAL nb) {
STRING *result;
STRING *buf;
GET_ATTR_buf(INTERP, SELF, buf);
if (Parrot_io_socket_is_closed(INTERP, SELF))
RETURN(STRING * STRINGNULL);
if (buf == STRINGNULL)
buf = Parrot_io_reads(INTERP, SELF, 0);
while (Parrot_str_length(INTERP, buf) < nb) {
STRING *more = Parrot_io_reads(INTERP, SELF, 0);
if (Parrot_str_length(INTERP, more) == 0) {
SET_ATTR_buf(INTERP, SELF, STRINGNULL);
RETURN(STRING *buf);
}
buf = Parrot_str_concat(INTERP, buf, more);
}
result = Parrot_str_substr(INTERP, buf, 0, nb);
buf = Parrot_str_substr(INTERP, buf, nb, Parrot_str_length(INTERP, buf) - nb);
SET_ATTR_buf(INTERP, SELF, buf);
RETURN(STRING *result);
}
/*
=item C<METHOD readline()>
Read a line from the socket and return it in a string.
=cut
*/
METHOD readline() {
INTVAL idx;
STRING *result;
STRING *buf;
GET_ATTR_buf(INTERP, SELF, buf);
if (Parrot_io_socket_is_closed(INTERP, SELF))
RETURN(STRING * STRINGNULL);
if (buf == STRINGNULL)
buf = Parrot_io_reads(INTERP, SELF, 0);
while ((idx = Parrot_str_find_index(INTERP, buf, CONST_STRING(INTERP, "\n"), 0)) < 0) {
STRING *more = Parrot_io_reads(INTERP, SELF, 0);
if (Parrot_str_length(INTERP, more) == 0) {
SET_ATTR_buf(INTERP, SELF, STRINGNULL);
RETURN(STRING *buf);
}
buf = Parrot_str_concat(INTERP, buf, more);
}
idx++;
result = Parrot_str_substr(INTERP, buf, 0, idx);
buf = Parrot_str_substr(INTERP, buf, idx, Parrot_str_length(INTERP, buf) - idx);
SET_ATTR_buf(INTERP, SELF, buf);
RETURN(STRING *result);
}
/*
=item C<METHOD puts(STRING *buf)>
Print the string to the socket.
=cut
*/
METHOD puts(STRING *buf) {
INTVAL res = Parrot_io_send(INTERP, SELF, buf);
RETURN(INTVAL res);
}
/*
=back
=cut
*/
} /* end pmclass */
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/