1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
akr 2009-02-10 12:09:57 +00:00
parent 7be3cdbc04
commit 98d3eca7fc
3 changed files with 71 additions and 0 deletions

View file

@ -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.

View file

@ -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 */

View file

@ -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