Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lib/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ class << self
__bind_method__ :recvfrom, :Socket_recvfrom
__bind_method__ :sysaccept, :Socket_sysaccept, 0

def ipv6only!
setsockopt(:IPV6, :V6ONLY, 1) if defined?(Socket::IPV6_V6ONLY)
end

class << self
__bind_method__ :pair, :Socket_pair
__bind_method__ :pack_sockaddr_in, :Socket_pack_sockaddr_in
Expand Down Expand Up @@ -394,15 +398,25 @@ def ip?
ipv4? || ipv6?
end

def listen
socket = Socket.new(afamily, socktype, protocol)
client = Socket.for_fd(socket.listen(0))
return client unless block_given?

def listen(backlog = Socket::SOMAXCONN)
sock = Socket.new(pfamily, socktype, protocol)
begin
yield(client)
ensure
client.close
sock.ipv6only! if ipv6?
sock.setsockopt(:SOCKET, :REUSEADDR, 1) if pfamily != Socket::PF_UNIX
sock.bind(self)
sock.listen(backlog)
rescue Exception
sock.close
raise
end
if block_given?
begin
yield sock
ensure
sock.close
end
else
sock
end
end

Expand Down
Loading