Project

General

Profile

Feature #11139 ยป 0002-socket-accept_nonblock-supports-nonblock-false-kwarg.patch

normalperson (Eric Wong), 05/12/2015 12:14 AM

View differences:

ext/socket/init.c
29 29
#endif
30 30

  
31 31
int rsock_do_not_reverse_lookup = 1;
32
static VALUE sym_exception, sym_wait_readable;
32
static VALUE sym_exception, sym_wait_readable, sym_nonblock;
33 33

  
34 34
void
35 35
rsock_raise_socket_error(const char *reason, int error)
......
526 526
{
527 527
    int fd2;
528 528
    int ex = 1;
529
    int nonblock = 1;
529 530
    VALUE opts = Qnil;
530 531

  
531 532
    rb_scan_args(argc, argv, "0:", &opts);
532 533

  
533
    if (!NIL_P(opts) && Qfalse == rb_hash_lookup2(opts, sym_exception, Qundef))
534
	ex = 0;
534
    if (!NIL_P(opts)) {
535
        if (Qfalse == rb_hash_lookup2(opts, sym_exception, Qundef))
536
            ex = 0;
537
        if (Qfalse == rb_hash_lookup2(opts, sym_nonblock, Qundef))
538
            nonblock = 0;
539
    }
535 540

  
536 541
    rb_secure(3);
537 542
    rb_io_set_nonblock(fptr);
538
    fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len, 1);
543
    fd2 = cloexec_accept(fptr->fd, (struct sockaddr*)sockaddr, len, nonblock);
539 544
    if (fd2 < 0) {
540 545
	switch (errno) {
541 546
	  case EAGAIN:
......
639 644
#undef rb_intern
640 645
    sym_exception = ID2SYM(rb_intern("exception"));
641 646
    sym_wait_readable = ID2SYM(rb_intern("wait_readable"));
647
    sym_nonblock = ID2SYM(rb_intern("nonblock"));
642 648
}
test/socket/test_nonblock.rb
27 27
    assert_equal(Socket.unpack_sockaddr_in(c.getsockname), Socket.unpack_sockaddr_in(sockaddr))
28 28
    if s.respond_to?(:nonblock?)
29 29
      assert s.nonblock?, 'accepted socket is non-blocking'
30

  
31
      tf = [ true, false ]
32
      assert_equal(tf, tf.map do |nb|
33
        begin
34
          b = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
35
          b.connect(serv.getsockname)
36
          IO.select([serv])
37
          a, _= serv.accept_nonblock(nonblock: nb)
38
          a.nonblock?
39
        ensure
40
          a.close if a
41
          b.close if b
42
        end
43
      end)
30 44
    end
31 45
  ensure
32 46
    serv.close if serv
33
-