Граф коммитов

35 Коммитов

Автор SHA1 Сообщение Дата
normal 1d9cf30d39 test/socket/test_nonblock: use smaller buffer for sendmsg
OpenBSD's limit is less than 128.

* test/socket/test_nonblock: use smaller buffer for sendmsg
  Patch-by: Jeremy Evans <code@jeremyevans.net>
  [ruby-core:70016] [Bug #11364]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51288 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-17 22:21:37 +00:00
nobu beb1c085d5 use Timeout.timeout
* time: Object#timeout has been deprecated a long time ago, use
  Timeout.timeout.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-13 10:07:01 +00:00
nobu c2c81723a4 test_nonblock.rb: use assert_predicate
* test/socket/test_nonblock.rb (test_recv_nonblock_no_exception),
  (test_recvfrom_nonblock_no_exception): use assert_predicate for
  failure messages.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50917 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-16 09:29:17 +00:00
nobu 85d5df5528 test_nonblock.rb: fix failure on OSX
* test/socket/test_nonblock.rb (test_recvfrom_nonblock_no_exception):
  no destination to already connected socket.  fix failure on OSX.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50916 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-16 09:26:26 +00:00
normal a02a3f4649 socket: allow explicit buffer for recv and recv_nonblock
This reduces GC overhead and makes the API more consistent
with IO#read and IO#read_nonblock.

* ext/socket/basicsocket.c (bsock_recv): document outbuf
* ext/socket/unixsocket.c (unix_recvfrom): ditto
* ext/socket/init.c (rsock_strbuf, recvfrom_locktmp): new functions
  (rsock_s_recvfrom): support destination buffer as 3rd arg
  (rsock_s_recvfrom_nonblock): ditto
* string.c (rb_str_locktmp_ensure): export for internal ext
* test/socket/test_nonblock.rb: test recv_nonblock
* test/socket/test_unix.rb: test recv
  [ruby-core:69543] [Feature #11242]

Benchmark results:

             user     system      total        real
alloc    0.130000   0.280000   0.410000 (  0.420656)
extbuf   0.100000   0.220000   0.320000 (  0.318708)

-------------------8<--------------------
require 'socket'
require 'benchmark'
nr = 100000
msg = ' ' * 16384
size = msg.bytesize
buf = ' ' * size
UNIXSocket.pair(:DGRAM) do |a, b|
  Benchmark.bmbm do |x|
    x.report('alloc') do
      nr.times do
        b.send(msg, 0)
        a.recv(size, 0)
      end
    end

    x.report('extbuf') do
      nr.times do
        b.send(msg, 0)
        a.recv(size, 0, buf)
      end
    end
  end
end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50912 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-15 20:02:43 +00:00
normal b9a91334c5 socket: allow exception-free nonblocking sendmsg/recvmsg
As documented before, exceptions are expensive and IO::Wait*able are too
common in socket applications to be the exceptional case.  Datagram
sockets deserve the same API which stream sockets are allowed with
read_nonblock and write_nonblock.

Note: this does not offer a performance advantage under optimal
conditions when both ends are equally matched in speed, but it it
does make debug output cleaner by avoiding exceptions whenever
the receiver slows down.

* ext/socket/ancdata.c (bsock_sendmsg_internal, bsock_recvmsg_internal):
  support "exception: false" kwarg
* ext/socket/init.c (rsock_s_recvfrom_nonblock):
  ditto
* ext/socket/init.c (rsock_s_recvfrom_nonblock): use rsock_opt_false_p
* ext/socket/socket.c (sock_connect_nonblock): ditto
* ext/socket/rubysocket.h (rsock_opt_false_p): new function
* ext/socket/basicsocket.c (bsock_recv_nonblock): update rdoc
* ext/socket/udpsocket.c (udp_recvfrom_nonblock): ditto
* test/socket/test_nonblock.rb: new tests

[ruby-core:69542] [Feature #11229]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50910 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-15 19:38:49 +00:00
normal 5d983e4fb2 test/socket/test_nonblock.rb: try to avoid EMSGSIZE
We want to test the IO::WaitWritable behavior, so silently
discarding Errno::EMSGSIZE prevents the test from being
effective.
[ruby-core:69466]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50788 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-05 23:55:23 +00:00
naruse d348051773 FreeBSD raises Errno::EMSGSIZE
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50766 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-04 03:25:04 +00:00
nobu ae042f21fb use assert_raise
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50727 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-02 02:18:44 +00:00
nobu 21f2c194bb test_nonblock.rb: skip if EPROTONOSUPPORT
* test/socket/test_nonblock.rb (test_sendmsg_nonblock_seqpacket):
  OSX raises EPROTONOSUPPORT.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50726 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-02 02:02:00 +00:00
normal 18e7743817 test/socket/test_nonblock.rb: new test for sendmsg_nonblock
sendmsg_nonblock was not tested on any of my systems due to the
common 64K limit.  I also don't believe UDP sockets are at all
a useful candidate for sendmsg_nonblock testing since they
should never block on sending.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50725 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-02 01:47:41 +00:00
nobu 903c0f55e9 test_nonblock.rb: refine failure message
* test/socket/test_nonblock.rb (test_accept_nonblock): refine
  assertion for better failure message.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50530 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-05-18 09:41:37 +00:00
normal 8ff35b816a ext/socket/init.c: use SOCK_NONBLOCK if available
This saves a system call by allowing us to use SOCK_NONBLOCK in
Linux when accept4 is available.

Note: I do not agree accept_nonblock should always make accepted
sockets non-blocking, and will propose a future API to allow
controlling whether accepted sockets are non-blocking or not
regardless of how they were created.

* ext/socket/init.c (cloexec_accept): support nonblock flag and
  use SOCK_NONBLOCK if possible
* ext/socket/init.c (rsock_s_accept_nonblock): update cloexec_accept call
* ext/socket/init.c (accept_blocking): ditto for blocking
* test/socket/test_nonblock.rb: check nonblock? on accepted socket
  [Feature #11138]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50518 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-05-17 05:56:07 +00:00
normal b88296be43 connect_nonblock(..., exception: false) does not raise EISCONN
* ext/socket/socket.c (sock_connect_nonblock): do not raise EISCONN
  [ruby-core:68926] [Feature #11072]
* test/socket/test_nonblock.rb: check non-EISCONN on 2nd connect

This is to reduce exceptions for code which issues a
(IMHO, unnecessary) second connect() syscall.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50347 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-04-20 02:11:10 +00:00
normal c18df6d87c connect_nonblock supports "exception: false"
This is for consistency with accept_nonblock arguments and gives a
minor speedup from avoiding exceptions.
[ruby-core:68838] [Feature #11024]

* ext/openssl/ossl_ssl.c (ossl_ssl_connect_nonblock):
  support `exception: false'
* (get_no_exception): move function location
* ext/socket/socket.c (sock_connect_nonblock):
  support `exception: false'
* test/openssl/test_pair.rb (test_connect_accept_nonblock_no_exception):
  test `exception: false' on connect,
  rename from `test_accept_nonblock_no_exception'
* test/socket/test_nonblock.rb (test_connect_nonblock_no_exception):
  new test

Benchmark results:

default            0.050000   0.100000   0.150000 (  0.151307)
exception: false   0.030000   0.080000   0.110000 (  0.108840)

----------------------------8<-----------------------
require 'socket'
require 'benchmark'
require 'io/wait'
require 'tmpdir'

host = '127.0.0.1'
serv = TCPServer.new(host, 0) # UNIX sockets may not hit EINPROGRESS

nr = 5000 # few iterations to avoid running out of ports

addr = serv.getsockname
pid = fork do
  begin
    serv.accept.close
  rescue => e
    warn "#$$: #{e.message} (#{e.class})"
  end while true
end
at_exit { Process.kill(:TERM, pid) }
serv.close

Benchmark.bmbm do |x|
  x.report("default") do
    nr.times do
      s = Socket.new(:INET, :STREAM)
      s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
      begin
        s.connect_nonblock(addr)
      rescue IO::WaitWritable
        s.wait_writable
      end
      s.close
    end
  end
  x.report("exception: false") do
    nr.times do
      s = Socket.new(:INET, :STREAM)
      s.setsockopt(:SOL_SOCKET, :SO_REUSEADDR, 1)
      case s.connect_nonblock(addr, exception: false)
      when :wait_writable
        s.wait_writable
      end
      s.close
    end
  end
end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50254 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-04-12 01:41:51 +00:00
normal aaf2d070a8 accept_nonblock supports "exception: false"
This is analogous to functionality found in IO#read_nonblock and
IO#wait_nonblock.  Raising exceptions for common failures on
non-blocking servers is expensive and makes $DEBUG too noisy.

Benchmark results:
                                    user     system      total        real
default                         2.790000   0.870000   3.660000 (  3.671597)
exception: false                1.120000   0.800000   1.920000 (  1.922032)
exception: false (cached arg)   0.820000   0.770000   1.590000 (  1.589267)
--------------------- benchmark script ------------------------
require 'socket'
require 'benchmark'
require 'tmpdir'
nr = 1000000
Dir.mktmpdir('nb_bench') do |path|
  sock_path = "#{path}/test.sock"
  s = UNIXServer.new(sock_path)
  Benchmark.bmbm do |x|
    x.report("default") do
      nr.times do
        begin
          s.accept_nonblock
        rescue IO::WaitReadable
        end
      end
    end
    x.report("exception: false") do
      nr.times do
        begin
          s.accept_nonblock(exception: false)
        rescue IO::WaitReadable
          abort "should not raise"
        end
      end
    end
    x.report("exception: false (cached arg)") do
      arg = { exception: false }
      nr.times do
        begin
          s.accept_nonblock(arg)
        rescue IO::WaitReadable
          abort "should not raise"
        end
      end
    end
  end
end

* ext/socket/init.c (rsock_s_accept_nonblock):
  support exception: false
  [ruby-core:66385] [Feature #10532]
* ext/socket/init.c (rsock_init_socket_init): define new symbols
* ext/socket/rubysocket.h: adjust prototype
* ext/socket/socket.c (sock_accept_nonblock): support exception: false
* ext/openssl/ossl_ssl.c (ossl_ssl_accept_nonblock): ditto
* ext/socket/socket.c (Init_socket): adjust accept_nonblock definition
* ext/openssl/ossl_ssl.c (Init_ossl_ssl): ditto
* ext/socket/tcpserver.c (rsock_init_tcpserver): ditto
* ext/socket/unixserver.c (rsock_init_unixserver): ditto
* ext/socket/tcpserver.c (tcp_accept_nonblock): adjust
  rsock_s_accept_nonblock call
* ext/socket/unixserver.c (unix_accept_nonblock): ditto
* ext/openssl/ossl_ssl.c (ossl_start_ssl): support no_exception
* ext/openssl/ossl_ssl.c (ossl_ssl_connect): adjust ossl_start_ssl call
* ext/openssl/ossl_ssl.c (ossl_ssl_connect_nonblock): ditto
* ext/openssl/ossl_ssl.c (ossl_ssl_accept): ditto
* test/socket/test_nonblock.rb (test_accept_nonblock): test for
  "exception :false"
* test/socket/test_tcp.rb (test_accept_nonblock): new test
* test/socket/test_unix.rb (test_accept_nonblock): ditto
* test/openssl/test_pair.rb (test_accept_nonblock_no_exception): ditto

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49948 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-03-12 03:03:04 +00:00
hsbt 93156392dd * test/socket/test_addrinfo.rb: remove unused variables.
* test/socket/test_nonblock.rb: ditto.
* test/socket/test_socket.rb: ditto.
* test/socket/test_unix.rb: ditto.
* test/testunit/test_parallel.rb: ditto.
* test/webrick/test_filehandler.rb: ditto.
* test/xmlrpc/test_features.rb: ditto.
* test/zlib/test_zlib.rb: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45034 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2014-02-18 12:05:09 +00:00
tenderlove 988ca60565 * io.c (io_read_nonblock): support non-blocking reads without raising
exceptions. As in: `io.read_nonblock(size, exception: false)`
  [ruby-core:38666] [Feature #5138]
* ext/openssl/ossl_ssl.c (ossl_ssl_read_internal): ditto
* ext/stringio/stringio.c (strio_sysread): ditto
* io.c (rb_io_write_nonblock): support non-blocking writes without
  raising an exception.
* ext/openssl/ossl_ssl.c (ossl_ssl_write_internal): ditto
* test/openssl/test_pair.rb (class OpenSSL): tests
* test/ruby/test_io.rb (class TestIO): ditto
* test/socket/test_nonblock.rb (class TestSocketNonblock): ditto
* test/stringio/test_stringio.rb (class TestStringIO): ditto

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42695 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-08-26 22:41:44 +00:00
akr adb575e21e * ext/socket/ancdata.c (bsock_sendmsg_internal): Always set
controls_num to raise NotImplementedError appropriately.
  (bsock_recvmsg_internal): Raise NotImplementedError if
  :scm_rights=>true is given on platforms which don't have
  4.4BSD style control message.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40624 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-05-09 11:48:29 +00:00
akr 3a4231cdf4 * test/socket/test_socket.rb (test_udp_server): rescue Errno::ENOSYS
for Windows.

* test/socket/test_nonblock.rb (test_sendmsg_nonblock_error): ditto.

  [ruby-dev:41597] reported by Masaya Tarui.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28320 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2010-06-14 13:46:06 +00:00
akr 666049981f Use UDP for sendmsg/recvmsg tests.
sendmsg/recvmsg doesn't work with SOCK_STREAM on Windows.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24204 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-07-19 08:53:11 +00:00
usa 229b4e4c37 * ext/socket/socket.c (socket_s_ip_address_list): drop inactive
adapters.

	* test/socket/test_{nonblock,addrinfo,socket}.rb: skip some tests on
	  Windows.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24173 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-07-17 13:40:59 +00:00
akr 758f9510a6 use IO::WaitWritable.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-19 12:45:40 +00:00
akr c4049f4cb6 * io.c (rb_mWaitReadable): defined.
(rb_mWaitWritable): defined.
  (io_getpartial): extend IO::WaitReadable on EWOULDBLOCK and EAGAIN.
  (rb_io_write_nonblock): extend IO::WaitWritable on EWOULDBLOCK and
  EAGAIN.

* error.c (make_errno_exc): extracted from rb_sys_fail.
  (rb_mod_sys_fail): new function.

* include/ruby/ruby.h (rb_mod_sys_fail): declared.
  (rb_mWaitReadable): declared.
  (rb_mWaitWritable): declared.

* ext/socket/init.c (rsock_s_recvfrom_nonblock): extend
  IO::WaitReadable on EWOULDBLOCK and EAGAIN.
  (rsock_s_accept_nonblock): extend IO::WaitReadable on EWOULDBLOCK,
  EAGAIN, ECONNABORTED and EPROTO.

* ext/socket/socket.c (sock_connect_nonblock): extend IO::WaitWritable
  on EINPROGRESS.

* ext/socket/ancdata.c (bsock_sendmsg_internal): extend
  IO::WaitWritable on EWOULDBLOCK and EAGAIN.
  (bsock_recvmsg_internal): extend IO::WaitReadable on EWOULDBLOCK and
  EAGAIN.

* ext/openssl/ossl_ssl.c (ossl_ssl_read_internal): raise SSLError
  extended by IO::WaitReadable/IO::WaitWritable on
  SSL_ERROR_WANT_READ/SSL_ERROR_WANT_WRITE.

* ext/openssl/ossl.c (ossl_make_error): extracted from ossl_raise.
  (ossl_exc_new): new function.

* ext/openssl/ossl.h (ossl_exc_new): declared.

* lib/net/protocol.rb (rbuf_fill): rescue IO::WaitReadable and
  IO::WaitWritable.

  [ruby-core:22539], [ruby-dev:38140] 



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23006 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-19 11:40:38 +00:00
nobu 287a34ae0d * {ext,lib,test}/**/*.rb: removed trailing spaces.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-06 03:56:38 +00:00
akr 9cfe35ad50 * io.c (io_getpartial): error message describes what should be
waited after nonblocking error.
  (rb_io_write_nonblock): ditto.

* ext/socket/init.c (s_recvfrom_nonblock): ditto.
  (s_accept_nonblock): ditto.

* ext/socket/socket.c (sock_connect_nonblock): ditto.

* ext/socket/ancdata.c (bsock_sendmsg_internal): ditto.
  (bsock_recvmsg_internal): ditto.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22486 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-21 15:57:52 +00:00
akr 536adb5d7c care EWOULDBLOCK of accept_nonblock.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22375 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-17 11:05:35 +00:00
akr a8de15e0b5 rename tests classes.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-02-11 04:17:57 +00:00
akr b8d19fa683 don't compare struct sockaddr directly.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10591 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-07-24 07:21:45 +00:00
akr 8a15f88901 add a note for cygwin empty UDP packet problem.
This commit is a result of hacks at the CodeFest Akihabara 2006 hosted by FSIJ.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10443 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-07-01 07:27:55 +00:00
akr ef908a25d8 * test/socket/test_nonblock.rb: add timeout to send/receive
an empty UDP packet.
  [ruby-dev:28820]


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-07-01 06:23:21 +00:00
akr b73e6801e3 * ext/socket/socket.c (bsock_recv_nonblock): new method
BasicSocket#recv_nonblock.
  (udp_recvfrom_nonblock): renamed from ip_recvfrom_nonblock.
  IPSocket#recvfrom_nonblock is moved to UDPSocket#recvfrom_nonblock.
  (unix_recvfrom_nonblock): removed.
  UNIXSocket#recvfrom_nonblock is removed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10403 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-26 16:39:59 +00:00
usa af19c66570 * test/socket/{test_nonblock.rb, test_socket.rb}: Windows support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10228 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-07 06:44:24 +00:00
akr 9fcf44697a fix tests.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10216 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-04 12:15:01 +00:00
akr 5f4ad759f1 * ext/socket/socket.c (s_recvfrom): alen may be zero with UNIXSocket
too.  (tested on NetBSD 3.0)
  (s_recvfrom_nonblock): extracted from sock_recvfrom_nonblock.
  (sock_recvfrom_nonblock): use s_recvfrom_nonblock.
  (ip_recvfrom_nonblock): new method: IPSocket#recvfrom_nonblock
  (unix_recvfrom_nonblock): new method: UNIXSocket#recvfrom_nonblock
  (s_accept_nonblock): extracted from sock_accept_nonblock.
  (sock_accept_nonblock): use s_accept_nonblock.
  (tcp_accept_nonblock): new method: TCPServer#accept_nonblock
  (unix_accept_nonblock): new method: UNIXServer#accept_nonblock


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10203 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2006-06-01 15:33:09 +00:00