[前][次][番号順一覧][スレッド一覧]

ruby-changes:10674

From: akr <ko1@a...>
Date: Wed, 11 Feb 2009 17:35:48 +0900 (JST)
Subject: [ruby-changes:10674] Ruby:r22237 (trunk): * ext/socket/lib/socket.rb (Socket.tcp_server_sockets): call the block

akr	2009-02-11 17:35:35 +0900 (Wed, 11 Feb 2009)

  New Revision: 22237

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=22237

  Log:
    * ext/socket/lib/socket.rb (Socket.tcp_server_sockets): call the block
      if given.  close the sockets when the block exits.
      (Socket.tcp_server_loop): use tcp_server_sockets in block form.

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/lib/socket.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 22236)
+++ ChangeLog	(revision 22237)
@@ -1,3 +1,9 @@
+Wed Feb 11 17:34:16 2009  Tanaka Akira  <akr@f...>
+
+	* ext/socket/lib/socket.rb (Socket.tcp_server_sockets): call the block
+	  if given.  close the sockets when the block exits.
+	  (Socket.tcp_server_loop): use tcp_server_sockets in block form.
+
 Wed Feb 11 17:01:52 2009  Tanaka Akira  <akr@f...>
 
 	* ext/socket/lib/socket.rb (Socket.unix_server_loop): use
Index: ext/socket/lib/socket.rb
===================================================================
--- ext/socket/lib/socket.rb	(revision 22236)
+++ ext/socket/lib/socket.rb	(revision 22237)
@@ -279,8 +279,13 @@
   # creates TCP server sockets for _host_ and _port_.
   # _host_ is optional.
   #
-  # It returns an array of listening sockets.
+  # If a block is not given,
+  # it returns an array of listening sockets.
   #
+  # If a block is given, the block is called with the sockets.
+  # The value of the block is returned.
+  # The socket is closed when this method returns.
+  #
   # If _port_ is 0, actual port number is choosen dynamically.
   # However all sockets in the result has same port number.
   #
@@ -299,30 +304,42 @@
   #   #=> #<Addrinfo: [::]:53114 TCP>
   #   #   #<Addrinfo: 0.0.0.0:53114 TCP>
   #
+  #   # The block is called with the sockets.
+  #   Socket.tcp_server_sockets(0) {|sockets|
+  #     p sockets #=> [#<Socket:fd 3>, #<Socket:fd 4>]
+  #   }
+  #
   def self.tcp_server_sockets(host=nil, port)
-    return tcp_server_sockets_port0(host) if port == 0
-    begin
-      last_error = nil
-      sockets = []
-      Addrinfo.foreach(host, port, nil, :STREAM, nil, Socket::AI_PASSIVE) {|ai|
-        begin
-          s = ai.listen
-        rescue SystemCallError
-          last_error = $!
-          next
+    if port == 0
+      sockets = tcp_server_sockets_port0(host)
+    else
+      begin
+        last_error = nil
+        sockets = []
+        Addrinfo.foreach(host, port, nil, :STREAM, nil, Socket::AI_PASSIVE) {|ai|
+          begin
+            s = ai.listen
+          rescue SystemCallError
+            last_error = $!
+            next
+          end
+          sockets << s
+        }
+        if sockets.empty?
+          raise last_error
         end
-        sockets << s
-      }
-      if sockets.empty?
-        raise last_error
+      ensure
+        sockets.each {|s| s.close if !s.closed? } if $!
       end
+    end
+    if block_given?
+      begin
+        yield sockets
+      ensure
+        sockets.each {|s| s.close if !s.closed? }
+      end
+    else
       sockets
-    ensure
-      if $!
-        sockets.each {|s|
-          s.close if !s.closed?
-        }
-      end
     end
   end
 
@@ -395,14 +412,9 @@
   #   }
   #
   def self.tcp_server_loop(host=nil, port, &b) # :yield: socket, client_addrinfo
-    sockets = tcp_server_sockets(host, port)
-    accept_loop(sockets, &b)
-  ensure
-    if sockets
-      sockets.each {|s|
-        s.close if !s.closed?
-      }
-    end
+    tcp_server_sockets(host, port) {|sockets|
+      accept_loop(sockets, &b)
+    }
   end
 
   # :call-seq:
@@ -593,7 +605,7 @@
 
   # creates UNIX server sockets on _path_
   #
-  # It returns a listening socket.
+  # If a block is not given, it returns a listening socket.
   #
   # If a block is given, it is called with the socket and the block value is returned.
   # When the block exits, the socket is closed and the socket file is removed.

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]