Method: PG::Connection#socket_io

Defined in:
ext/pg_connection.c

#socket_ioObject

Fetch a memorized IO object created from the Connection's underlying socket. This object can be used for IO.select to wait for events while running asynchronous API calls.

Using this instead of #socket avoids the problem of the underlying connection being closed by Ruby when an IO created using IO.for_fd(conn.socket) goes out of scope. In contrast to #socket, it also works on Windows.



885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'ext/pg_connection.c', line 885

static VALUE
pgconn_socket_io(VALUE self)
{
	int sd;
	int ruby_sd;
	ID id_autoclose = rb_intern("autoclose=");
	t_pg_connection *this = pg_get_connection_safe( self );
	VALUE socket_io = this->socket_io;

	if ( !RTEST(socket_io) ) {
		if( (sd = PQsocket(this->pgconn)) < 0)
			rb_raise(rb_eConnectionBad, "PQsocket() can't get socket descriptor");

		#ifdef _WIN32
			ruby_sd = rb_w32_wrap_io_handle((HANDLE)(intptr_t)sd, O_RDWR|O_BINARY|O_NOINHERIT);
			this->ruby_sd = ruby_sd;
		#else
			ruby_sd = sd;
		#endif

		socket_io = rb_funcall( rb_cIO, rb_intern("for_fd"), 1, INT2NUM(ruby_sd) );

		/* Disable autoclose feature */
		rb_funcall( socket_io, id_autoclose, 1, Qfalse );

		this->socket_io = socket_io;
	}

	return socket_io;
}