ext/kgio/kgio_ext.c in kgio-1.0.1 vs ext/kgio/kgio_ext.c in kgio-1.1.0
- old
+ new
@@ -30,10 +30,12 @@
static int accept4_flags = A4_SOCK_CLOEXEC;
#else /* ! linux */
static int accept4_flags = A4_SOCK_CLOEXEC | A4_SOCK_NONBLOCK;
#endif /* ! linux */
+static VALUE cClientSocket;
+static VALUE mSocketMethods;
static VALUE cSocket;
static VALUE localhost;
static VALUE mKgio_WaitReadable, mKgio_WaitWritable;
static ID io_wait_rd, io_wait_wr;
static ID iv_kgio_addr;
@@ -531,11 +533,11 @@
if (errno == EINTR)
goto retry;
rb_sys_fail("accept");
}
}
- return sock_for_fd(cSocket, client);
+ return sock_for_fd(cClientSocket, client);
}
static void in_addr_set(VALUE io, struct sockaddr_in *addr)
{
VALUE host = rb_str_new(0, INET_ADDRSTRLEN);
@@ -948,14 +950,38 @@
static VALUE kgio_start(VALUE klass, VALUE addr)
{
return stream_connect(klass, addr, 0);
}
+static VALUE set_accepted(VALUE klass, VALUE aclass)
+{
+ VALUE tmp;
+
+ if (NIL_P(aclass))
+ aclass = cSocket;
+
+ 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;
+}
+
+static VALUE get_accepted(VALUE klass)
+{
+ return cClientSocket;
+}
+
void Init_kgio_ext(void)
{
VALUE mKgio = rb_define_module("Kgio");
- VALUE mPipeMethods, mSocketMethods;
+ VALUE mPipeMethods;
VALUE cUNIXServer, cTCPServer, cUNIXSocket, cTCPSocket;
rb_require("socket");
/*
@@ -965,10 +991,11 @@
* This is returned by all Kgio methods that accept(2) a connected
* stream socket.
*/
cSocket = rb_const_get(rb_cObject, rb_intern("Socket"));
cSocket = rb_define_class_under(mKgio, "Socket", cSocket);
+ cClientSocket = cSocket;
localhost = rb_str_new2("127.0.0.1");
/*
* The IPv4 address of UNIX domain sockets, useful for creating
@@ -999,9 +1026,11 @@
rb_define_singleton_method(mKgio, "wait_writable", wait_wr, 0);
rb_define_singleton_method(mKgio, "accept_cloexec?", get_cloexec, 0);
rb_define_singleton_method(mKgio, "accept_cloexec=", set_cloexec, 1);
rb_define_singleton_method(mKgio, "accept_nonblock?", get_nonblock, 0);
rb_define_singleton_method(mKgio, "accept_nonblock=", set_nonblock, 1);
+ rb_define_singleton_method(mKgio, "accept_class=", set_accepted, 1);
+ rb_define_singleton_method(mKgio, "accept_class", get_accepted, 0);
/*
* Document-module: Kgio::PipeMethods
*
* This module may be used used to create classes that respond to