Class: Kgio::Socket

Inherits:
Socket
  • Object
show all
Includes:
SocketMethods
Defined in:
ext/kgio/connect.c,
ext/kgio/connect.c

Overview

A generic socket class with Kgio::SocketMethods included. This is returned by all Kgio methods that accept(2) a connected stream socket.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.addr=(Socket) ⇒ Object

Kgio::Socket.connect(addr) -> socket

addr = Socket.pack_sockaddr_un("/path/to/unix/socket")

Kgio::Socket.connect(addr) -> socket

Creates a generic Kgio::Socket object and initiates a non-blocking connection.

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



328
329
330
331
# File 'ext/kgio/connect.c', line 328

static VALUE kgio_connect(VALUE klass, VALUE addr)
{
	return stream_connect(klass, addr, 1);
}

.new(*args) ⇒ Object

If passed one argument, this is identical to Kgio::Socket.connect. If passed two or three arguments, it uses its superclass method:

Socket.new(domain, socktype [, protocol ])


339
340
341
342
343
344
345
346
# File 'ext/kgio/connect.c', line 339

static VALUE kgio_new(int argc, VALUE *argv, VALUE klass)
{
	if (argc == 1)
		/* backwards compat, the only way for kgio <= 2.7.4 */
		return stream_connect(klass, argv[0], 1);

	return rb_call_super(argc, argv);
}

.addr=(Socket) ⇒ Object

Kgio::Socket.start(addr) -> socket

addr = Socket.pack_sockaddr_un("/path/to/unix/socket")

Kgio::Socket.start(addr) -> socket

Creates a generic Kgio::Socket object and initiates a non-blocking connection. The caller should select/poll on the socket for writability before attempting to write or optimistically attempt a write and handle :wait_writable or Errno::EAGAIN.



362
363
364
365
# File 'ext/kgio/connect.c', line 362

static VALUE kgio_start(VALUE klass, VALUE addr)
{
	return stream_connect(klass, addr, 0);
}

Instance Method Details

#kgio_fastopen(buf, addr) ⇒ Object

s = Kgio::Socket.new(:INET, :STREAM) addr = Socket.pack_sockaddr_in(80, “example.com”) s.kgio_fastopen(“hello world”, addr) -> nil

Starts a TCP connection using TCP Fast Open. This uses a blocking sendto() syscall and is only available on Ruby 1.9 or later. This raises exceptions (including Errno::EINPROGRESS/Errno::EAGAIN) on errors. Using this is only recommended for blocking sockets.

Timeouts may be set with setsockopt:

s.setsockopt(:SOCKET, :SNDTIMEO, [1,0].pack(“l_l_”))



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'ext/kgio/connect.c', line 185

static VALUE fastopen(VALUE sock, VALUE buf, VALUE addr)
{
	struct tfo_args a;
	VALUE str = (TYPE(buf) == T_STRING) ? buf : rb_obj_as_string(buf);
	ssize_t w;

	a.fd = my_fileno(sock);
	a.buf = RSTRING_PTR(str);
	a.buflen = (size_t)RSTRING_LEN(str);
	a.addr = sockaddr_from(&a.addrlen, addr);

	/* n.b. rb_thread_blocking_region preserves errno */
	w = (ssize_t)rb_thread_io_blocking_region(tfo_sendto, &a, a.fd);
	if (w < 0)
		rb_sys_fail("sendto");
	if ((size_t)w == a.buflen)
		return Qnil;

	return rb_str_subseq(str, w, a.buflen - w);
}