Module: Raptor::VectorIO

Defined in:
ext/raptor_native/raptor_native.c

Class Method Summary collapse

Class Method Details

.writev_nonblock(io, strings) ⇒ Object



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
# File 'ext/raptor_native/raptor_native.c', line 18

static VALUE raptor_native_writev_nonblock(VALUE self, VALUE io, VALUE strings) {
  (void)self;
  Check_Type(strings, T_ARRAY);
  long len = RARRAY_LEN(strings);
  if (len == 0) return LONG2NUM(0);
  if (len > IOV_MAX) len = IOV_MAX;

  int fd = rb_io_descriptor(io);
  struct iovec *iov = alloca(len * sizeof(struct iovec));

  for (long i = 0; i < len; i++) {
    VALUE str = RARRAY_AREF(strings, i);
    Check_Type(str, T_STRING);
    iov[i].iov_base = RSTRING_PTR(str);
    iov[i].iov_len = RSTRING_LEN(str);
  }

  ssize_t written;
  while ((written = writev(fd, iov, (int)len)) < 0) {
    if (errno == EINTR) continue;
    if (errno == EAGAIN || errno == EWOULDBLOCK) rb_raise(eEAGAINWaitWritable, "writev would block");
    rb_sys_fail("writev");
  }

  return LONG2NUM((long)written);
}