Project

General

Profile

Feature #13362 ยป 0001-socket-avoid-fcntl-for-read-write_nonblock-on-Linux.patch

normalperson (Eric Wong), 03/24/2017 05:51 AM

View differences:

ext/socket/lib/socket.rb
442 442
                       scm_rights: false, exception: true)
443 443
    __recvmsg_nonblock(dlen, flags, clen, scm_rights, exception)
444 444
  end
445

  
446
  # Linux-specific optimizations to avoid fcntl for IO#read_nonblock
447
  # and IO#write_nonblock using MSG_DONTWAIT
448
  # Do other platforms suport MSG_DONTWAIT reliably?
449
  if RUBY_PLATFORM =~ /linux/ && Socket.const_defined?(:MSG_DONTWAIT)
450
    def read_nonblock(len, str = nil, exception: true) # :nodoc:
451
      case rv = __recv_nonblock(len, 0, str, exception)
452
      when '' # recv_nonblock returns empty string on EOF
453
        exception ? raise(EOFError, 'end of file reached') : nil
454
      else
455
        rv
456
      end
457
    end
458

  
459
    def write_nonblock(buf, exception: true) # :nodoc:
460
      __sendmsg_nonblock(buf, 0, nil, nil, exception)
461
    end
462
  end
445 463
end
446 464

  
447 465
class Socket < BasicSocket
test/socket/test_basicsocket.rb
152 152
      sock.close
153 153
    end
154 154
  end
155

  
156
  def test_read_write_nonblock
157
    socks do |sserv, ssock, csock|
158
      buf = String.new
159
      assert_equal :wait_readable, ssock.read_nonblock(1, buf, exception: false)
160
      assert_equal 5, csock.write_nonblock('hello')
161
      IO.select([ssock])
162
      assert_same buf, ssock.read_nonblock(5, buf, exception: false)
163
      assert_equal 'hello', buf
164
      buf = '*' * 16384
165
      n = 0
166

  
167
      case w = csock.write_nonblock(buf, exception: false)
168
      when Integer
169
        n += w
170
      when :wait_writable
171
        break
172
      end while true
173

  
174
      assert_equal :wait_writable, w
175
      assert_raise(IO::WaitWritable) { loop { csock.write_nonblock(buf) } }
176
      assert_operator n, :>, 0
177
      csock.close
178

  
179
      case r = ssock.read_nonblock(16384, buf, exception: false)
180
      when String
181
        next
182
      when nil
183
        break
184
      else
185
        flunk "unexpected read_nonblock return: #{r.inspect}"
186
      end while true
187

  
188
      assert_raise(EOFError) { ssock.read_nonblock(1) }
189
    end
190
  end
155 191
end if defined?(BasicSocket)
156
-