about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2012-12-13 00:02:50 +0000
committerEric Wong <normalperson@yhbt.net>2012-12-13 00:09:53 +0000
commit7a3fc55424338ad458cc719d4cb3c4e28802d5cb (patch)
treee2d30f94072e448c6b71736b97a5cb2f99b9afe3
parent48fc432a3b9dfd2b0435f0975556d4a321a5239b (diff)
downloadkgio-7a3fc55424338ad458cc719d4cb3c4e28802d5cb.tar.gz
This allows us to create an unconnected socket, just like
the normal Socket class it inherits from.
-rw-r--r--ext/kgio/connect.c18
-rw-r--r--test/test_socket.rb14
2 files changed, 31 insertions, 1 deletions
diff --git a/ext/kgio/connect.c b/ext/kgio/connect.c
index 083a8f2..42ab44c 100644
--- a/ext/kgio/connect.c
+++ b/ext/kgio/connect.c
@@ -263,6 +263,21 @@ static VALUE kgio_connect(VALUE klass, VALUE addr)
         return stream_connect(klass, addr, 1);
 }
 
+/*
+ * 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 ])
+ */
+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);
+}
+
 /* call-seq:
  *
  *      addr = Socket.pack_sockaddr_in(80, 'example.com')
@@ -298,7 +313,8 @@ void init_kgio_connect(void)
          */
         cKgio_Socket = rb_define_class_under(mKgio, "Socket", cSocket);
         rb_include_module(cKgio_Socket, mSocketMethods);
-        rb_define_singleton_method(cKgio_Socket, "new", kgio_connect, 1);
+        rb_define_singleton_method(cKgio_Socket, "new", kgio_new, -1);
+        rb_define_singleton_method(cKgio_Socket, "connect", kgio_connect, 1);
         rb_define_singleton_method(cKgio_Socket, "start", kgio_start, 1);
 
         /*
diff --git a/test/test_socket.rb b/test/test_socket.rb
new file mode 100644
index 0000000..a85f0cc
--- /dev/null
+++ b/test/test_socket.rb
@@ -0,0 +1,14 @@
+require 'test/unit'
+require 'kgio'
+
+class TestKgioSocket < Test::Unit::TestCase
+  def test_socket_args
+    s = Kgio::Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
+    assert_kind_of Socket, s
+    assert_instance_of Kgio::Socket, s
+
+    s = Kgio::Socket.new(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
+    assert_kind_of Socket, s
+    assert_instance_of Kgio::Socket, s
+  end
+end