Module: Kgio

Defined in:
lib/kgio.rb,
ext/kgio/accept.c,
ext/kgio/autopush.c,
ext/kgio/connect.c,
ext/kgio/kgio_ext.c,
ext/kgio/poll.c,
ext/kgio/read_write.c,
ext/kgio/wait.c,
ext/kgio/tryopen.c

Overview

See the README

Defined Under Namespace

Modules: DefaultWaiters, PipeMethods, SocketMethods Classes: File, Pipe, Socket, TCPServer, TCPSocket, UNIXServer, UNIXSocket

Constant Summary collapse

LOCALHOST =

The IPv4 address of UNIX domain sockets, useful for creating Rack (and CGI) servers that also serve HTTP traffic over UNIX domain sockets.

'127.0.0.1'
WaitReadable =

Kgio::PipeMethods#kgio_tryread and Kgio::SocketMethods#kgio_tryread will return :wait_readable when waiting for a read is required.

:wait_readable
WaitWritable =

PipeMethods#kgio_trywrite and SocketMethods#kgio_trywrite will return :wait_writable when waiting for a read is required.

:wait_writable
SOCK_NONBLOCK =

Maps to the SOCK_NONBLOCK constant in Linux for setting the non-blocking flag on newly accepted sockets. This is usually unnecessary as sockets are made non-blocking whenever non-blocking methods are used.

INT2NUM(SOCK_NONBLOCK)
SOCK_CLOEXEC =

Maps to the SOCK_CLOEXEC constant in Linux for setting the close-on-exec flag on newly accepted descriptors. This is enabled by default, and there is usually no reason to disable close-on-exec for accepted sockets.

INT2NUM(SOCK_CLOEXEC)
TCP_FASTOPEN =
INT2NUM(TCP_FASTOPEN)
MSG_FASTOPEN =
INT2NUM(MSG_FASTOPEN)

Class Method Summary collapse

Class Method Details

.accept_classObject

Returns the default class for newly accepted sockets when kgio_accept or kgio_tryaccept are not passed arguments



56
57
58
59
# File 'ext/kgio/accept.c', line 56

static VALUE get_accepted(VALUE klass)
{
	return cClientSocket;
}

.accept_class=(aclass) ⇒ Object

Sets the default class for newly accepted sockets. This is legacy behavior, kgio_accept and kgio_tryaccept now take optional class arguments to override this value.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/kgio/accept.c', line 33

static VALUE set_accepted(VALUE klass, VALUE aclass)
{
	VALUE tmp;

	if (NIL_P(aclass))
		aclass = cKgio_Socket;

	tmp = rb_funcall(aclass, rb_intern("included_modules"), 0, 0);
	tmp = rb_funcall(tmp, rb_intern("include?"), 1, mSocketMethods);

	if (tmp != Qtrue)
		rb_raise(rb_eTypeError,
		         "class must include Kgio::SocketMethods");

	cClientSocket = aclass;

	return aclass;
}

.accept_cloexec=(boolean) ⇒ Object

Kgio.accept_cloexec = true Kgio.accept_cloexec = false

Sets whether or not Kgio::Socket objects created by TCPServer#kgio_accept, TCPServer#kgio_tryaccept, UNIXServer#kgio_accept, and UNIXServer#kgio_tryaccept default to being created with the FD_CLOEXEC file descriptor flag.

This is on by default, as there is little reason to deal to enable it for client sockets on a socket server.

Deprecated, use the per-socket flags for kgio_*accept instead.



426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'ext/kgio/accept.c', line 426

static VALUE set_cloexec(VALUE mod, VALUE boolean)
{
	switch (TYPE(boolean)) {
	case T_TRUE:
		accept4_flags |= SOCK_CLOEXEC;
		return boolean;
	case T_FALSE:
		accept4_flags &= ~SOCK_CLOEXEC;
		return boolean;
	}
	rb_raise(rb_eTypeError, "not true or false");
	return Qnil;
}

.accept_cloexec?Boolean

Kgio.accept_cloexec? -> true or false

Returns true if newly accepted Kgio::Sockets are created with the FD_CLOEXEC file descriptor flag, false if not.

Deprecated, use the per-socket flags for kgio_*accept instead.

Returns:

  • (Boolean)


387
388
389
390
# File 'ext/kgio/accept.c', line 387

static VALUE get_cloexec(VALUE mod)
{
	return (accept4_flags & SOCK_CLOEXEC) == SOCK_CLOEXEC ? Qtrue : Qfalse;
}

.accept_nonblock=(boolean) ⇒ Object

Kgio.accept_nonblock = true Kgio.accept_nonblock = false

Sets whether or not Kgio::Socket objects created by TCPServer#kgio_accept, TCPServer#kgio_tryaccept, UNIXServer#kgio_accept, and UNIXServer#kgio_tryaccept are created with the O_NONBLOCK file status flag.

This defaults to false for GNU/Linux where MSG_DONTWAIT is available (and on newer GNU/Linux, accept4() may also set the non-blocking flag. This defaults to true on non-GNU/Linux systems.

This is always true on Ruby implementations using user-space threads.

Deprecated, use the per-socket flags for kgio_*accept instead.



462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'ext/kgio/accept.c', line 462

static VALUE set_nonblock(VALUE mod, VALUE boolean)
{
	switch (TYPE(boolean)) {
	case T_TRUE:
		accept4_flags |= SOCK_NONBLOCK;
		return boolean;
	case T_FALSE:
		accept4_flags &= ~SOCK_NONBLOCK;
		return boolean;
	}
	rb_raise(rb_eTypeError, "not true or false");
	return Qnil;
}

.accept_nonblock?Boolean

Kgio.accept_nonblock? -> true or false

Returns true if newly accepted Kgio::Sockets are created with the O_NONBLOCK file status flag, false if not.

Deprecated, use the per-socket flags for kgio_*accept instead.

Returns:

  • (Boolean)


403
404
405
406
# File 'ext/kgio/accept.c', line 403

static VALUE get_nonblock(VALUE mod)
{
	return (accept4_flags & SOCK_NONBLOCK)==SOCK_NONBLOCK ? Qtrue : Qfalse;
}

.autopush=(val) ⇒ Object

Kgio.autopush = true Kgio.autopush = false

Enables or disables autopush for sockets created with kgio_accept and kgio_tryaccept methods. Autopush relies on TCP_CORK/TCP_NOPUSH being enabled on the listen socket.

Only available on systems with TCP_CORK (Linux) or TCP_NOPUSH (FreeBSD, and maybe other *BSDs).



108
109
110
111
112
113
# File 'ext/kgio/autopush.c', line 108

static VALUE s_set_autopush(VALUE self, VALUE val)
{
	enabled = RTEST(val);

	return val;
}

.autopush?Boolean

Kgio.autopush? -> true or false

Returns whether or not autopush is enabled.

Only available on systems with TCP_CORK (Linux) or TCP_NOPUSH (FreeBSD, and maybe other *BSDs).

Returns:

  • (Boolean)


91
92
93
94
# File 'ext/kgio/autopush.c', line 91

static VALUE s_get_autopush(VALUE self)
{
	return enabled ? Qtrue : Qfalse;
}

.poll(*args) ⇒ Object

Kgio.poll({ $stdin => :wait_readable }, 100) -> hash or nil Kgio.poll({ $stdin => Kgio::POLLIN }, 100) -> hash or nil

Accepts an input hash with IO objects to wait for as the key and the events to wait for as its value. The events may either be :wait_readable or :wait_writable symbols or a Fixnum mask of Kgio::POLL* constants:

Kgio::POLLIN - there is data to read Kgio::POLLPRI - there is urgent data to read Kgio::POLLOUT - writing will not block Kgio::POLLRDHUP - peer has shutdown writes (Linux 2.6.17+ only)

Timeout is specified in Integer milliseconds just like the underlying poll(2), not in seconds like IO.select. A nil timeout means to wait forever. It must be an Integer or nil.

Kgio.poll modifies and returns its input hash on success with the IO-like object as the key and an Integer mask of events as the hash value. It can return any of the events specified in the input above, along with the following events:

Kgio::POLLERR - error condition occurred on the descriptor Kgio::POLLHUP - hang up Kgio::POLLNVAL - invalid request (bad file descriptor)

This method is only available under Ruby 1.9 or any other implementations that uses native threads and rb_thread_blocking_region()



196
197
198
199
200
201
202
203
204
205
206
207
# File 'ext/kgio/poll.c', line 196

static VALUE s_poll(int argc, VALUE *argv, VALUE self)
{
	VALUE timeout;
	struct poll_args a;

	rb_scan_args(argc, argv, "11", &a.ios, &timeout);
	a.timeout = num2timeout(timeout);
	a.fds = NULL;
	a.fd_to_io = NULL;

	return rb_ensure(do_poll, (VALUE)&a, poll_free, (VALUE)&a);
}

.trypeek(*args) ⇒ Object

Kgio.trypeek(socket, maxlen) -> buffer Kgio.trypeek(socket, maxlen, buffer) -> buffer

Like Kgio.tryread, except it uses MSG_PEEK so it does not drain the socket buffer. This can only be used on sockets and not pipe objects. Maybe used in place of SocketMethods#kgio_trypeek for non-Kgio objects



326
327
328
329
330
331
# File 'ext/kgio/read_write.c', line 326

static VALUE s_trypeek(int argc, VALUE *argv, VALUE mod)
{
	if (argc <= 1)
		rb_raise(rb_eArgError, "wrong number of arguments");
	return my_peek(0, argc - 1, &argv[1], argv[0]);
}

.tryread(*args) ⇒ Object

Kgio.tryread(io, maxlen) -> buffer Kgio.tryread(io, maxlen, buffer) -> buffer

Returns nil on EOF. Returns :wait_readable if EAGAIN is encountered.

Maybe used in place of PipeMethods#kgio_tryread for non-Kgio objects



733
734
735
736
737
738
# File 'ext/kgio/read_write.c', line 733

static VALUE s_tryread(int argc, VALUE *argv, VALUE mod)
{
	if (argc <= 1)
		rb_raise(rb_eArgError, "wrong number of arguments");
	return my_read(0, argc - 1, &argv[1], argv[0]);
}

.trywrite(io, str) ⇒ Object

Kgio.trywrite(io, 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.

Maybe used in place of PipeMethods#kgio_trywrite for non-Kgio objects



755
756
757
758
# File 'ext/kgio/read_write.c', line 755

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

.trywritev(io, ary) ⇒ Object

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

Returns nil if the write was completed in full.

Returns a 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.

Maybe used in place of PipeMethods#kgio_trywritev for non-Kgio objects



775
776
777
778
# File 'ext/kgio/read_write.c', line 775

static VALUE s_trywritev(VALUE mod, VALUE io, VALUE ary)
{
	return kgio_trywritev(io, ary);
}