summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-10 12:09:57 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-10 12:09:57 +0000
commit98d3eca7fc092e0237f6cfd06bb5cf58d3796d36 (patch)
treec7d8c95cbcb3c0a0954a4eb4ab05a2469d3da5e9
parent7be3cdbc0443e007ab5aa294d216e086c8a8af7e (diff)
* ext/socket/option.c (sockopt_s_bool): new method.
(sockopt_bool): new method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22210 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/socket/option.c49
-rw-r--r--test/socket/test_sockopt.rb17
3 files changed, 71 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 672dac5802..11f7f58bb2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Tue Feb 10 21:09:23 2009 Tanaka Akira <akr@fsij.org>
+
+ * ext/socket/option.c (sockopt_s_bool): new method.
+ (sockopt_bool): new method.
+
Tue Feb 10 20:58:47 2009 Tanaka Akira <akr@fsij.org>
* ext/socket/option.c (sockopt_family_m): renamed from sockopt_family.
diff --git a/ext/socket/option.c b/ext/socket/option.c
index afd90e560a..87fecacd00 100644
--- a/ext/socket/option.c
+++ b/ext/socket/option.c
@@ -163,6 +163,52 @@ sockopt_int(VALUE self)
return INT2NUM(i);
}
+/*
+ * call-seq:
+ * Socket::Option.bool(family, level, optname, bool) => sockopt
+ *
+ * Creates a new Socket::Option object which contains boolean as data.
+ * Actually 0 or 1 as int is used.
+ *
+ * p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true)
+ * #=> #<Socket::Option: INET SOCKET KEEPALIVE 1>
+ *
+ * p Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false)
+ * #=> #<Socket::Option: AF_INET SOCKET KEEPALIVE 0>
+ *
+ */
+static VALUE
+sockopt_s_bool(VALUE klass, VALUE vfamily, VALUE vlevel, VALUE voptname, VALUE vbool)
+{
+ int family = family_arg(vfamily);
+ int level = level_arg(family, vlevel);
+ int optname = optname_arg(family, level, voptname);
+ int i = RTEST(vbool) ? 1 : 0;
+ return sockopt_new(family, level, optname, rb_str_new((char*)&i, sizeof(i)));
+}
+
+/*
+ * call-seq:
+ * sockopt.bool => true or false
+ *
+ * Returns the data in _sockopt_ as an boolean value.
+ *
+ * sockopt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
+ * p sockopt.bool => true
+ */
+static VALUE
+sockopt_bool(VALUE self)
+{
+ int i;
+ VALUE data = sockopt_data(self);
+ StringValue(data);
+ if (RSTRING_LEN(data) != sizeof(int))
+ rb_raise(rb_eTypeError, "size differ. expected as sizeof(int)=%d but %ld",
+ (int)sizeof(int), (long)RSTRING_LEN(data));
+ memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+ return i == 0 ? Qfalse : Qtrue;
+}
+
static int
inspect_int(int level, int optname, VALUE data, VALUE ret)
{
@@ -496,6 +542,9 @@ Init_sockopt(void)
rb_define_singleton_method(rb_cSockOpt, "int", sockopt_s_int, 4);
rb_define_method(rb_cSockOpt, "int", sockopt_int, 0);
+ rb_define_singleton_method(rb_cSockOpt, "bool", sockopt_s_bool, 4);
+ rb_define_method(rb_cSockOpt, "bool", sockopt_bool, 0);
+
rb_define_method(rb_cSockOpt, "unpack", sockopt_unpack, 1);
rb_define_method(rb_cSockOpt, "to_s", sockopt_data, 0); /* compatibility for ruby before 1.9.2 */
diff --git a/test/socket/test_sockopt.rb b/test/socket/test_sockopt.rb
new file mode 100644
index 0000000000..06575f9cc3
--- /dev/null
+++ b/test/socket/test_sockopt.rb
@@ -0,0 +1,17 @@
+require 'test/unit'
+require 'socket'
+
+class TestSockOpt < Test::Unit::TestCase
+ def test_bool
+ opt = Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true)
+ assert_equal(1, opt.int)
+ opt = Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false)
+ assert_equal(0, opt.int)
+ opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 0)
+ assert_equal(false, opt.bool)
+ opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 1)
+ assert_equal(true, opt.bool)
+ opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 2)
+ assert_equal(true, opt.bool)
+ end
+end