Module: Kgio::PipeMethods

Includes:
Waiters
Included in:
File, Pipe
Defined in:
ext/kgio/read_write.c,
ext/kgio/read_write.c

Overview

This module may be used used to create classes that respond to various Kgio methods for reading and writing. This is included in Kgio::Pipe by default.

Instance Method Summary collapse

Instance Method Details

#kgio_read(*args) ⇒ Object

io.kgio_read(maxlen) -> buffer io.kgio_read(maxlen, buffer) -> buffer

Reads at most maxlen bytes from the stream socket. Returns with a newly allocated buffer, or may reuse an existing buffer if supplied.

This may block and call any method defined to kgio_wait_readable for the class.

Returns nil on EOF.

This behaves like read(2) and IO#readpartial, NOT fread(3) or IO#read which possess read-in-full behavior.



175
176
177
178
# File 'ext/kgio/read_write.c', line 175

static VALUE kgio_read(int argc, VALUE *argv, VALUE io)
{
	return my_read(1, argc, argv, io);
}

#kgio_read!(*args) ⇒ Object

Same as Kgio::PipeMethods#kgio_read, except EOFError is raised on EOF without a backtrace. This method is intended as a drop-in replacement for places where IO#readpartial is used, and may be aliased as such.



186
187
188
189
190
191
192
# File 'ext/kgio/read_write.c', line 186

static VALUE kgio_read_bang(int argc, VALUE *argv, VALUE io)
{
	VALUE rv = my_read(1, argc, argv, io);

	if (NIL_P(rv)) my_eof_error();
	return rv;
}

#kgio_tryread(*args) ⇒ Object

io.kgio_tryread(maxlen) -> buffer io.kgio_tryread(maxlen, buffer) -> buffer

Reads at most maxlen bytes from the stream socket. Returns with a newly allocated buffer, or may reuse an existing buffer if supplied.

Returns nil on EOF.

Returns :wait_readable if EAGAIN is encountered.



207
208
209
210
# File 'ext/kgio/read_write.c', line 207

static VALUE kgio_tryread(int argc, VALUE *argv, VALUE io)
{
	return my_read(0, argc, argv, io);
}

#kgio_trywrite(str) ⇒ Object

io.kgio_trywrite(str) -> nil, String or :wait_writable

Returns nil if the write was completed in full.

Returns a String containing the unwritten portion if EAGAIN was encountered, but some portion was successfully written.

Returns :wait_writable if EAGAIN is encountered and nothing was written.



425
426
427
428
# File 'ext/kgio/read_write.c', line 425

static VALUE kgio_trywrite(VALUE io, VALUE str)
{
	return my_write(io, str, 0);
}

#kgio_trywritev(ary) ⇒ Object

io.kgio_trywritev(array) -> nil, Array or :wait_writable

Returns nil if the write was completed in full.

Returns an Array of strings containing the unwritten portion if EAGAIN was encountered, but some portion was successfully written.

Returns :wait_writable if EAGAIN is encountered and nothing was written.

Note: it uses Array() semantic for converting argument, so that it will succeed if you pass something else.



672
673
674
675
# File 'ext/kgio/read_write.c', line 672

static VALUE kgio_trywritev(VALUE io, VALUE ary)
{
	return my_writev(io, ary, 0);
}

#kgio_write(str) ⇒ Object

io.kgio_write(str) -> nil

Returns nil when the write completes.

This may block and call any method defined to kgio_wait_writable for the class.



407
408
409
410
# File 'ext/kgio/read_write.c', line 407

static VALUE kgio_write(VALUE io, VALUE str)
{
	return my_write(io, str, 1);
}

#kgio_writev(ary) ⇒ Object

io.kgio_writev(array) -> nil

Returns nil when the write completes.

This may block and call any method defined to kgio_wait_writable for the class.

Note: it uses Array() semantic for converting argument, so that it will succeed if you pass something else.



651
652
653
654
# File 'ext/kgio/read_write.c', line 651

static VALUE kgio_writev(VALUE io, VALUE ary)
{
	return my_writev(io, ary, 1);
}