[macruby-changes] [5105] MacRuby/trunk/ext/socket/socket.c
source_changes at macosforge.org
source_changes at macosforge.org
Mon Jan 3 00:44:04 PST 2011
Revision: 5105
http://trac.macosforge.org/projects/ruby/changeset/5105
Author: watson1978 at gmail.com
Date: 2011-01-03 00:44:00 -0800 (Mon, 03 Jan 2011)
Log Message:
-----------
Socket.pair will yield the sockets when given a block.
Test Script:
{{{
require 'socket'
require 'test/unit/assertions.rb'
include Test::Unit::Assertions
sock1 = sock2 = nil
sock3 = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM) do |s1, s2|
sock1 = s1
sock2 = s2
end
assert_match(/#<Socket.+>/, sock1.inspect)
assert_match(/#<Socket.+>/, sock2.inspect)
assert_match(/#<Socket.+>/, sock3.inspect)
puts :ok
}}}
Modified Paths:
--------------
MacRuby/trunk/ext/socket/socket.c
Modified: MacRuby/trunk/ext/socket/socket.c
===================================================================
--- MacRuby/trunk/ext/socket/socket.c 2011-01-03 08:43:54 UTC (rev 5104)
+++ MacRuby/trunk/ext/socket/socket.c 2011-01-03 08:44:00 UTC (rev 5105)
@@ -2271,13 +2271,35 @@
return init_sock(sock, fd);
}
+#if defined HAVE_SOCKETPAIR
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));
+}
+#endif
+
+static VALUE
sock_s_socketpair(VALUE klass, SEL sel, int argc, VALUE *argv)
{
#if defined HAVE_SOCKETPAIR
VALUE domain, type, protocol;
int d, t, p, sp[2];
int ret;
+ VALUE s1, s2, r;
+
rb_scan_args(argc, argv, "21", &domain, &type, &protocol);
if (NIL_P(protocol))
protocol = INT2FIX(0);
@@ -2293,8 +2315,13 @@
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20110103/a5ccf973/attachment.html>
More information about the macruby-changes
mailing list