diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-31 15:05:16 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-31 15:05:16 +0000 |
commit | 46d690d89133e89fe7505095e4d0f97f6aebd02c (patch) | |
tree | 61f7846d781aa023205dc33d3c8af5f68ae3d717 /ext/socket/socket.c | |
parent | 81ddb2bbc5c1857d76ce1ed28db9ec3997d0abbb (diff) | |
download | ruby-46d690d89133e89fe7505095e4d0f97f6aebd02c.tar.gz ruby-46d690d89133e89fe7505095e4d0f97f6aebd02c.tar.xz ruby-46d690d89133e89fe7505095e4d0f97f6aebd02c.zip |
* ext/socket/socket.c (sock_s_socketpair): yield if a block is given.
(io_call_close): defined.
(io_close): defined.
(pair_yield): defined.
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@21216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/socket/socket.c')
-rw-r--r-- | ext/socket/socket.c | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/ext/socket/socket.c b/ext/socket/socket.c index b9de752d7..11976b01a 100644 --- a/ext/socket/socket.c +++ b/ext/socket/socket.c @@ -2385,11 +2385,30 @@ sock_initialize(VALUE sock, VALUE domain, VALUE type, VALUE protocol) } static VALUE +io_call_close(VALUE io) +{ + return rb_funcall(io, rb_intern("close"), 0, 0); +} + +static VALUE +io_close(VALUE io) +{ + return rb_rescue(io_call_close, io, 0, 0); +} + +static VALUE +pair_yield(VALUE pair) +{ + return rb_ensure(rb_yield, pair, io_close, rb_ary_entry(pair, 1)); +} + +static VALUE sock_s_socketpair(VALUE klass, VALUE domain, VALUE type, VALUE protocol) { #if defined HAVE_SOCKETPAIR int d, t, p, sp[2]; int ret; + VALUE s1, s2, r; setup_domain_and_type(domain, &d, type, &t); p = NUM2INT(protocol); @@ -2402,8 +2421,13 @@ sock_s_socketpair(VALUE klass, VALUE domain, VALUE type, VALUE protocol) rb_sys_fail("socketpair(2)"); } - return rb_assoc_new(init_sock(rb_obj_alloc(klass), sp[0]), - init_sock(rb_obj_alloc(klass), sp[1])); + s1 = init_sock(rb_obj_alloc(klass), sp[0]); + s2 = init_sock(rb_obj_alloc(klass), sp[1]); + r = rb_assoc_new(s1, s2); + if (rb_block_given_p()) { + return rb_ensure(pair_yield, r, io_close, s1); + } + return r; #else rb_notimplement(); #endif |