cool.io 1.0.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/test.yaml +54 -0
  3. data/.gitignore +30 -0
  4. data/.rspec +3 -0
  5. data/{CHANGES → CHANGES.md} +175 -79
  6. data/{README.markdown → README.md} +43 -49
  7. data/Rakefile +44 -69
  8. data/appveyor.yml +18 -0
  9. data/cool.io.gemspec +18 -131
  10. data/examples/callbacked_echo_server.rb +24 -0
  11. data/ext/cool.io/buffer.c +781 -0
  12. data/ext/cool.io/cool.io.h +13 -0
  13. data/ext/cool.io/cool.io_ext.c +1 -0
  14. data/ext/cool.io/ev_wrap.h +4 -2
  15. data/ext/cool.io/extconf.rb +27 -16
  16. data/ext/cool.io/iowatcher.c +17 -8
  17. data/ext/cool.io/loop.c +36 -84
  18. data/ext/cool.io/stat_watcher.c +103 -25
  19. data/ext/cool.io/timer_watcher.c +3 -3
  20. data/ext/cool.io/utils.c +5 -0
  21. data/ext/cool.io/watcher.h +1 -1
  22. data/ext/libev/Changes +152 -3
  23. data/ext/libev/LICENSE +2 -1
  24. data/ext/libev/README +8 -8
  25. data/ext/libev/ev.c +1719 -348
  26. data/ext/libev/ev.h +146 -118
  27. data/ext/libev/ev_epoll.c +71 -20
  28. data/ext/libev/ev_kqueue.c +36 -16
  29. data/ext/libev/ev_poll.c +14 -11
  30. data/ext/libev/ev_port.c +43 -18
  31. data/ext/libev/ev_select.c +82 -25
  32. data/ext/libev/ev_vars.h +28 -21
  33. data/ext/libev/ev_win32.c +19 -10
  34. data/ext/libev/ev_wrap.h +166 -152
  35. data/ext/libev/test_libev_win32.c +1 -1
  36. data/gems.rb +9 -0
  37. data/lib/cool.io/async_watcher.rb +1 -1
  38. data/lib/cool.io/custom_require.rb +9 -0
  39. data/lib/cool.io/dns_resolver.rb +30 -22
  40. data/lib/cool.io/dsl.rb +32 -28
  41. data/lib/cool.io/io.rb +55 -35
  42. data/lib/cool.io/iowatcher.rb +1 -1
  43. data/lib/cool.io/listener.rb +35 -17
  44. data/lib/cool.io/loop.rb +17 -25
  45. data/lib/cool.io/meta.rb +6 -5
  46. data/lib/cool.io/server.rb +5 -4
  47. data/lib/cool.io/socket.rb +41 -30
  48. data/lib/cool.io/timer_watcher.rb +1 -1
  49. data/lib/cool.io/version.rb +7 -0
  50. data/lib/cool.io.rb +11 -10
  51. data/lib/coolio.rb +1 -1
  52. data/libev_ruby_gil.diff +175 -0
  53. data/libev_win_select.diff +130 -0
  54. data/spec/async_watcher_spec.rb +5 -5
  55. data/spec/dns_spec.rb +27 -8
  56. data/spec/iobuffer_spec.rb +147 -0
  57. data/spec/spec_helper.rb +15 -1
  58. data/spec/stat_watcher_spec.rb +81 -0
  59. data/spec/tcp_server_spec.rb +225 -0
  60. data/spec/tcp_socket_spec.rb +185 -0
  61. data/spec/timer_watcher_spec.rb +23 -19
  62. data/spec/udp_socket_spec.rb +58 -0
  63. data/spec/unix_listener_spec.rb +8 -8
  64. data/spec/unix_server_spec.rb +10 -8
  65. metadata +109 -112
  66. data/VERSION +0 -1
  67. data/examples/httpclient.rb +0 -38
  68. data/ext/http11_client/.gitignore +0 -5
  69. data/ext/http11_client/LICENSE +0 -31
  70. data/ext/http11_client/ext_help.h +0 -14
  71. data/ext/http11_client/extconf.rb +0 -6
  72. data/ext/http11_client/http11_client.c +0 -300
  73. data/ext/http11_client/http11_parser.c +0 -403
  74. data/ext/http11_client/http11_parser.h +0 -48
  75. data/ext/http11_client/http11_parser.rl +0 -173
  76. data/lib/cool.io/eventmachine.rb +0 -234
  77. data/lib/cool.io/http_client.rb +0 -419
  78. data/lib/rev.rb +0 -4
  79. data/spec/possible_tests/schedules_other_threads.rb +0 -48
  80. data/spec/possible_tests/test_on_resolve_failed.rb +0 -9
  81. data/spec/possible_tests/test_resolves.rb +0 -27
  82. data/spec/possible_tests/test_write_during_resolve.rb +0 -27
  83. data/spec/possible_tests/works_straight.rb +0 -71
@@ -0,0 +1,781 @@
1
+ /*
2
+ * Copyright (C) 2007-12 Tony Arcieri
3
+ * You may redistribute this under the terms of the MIT license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+ #include "ruby.h"
8
+ #include "ruby/io.h"
9
+
10
+ #include <assert.h>
11
+
12
+ #include <string.h>
13
+ #include <time.h>
14
+ #ifndef _MSC_VER
15
+ #include <unistd.h>
16
+ #endif
17
+ #include <errno.h>
18
+
19
+ /* 1 GiB maximum buffer size */
20
+ #define MAX_BUFFER_SIZE 0x40000000
21
+
22
+ /* Macro for retrieving the file descriptor from an FPTR */
23
+ #if !HAVE_RB_IO_T_FD
24
+ #define FPTR_TO_FD(fptr) fileno(fptr->f)
25
+ #else
26
+ #define FPTR_TO_FD(fptr) fptr->fd
27
+ #endif
28
+
29
+ /* Default number of bytes in each node's buffer. Should be >= MTU */
30
+ #define DEFAULT_NODE_SIZE 16384
31
+ static unsigned default_node_size = DEFAULT_NODE_SIZE;
32
+
33
+ struct buffer {
34
+ unsigned size, node_size;
35
+ struct buffer_node *head, *tail;
36
+ struct buffer_node *pool_head, *pool_tail;
37
+
38
+ };
39
+
40
+ struct buffer_node {
41
+ unsigned start, end;
42
+ struct buffer_node *next;
43
+ unsigned char data[0];
44
+ };
45
+
46
+ static VALUE mCoolio = Qnil;
47
+ static VALUE cCoolio_Buffer = Qnil;
48
+
49
+ static VALUE Coolio_Buffer_allocate(VALUE klass);
50
+ static void Coolio_Buffer_mark(struct buffer *);
51
+ static void Coolio_Buffer_free(struct buffer *);
52
+
53
+ static VALUE Coolio_Buffer_default_node_size(VALUE klass);
54
+ static VALUE Coolio_Buffer_set_default_node_size(VALUE klass, VALUE size);
55
+ static VALUE Coolio_Buffer_initialize(int argc, VALUE * argv, VALUE self);
56
+ static VALUE Coolio_Buffer_clear(VALUE self);
57
+ static VALUE Coolio_Buffer_size(VALUE self);
58
+ static VALUE Coolio_Buffer_empty(VALUE self);
59
+ static VALUE Coolio_Buffer_append(VALUE self, VALUE data);
60
+ static VALUE Coolio_Buffer_prepend(VALUE self, VALUE data);
61
+ static VALUE Coolio_Buffer_read(int argc, VALUE * argv, VALUE self);
62
+ static VALUE Coolio_Buffer_read_frame(VALUE self, VALUE data, VALUE mark);
63
+ static VALUE Coolio_Buffer_to_str(VALUE self);
64
+ static VALUE Coolio_Buffer_read_from(VALUE self, VALUE io);
65
+ static VALUE Coolio_Buffer_write_to(VALUE self, VALUE io);
66
+
67
+ static struct buffer *buffer_new(void);
68
+ static void buffer_clear(struct buffer * buf);
69
+ static void buffer_free(struct buffer * buf);
70
+ static void buffer_free_pool(struct buffer * buf);
71
+ static void buffer_prepend(struct buffer * buf, char *str, unsigned len);
72
+ static void buffer_append(struct buffer * buf, char *str, unsigned len);
73
+ static void buffer_read(struct buffer * buf, char *str, unsigned len);
74
+ static int buffer_read_frame(struct buffer * buf, VALUE str, char frame_mark);
75
+ static void buffer_copy(struct buffer * buf, char *str, unsigned len);
76
+ static int buffer_read_from(struct buffer * buf, int fd);
77
+ static int buffer_write_to(struct buffer * buf, int fd);
78
+
79
+ /*
80
+ * High-performance I/O buffer intended for use in non-blocking programs
81
+ *
82
+ * Data is stored in as a memory-pooled linked list of equally sized chunks.
83
+ * Routines are provided for high speed non-blocking reads and writes from
84
+ * Ruby IO objects.
85
+ */
86
+ void
87
+ Init_coolio_buffer()
88
+ {
89
+ VALUE cCoolio_IO;
90
+
91
+ mCoolio = rb_define_module("Coolio");
92
+ cCoolio_Buffer = rb_define_class_under(mCoolio, "Buffer", rb_cObject);
93
+ rb_define_alloc_func(cCoolio_Buffer, Coolio_Buffer_allocate);
94
+
95
+ rb_define_singleton_method(cCoolio_Buffer, "default_node_size",
96
+ Coolio_Buffer_default_node_size, 0);
97
+ rb_define_singleton_method(cCoolio_Buffer, "default_node_size=",
98
+ Coolio_Buffer_set_default_node_size, 1);
99
+
100
+ rb_define_method(cCoolio_Buffer, "initialize", Coolio_Buffer_initialize, -1);
101
+ rb_define_method(cCoolio_Buffer, "clear", Coolio_Buffer_clear, 0);
102
+ rb_define_method(cCoolio_Buffer, "size", Coolio_Buffer_size, 0);
103
+ rb_define_method(cCoolio_Buffer, "empty?", Coolio_Buffer_empty, 0);
104
+ rb_define_method(cCoolio_Buffer, "<<", Coolio_Buffer_append, 1);
105
+ rb_define_method(cCoolio_Buffer, "append", Coolio_Buffer_append, 1);
106
+ rb_define_method(cCoolio_Buffer, "write", Coolio_Buffer_append, 1);
107
+ rb_define_method(cCoolio_Buffer, "prepend", Coolio_Buffer_prepend, 1);
108
+ rb_define_method(cCoolio_Buffer, "read", Coolio_Buffer_read, -1);
109
+ rb_define_method(cCoolio_Buffer, "read_frame", Coolio_Buffer_read_frame, 2);
110
+ rb_define_method(cCoolio_Buffer, "to_str", Coolio_Buffer_to_str, 0);
111
+ rb_define_method(cCoolio_Buffer, "read_from", Coolio_Buffer_read_from, 1);
112
+ rb_define_method(cCoolio_Buffer, "write_to", Coolio_Buffer_write_to, 1);
113
+
114
+ rb_define_const(cCoolio_Buffer, "MAX_SIZE", INT2NUM(MAX_BUFFER_SIZE));
115
+ }
116
+
117
+ static VALUE
118
+ Coolio_Buffer_allocate(VALUE klass)
119
+ {
120
+ return Data_Wrap_Struct(klass, Coolio_Buffer_mark, Coolio_Buffer_free, buffer_new());
121
+ }
122
+
123
+ static void
124
+ Coolio_Buffer_mark(struct buffer * buf)
125
+ {
126
+ /* Naively discard the memory pool whenever Ruby garbage collects */
127
+ buffer_free_pool(buf);
128
+ }
129
+
130
+ static void
131
+ Coolio_Buffer_free(struct buffer * buf)
132
+ {
133
+ buffer_free(buf);
134
+ }
135
+
136
+ /**
137
+ * call-seq:
138
+ * Coolio::Buffer.default_node_size -> 4096
139
+ *
140
+ * Retrieves the current value of the default node size.
141
+ */
142
+ static VALUE
143
+ Coolio_Buffer_default_node_size(VALUE klass)
144
+ {
145
+ return UINT2NUM(default_node_size);
146
+ }
147
+
148
+ /*
149
+ * safely converts node sizes from Ruby numerics to C and raising
150
+ * ArgumentError or RangeError on invalid sizes
151
+ */
152
+ static unsigned
153
+ convert_node_size(VALUE size)
154
+ {
155
+ if (
156
+ rb_funcall(size, rb_intern("<"), 1, INT2NUM(1)) == Qtrue ||
157
+ rb_funcall(size, rb_intern(">"), 1, INT2NUM(MAX_BUFFER_SIZE)) == Qtrue
158
+ )
159
+ rb_raise(rb_eArgError, "invalid buffer size");
160
+
161
+ return (unsigned) NUM2INT(size);
162
+ }
163
+
164
+ /**
165
+ * call-seq:
166
+ * Coolio::Buffer.default_node_size = 16384
167
+ *
168
+ * Sets the default node size for calling Coolio::Buffer.new with no arguments.
169
+ */
170
+ static VALUE
171
+ Coolio_Buffer_set_default_node_size(VALUE klass, VALUE size)
172
+ {
173
+ default_node_size = convert_node_size(size);
174
+
175
+ return size;
176
+ }
177
+
178
+ /**
179
+ * call-seq:
180
+ * Coolio::Buffer.new(size = Coolio::Buffer.default_node_size) -> Coolio::Buffer
181
+ *
182
+ * Create a new Coolio::Buffer with linked segments of the given size
183
+ */
184
+ static VALUE
185
+ Coolio_Buffer_initialize(int argc, VALUE * argv, VALUE self)
186
+ {
187
+ VALUE node_size_obj;
188
+ struct buffer *buf;
189
+
190
+ if (rb_scan_args(argc, argv, "01", &node_size_obj) == 1) {
191
+ Data_Get_Struct(self, struct buffer, buf);
192
+
193
+ /*
194
+ * Make sure we're not changing the buffer size after data
195
+ * has been allocated
196
+ */
197
+ assert(!buf->head);
198
+ assert(!buf->pool_head);
199
+
200
+ buf->node_size = convert_node_size(node_size_obj);
201
+ }
202
+ return Qnil;
203
+ }
204
+
205
+ /**
206
+ * call-seq:
207
+ * Coolio::Buffer#clear -> nil
208
+ *
209
+ * Clear all data from the Coolio::Buffer
210
+ */
211
+ static VALUE
212
+ Coolio_Buffer_clear(VALUE self)
213
+ {
214
+ struct buffer *buf;
215
+ Data_Get_Struct(self, struct buffer, buf);
216
+
217
+ buffer_clear(buf);
218
+
219
+ return Qnil;
220
+ }
221
+
222
+ /**
223
+ * call-seq:
224
+ * Coolio::Buffer#size -> Integer
225
+ *
226
+ * Return the size of the buffer in bytes
227
+ */
228
+ static VALUE
229
+ Coolio_Buffer_size(VALUE self)
230
+ {
231
+ struct buffer *buf;
232
+ Data_Get_Struct(self, struct buffer, buf);
233
+
234
+ return INT2NUM(buf->size);
235
+ }
236
+
237
+ /**
238
+ * call-seq:
239
+ * Coolio::Buffer#empty? -> Boolean
240
+ *
241
+ * Is the buffer empty?
242
+ */
243
+ static VALUE
244
+ Coolio_Buffer_empty(VALUE self)
245
+ {
246
+ struct buffer *buf;
247
+ Data_Get_Struct(self, struct buffer, buf);
248
+
249
+ return buf->size > 0 ? Qfalse : Qtrue;
250
+ }
251
+
252
+ /**
253
+ * call-seq:
254
+ * Coolio::Buffer#append(data) -> String
255
+ *
256
+ * Append the given data to the end of the buffer
257
+ */
258
+ static VALUE
259
+ Coolio_Buffer_append(VALUE self, VALUE data)
260
+ {
261
+ struct buffer *buf;
262
+ Data_Get_Struct(self, struct buffer, buf);
263
+
264
+ /* Is this needed? Never seen anyone else do it... */
265
+ data = rb_convert_type(data, T_STRING, "String", "to_str");
266
+ buffer_append(buf, RSTRING_PTR(data), RSTRING_LEN(data));
267
+
268
+ return data;
269
+ }
270
+
271
+ /**
272
+ * call-seq:
273
+ * Coolio::Buffer#prepend(data) -> String
274
+ *
275
+ * Prepend the given data to the beginning of the buffer
276
+ */
277
+ static VALUE
278
+ Coolio_Buffer_prepend(VALUE self, VALUE data)
279
+ {
280
+ struct buffer *buf;
281
+ Data_Get_Struct(self, struct buffer, buf);
282
+
283
+ data = rb_convert_type(data, T_STRING, "String", "to_str");
284
+ buffer_prepend(buf, RSTRING_PTR(data), RSTRING_LEN(data));
285
+
286
+ return data;
287
+ }
288
+
289
+ /**
290
+ * call-seq:
291
+ * Coolio::Buffer#read(length = nil) -> String
292
+ *
293
+ * Read the specified abount of data from the buffer. If no value
294
+ * is given the entire contents of the buffer are returned. Any data
295
+ * read from the buffer is cleared.
296
+ * The given length must be greater than 0 or an exception would raise.
297
+ * If the buffer size is zero then an empty string is returned (regardless
298
+ * the given length).
299
+ */
300
+ static VALUE
301
+ Coolio_Buffer_read(int argc, VALUE * argv, VALUE self)
302
+ {
303
+ VALUE length_obj, str;
304
+ int length;
305
+ struct buffer *buf;
306
+
307
+ Data_Get_Struct(self, struct buffer, buf);
308
+
309
+ if (rb_scan_args(argc, argv, "01", &length_obj) == 1) {
310
+ length = NUM2INT(length_obj);
311
+ if(length < 1)
312
+ rb_raise(rb_eArgError, "length must be greater than zero");
313
+ if(length > buf->size)
314
+ length = buf->size;
315
+ } else
316
+ length = buf->size;
317
+
318
+ if(buf->size == 0)
319
+ return rb_str_new2("");
320
+
321
+ str = rb_str_new(0, length);
322
+ buffer_read(buf, RSTRING_PTR(str), length);
323
+
324
+ return str;
325
+ }
326
+
327
+ /**
328
+ * call-seq:
329
+ * Coolio::Buffer#read_frame(str, mark) -> boolean
330
+ *
331
+ * Read up to and including the given frame marker (expressed a a
332
+ * Fixnum 0-255) byte, copying into the supplied string object. If the mark is
333
+ * not encountered before the end of the buffer, false is returned but data
334
+ * is still copied into str. True is returned if the end of a frame is reached.
335
+ *
336
+ */
337
+ static VALUE
338
+ Coolio_Buffer_read_frame(VALUE self, VALUE data, VALUE mark)
339
+ {
340
+ char mark_c = (char) NUM2INT(mark);
341
+ struct buffer *buf;
342
+
343
+ Data_Get_Struct(self, struct buffer, buf);
344
+
345
+ if (buffer_read_frame(buf, data, mark_c)) {
346
+ return Qtrue;
347
+ } else {
348
+ return Qfalse;
349
+ }
350
+ }
351
+
352
+ /**
353
+ * call-seq:
354
+ * Coolio::Buffer#to_str -> String
355
+ *
356
+ * Convert the Buffer to a String. The original buffer is unmodified.
357
+ */
358
+ static VALUE
359
+ Coolio_Buffer_to_str(VALUE self)
360
+ {
361
+ VALUE str;
362
+ struct buffer *buf;
363
+
364
+ Data_Get_Struct(self, struct buffer, buf);
365
+
366
+ str = rb_str_new(0, buf->size);
367
+ buffer_copy(buf, RSTRING_PTR(str), buf->size);
368
+
369
+ return str;
370
+ }
371
+
372
+ /**
373
+ * call-seq:
374
+ * Coolio::Buffer#read_from(io) -> Integer
375
+ *
376
+ * Perform a nonblocking read of the the given IO object and fill
377
+ * the buffer with any data received. The call will read as much
378
+ * data as it can until the read would block.
379
+ */
380
+ static VALUE
381
+ Coolio_Buffer_read_from(VALUE self, VALUE io)
382
+ {
383
+ struct buffer *buf;
384
+ int ret;
385
+ #if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR)
386
+ rb_io_t *fptr;
387
+ #else
388
+ OpenFile *fptr;
389
+ #endif
390
+
391
+ Data_Get_Struct(self, struct buffer, buf);
392
+ io = rb_convert_type(io, T_FILE, "IO", "to_io");
393
+ GetOpenFile(io, fptr);
394
+ rb_io_set_nonblock(fptr);
395
+
396
+ #ifdef HAVE_RB_IO_DESCRIPTOR
397
+ ret = buffer_read_from(buf, rb_io_descriptor(io));
398
+ #else
399
+ ret = buffer_read_from(buf, FPTR_TO_FD(fptr));
400
+ #endif
401
+ return ret == -1 ? Qnil : INT2NUM(ret);
402
+ }
403
+
404
+ /**
405
+ * call-seq:
406
+ * Coolio::Buffer#write_to(io) -> Integer
407
+ *
408
+ * Perform a nonblocking write of the buffer to the given IO object.
409
+ * As much data as possible is written until the call would block.
410
+ * Any data which is written is removed from the buffer.
411
+ */
412
+ static VALUE
413
+ Coolio_Buffer_write_to(VALUE self, VALUE io)
414
+ {
415
+ struct buffer *buf;
416
+ #if defined(HAVE_RB_IO_T) || defined(HAVE_RB_IO_DESCRIPTOR)
417
+ rb_io_t *fptr;
418
+ #else
419
+ OpenFile *fptr;
420
+ #endif
421
+
422
+ Data_Get_Struct(self, struct buffer, buf);
423
+ io = rb_convert_type(io, T_FILE, "IO", "to_io");
424
+ GetOpenFile(io, fptr);
425
+ rb_io_set_nonblock(fptr);
426
+
427
+ #ifdef HAVE_RB_IO_DESCRIPTOR
428
+ return INT2NUM(buffer_write_to(buf, rb_io_descriptor(io)));
429
+ #else
430
+ return INT2NUM(buffer_write_to(buf, FPTR_TO_FD(fptr)));
431
+ #endif
432
+ }
433
+
434
+ /*
435
+ * Ruby bindings end here. Below is the actual implementation of
436
+ * the underlying byte queue ADT
437
+ */
438
+
439
+ /* Create a new buffer */
440
+ static struct buffer *
441
+ buffer_new(void)
442
+ {
443
+ struct buffer *buf;
444
+
445
+ buf = (struct buffer *) xmalloc(sizeof(struct buffer));
446
+ buf->head = buf->tail = buf->pool_head = buf->pool_tail = 0;
447
+ buf->size = 0;
448
+ buf->node_size = default_node_size;
449
+
450
+ return buf;
451
+ }
452
+
453
+ /* Clear all data from a buffer */
454
+ static void
455
+ buffer_clear(struct buffer * buf)
456
+ {
457
+ /* Move everything into the buffer pool */
458
+ if (!buf->pool_tail) {
459
+ buf->pool_head = buf->pool_tail = buf->head;
460
+ } else {
461
+ buf->pool_tail->next = buf->head;
462
+ }
463
+
464
+ buf->head = buf->tail = 0;
465
+ buf->size = 0;
466
+ }
467
+
468
+ /* Free a buffer */
469
+ static void
470
+ buffer_free(struct buffer * buf)
471
+ {
472
+ buffer_clear(buf);
473
+ buffer_free_pool(buf);
474
+
475
+ free(buf);
476
+ }
477
+
478
+ /* Free the memory pool */
479
+ static void
480
+ buffer_free_pool(struct buffer * buf)
481
+ {
482
+ struct buffer_node *tmp;
483
+
484
+ while (buf->pool_head) {
485
+ tmp = buf->pool_head;
486
+ buf->pool_head = tmp->next;
487
+ free(tmp);
488
+ }
489
+
490
+ buf->pool_tail = 0;
491
+ }
492
+
493
+ /* Create a new buffer_node (or pull one from the memory pool) */
494
+ static struct buffer_node *
495
+ buffer_node_new(struct buffer * buf)
496
+ {
497
+ struct buffer_node *node;
498
+
499
+ /* Pull from the memory pool if available */
500
+ if (buf->pool_head) {
501
+ node = buf->pool_head;
502
+ buf->pool_head = node->next;
503
+
504
+ if (node->next)
505
+ node->next = 0;
506
+ else
507
+ buf->pool_tail = 0;
508
+ } else {
509
+ node = (struct buffer_node *) xmalloc(sizeof(struct buffer_node) + buf->node_size);
510
+ node->next = 0;
511
+ }
512
+
513
+ node->start = node->end = 0;
514
+ return node;
515
+ }
516
+
517
+ /* Free a buffer node (i.e. return it to the memory pool) */
518
+ static void
519
+ buffer_node_free(struct buffer * buf, struct buffer_node * node)
520
+ {
521
+ node->next = buf->pool_head;
522
+ buf->pool_head = node;
523
+
524
+ if (!buf->pool_tail) {
525
+ buf->pool_tail = node;
526
+ }
527
+ }
528
+
529
+ /* Prepend data to the front of the buffer */
530
+ static void
531
+ buffer_prepend(struct buffer * buf, char *str, unsigned len)
532
+ {
533
+ struct buffer_node *node, *tmp;
534
+ buf->size += len;
535
+
536
+ /* If it fits in the beginning of the head */
537
+ if (buf->head && buf->head->start >= len) {
538
+ buf->head->start -= len;
539
+ memcpy(buf->head->data + buf->head->start, str, len);
540
+ } else {
541
+ node = buffer_node_new(buf);
542
+ node->next = buf->head;
543
+ buf->head = node;
544
+ if (!buf->tail)
545
+ buf->tail = node;
546
+
547
+ while (len > buf->node_size) {
548
+ memcpy(node->data, str, buf->node_size);
549
+ node->end = buf->node_size;
550
+
551
+ tmp = buffer_node_new(buf);
552
+ tmp->next = node->next;
553
+ node->next = tmp;
554
+
555
+ if (buf->tail == node)
556
+ buf->tail = tmp;
557
+ node = tmp;
558
+
559
+ str += buf->node_size;
560
+ len -= buf->node_size;
561
+ }
562
+
563
+ if (len > 0) {
564
+ memcpy(node->data, str, len);
565
+ node->end = len;
566
+ }
567
+ }
568
+ }
569
+
570
+ /* Append data to the front of the buffer */
571
+ static void
572
+ buffer_append(struct buffer * buf, char *str, unsigned len)
573
+ {
574
+ unsigned nbytes;
575
+ buf->size += len;
576
+
577
+ /* If it fits in the remaining space in the tail */
578
+ if (buf->tail && len <= buf->node_size - buf->tail->end) {
579
+ memcpy(buf->tail->data + buf->tail->end, str, len);
580
+ buf->tail->end += len;
581
+ return;
582
+ }
583
+ /* Empty list needs initialized */
584
+ if (!buf->head) {
585
+ buf->head = buffer_node_new(buf);
586
+ buf->tail = buf->head;
587
+ }
588
+ /* Build links out of the data */
589
+ while (len > 0) {
590
+ nbytes = buf->node_size - buf->tail->end;
591
+ if (len < nbytes)
592
+ nbytes = len;
593
+
594
+ memcpy(buf->tail->data + buf->tail->end, str, nbytes);
595
+ str += nbytes;
596
+ len -= nbytes;
597
+
598
+ buf->tail->end += nbytes;
599
+
600
+ if (len > 0) {
601
+ buf->tail->next = buffer_node_new(buf);
602
+ buf->tail = buf->tail->next;
603
+ }
604
+ }
605
+ }
606
+
607
+ /* Read data from the buffer (and clear what we've read) */
608
+ static void
609
+ buffer_read(struct buffer * buf, char *str, unsigned len)
610
+ {
611
+ unsigned nbytes;
612
+ struct buffer_node *tmp;
613
+
614
+ while (buf->size > 0 && len > 0) {
615
+ nbytes = buf->head->end - buf->head->start;
616
+ if (len < nbytes)
617
+ nbytes = len;
618
+
619
+ memcpy(str, buf->head->data + buf->head->start, nbytes);
620
+ str += nbytes;
621
+ len -= nbytes;
622
+
623
+ buf->head->start += nbytes;
624
+ buf->size -= nbytes;
625
+
626
+ if (buf->head->start == buf->head->end) {
627
+ tmp = buf->head;
628
+ buf->head = tmp->next;
629
+ buffer_node_free(buf, tmp);
630
+
631
+ if (!buf->head)
632
+ buf->tail = 0;
633
+ }
634
+ }
635
+ }
636
+
637
+ /*
638
+ * Read data from the buffer into str until byte frame_mark or empty. Bytes
639
+ * are copied into str and removed if a complete frame is read, a true value
640
+ * is returned
641
+ */
642
+ static int
643
+ buffer_read_frame(struct buffer * buf, VALUE str, char frame_mark)
644
+ {
645
+ unsigned nbytes = 0;
646
+ struct buffer_node *tmp;
647
+
648
+ while (buf->size > 0) {
649
+ struct buffer_node *head = buf->head;
650
+ char *loc, *s = head->data + head->start, *e = head->data + head->end;
651
+ nbytes = e - s;
652
+
653
+ loc = memchr(s, frame_mark, nbytes);
654
+
655
+ if (loc) {
656
+ nbytes = loc - s + 1;
657
+ }
658
+
659
+ /* Copy less than everything if we found a frame byte */
660
+ rb_str_cat(str, s, nbytes);
661
+
662
+ /* Fixup the buffer pointers to indicate the bytes were consumed */
663
+ head->start += nbytes;
664
+ buf->size -= nbytes;
665
+
666
+ if (head->start == head->end) {
667
+ buf->head = head->next;
668
+ buffer_node_free(buf, head);
669
+
670
+ if (!buf->head)
671
+ buf->tail = 0;
672
+ }
673
+
674
+ if (loc) {
675
+ return 1;
676
+ }
677
+ }
678
+
679
+ return 0;
680
+ }
681
+
682
+ /* Copy data from the buffer without clearing it */
683
+ static void
684
+ buffer_copy(struct buffer * buf, char *str, unsigned len)
685
+ {
686
+ unsigned nbytes;
687
+ struct buffer_node *node;
688
+
689
+ node = buf->head;
690
+ while (node && len > 0) {
691
+ nbytes = node->end - node->start;
692
+ if (len < nbytes)
693
+ nbytes = len;
694
+
695
+ memcpy(str, node->data + node->start, nbytes);
696
+ str += nbytes;
697
+ len -= nbytes;
698
+
699
+ if (node->start + nbytes == node->end)
700
+ node = node->next;
701
+ }
702
+ }
703
+
704
+ /* Write data from the buffer to a file descriptor */
705
+ static int
706
+ buffer_write_to(struct buffer * buf, int fd)
707
+ {
708
+ int bytes_written, total_bytes_written = 0;
709
+ struct buffer_node *tmp;
710
+
711
+ while (buf->head) {
712
+ bytes_written = write(fd, buf->head->data + buf->head->start, buf->head->end - buf->head->start);
713
+
714
+ /* If the write failed... */
715
+ if (bytes_written < 0) {
716
+ if (errno != EAGAIN)
717
+ rb_sys_fail("write");
718
+
719
+ return total_bytes_written;
720
+ }
721
+
722
+ total_bytes_written += bytes_written;
723
+ buf->size -= bytes_written;
724
+
725
+ /* If the write blocked... */
726
+ if (bytes_written < buf->head->end - buf->head->start) {
727
+ buf->head->start += bytes_written;
728
+ return total_bytes_written;
729
+ }
730
+ /* Otherwise we wrote the whole buffer */
731
+ tmp = buf->head;
732
+ buf->head = tmp->next;
733
+ buffer_node_free(buf, tmp);
734
+
735
+ if (!buf->head)
736
+ buf->tail = 0;
737
+ }
738
+
739
+ return total_bytes_written;
740
+ }
741
+
742
+ /* Read data from a file descriptor to a buffer */
743
+ /* Append data to the front of the buffer */
744
+ static int
745
+ buffer_read_from(struct buffer * buf, int fd)
746
+ {
747
+ int bytes_read, total_bytes_read = 0;
748
+ unsigned nbytes;
749
+
750
+ /* Empty list needs initialized */
751
+ if (!buf->head) {
752
+ buf->head = buffer_node_new(buf);
753
+ buf->tail = buf->head;
754
+ }
755
+
756
+ do {
757
+ nbytes = buf->node_size - buf->tail->end;
758
+ bytes_read = read(fd, buf->tail->data + buf->tail->end, nbytes);
759
+
760
+ if (bytes_read == 0) {
761
+ return -1;
762
+ //When the file reaches EOF
763
+ } else if (bytes_read < 0) {
764
+ if (errno != EAGAIN)
765
+ rb_sys_fail("read");
766
+
767
+ return total_bytes_read;
768
+ }
769
+
770
+ total_bytes_read += bytes_read;
771
+ buf->tail->end += bytes_read;
772
+ buf->size += bytes_read;
773
+
774
+ if (buf->tail->end == buf->node_size) {
775
+ buf->tail->next = buffer_node_new(buf);
776
+ buf->tail = buf->tail->next;
777
+ }
778
+ } while (bytes_read == nbytes);
779
+
780
+ return total_bytes_read;
781
+ }