mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* ext/socket/lib/socket.rb (Socket.udp_server_recv): extracted from
Socket.udp_server_loop_on. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
866c79e2de
commit
abd31dc2ad
2 changed files with 47 additions and 19 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Fri Oct 9 00:01:17 2009 Tanaka Akira <akr@fsij.org>
|
||||||
|
|
||||||
|
* ext/socket/lib/socket.rb (Socket.udp_server_recv): extracted from
|
||||||
|
Socket.udp_server_loop_on.
|
||||||
|
|
||||||
Thu Oct 8 05:45:14 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
Thu Oct 8 05:45:14 2009 NARUSE, Yui <naruse@ruby-lang.org>
|
||||||
|
|
||||||
* tool/enc-unicode.rb: parse range notation of UnicodeData.txt.
|
* tool/enc-unicode.rb: parse range notation of UnicodeData.txt.
|
||||||
|
|
|
@ -552,6 +552,46 @@ class Socket
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# :call-seq:
|
||||||
|
# Socket.udp_server_recv(sockets) {|msg, msg_src| ... }
|
||||||
|
#
|
||||||
|
# Receive UDP/IP packets from the given _sockets_.
|
||||||
|
# For each packet received, the block is called.
|
||||||
|
#
|
||||||
|
# The block receives _msg_ and _msg_src_.
|
||||||
|
# _msg_ is a string which is the payload of the received packet.
|
||||||
|
# _msg_src_ is a Socket::UDPSource object which is used for reply.
|
||||||
|
#
|
||||||
|
# Socket.udp_server_loop can be implemented using this method as follows.
|
||||||
|
#
|
||||||
|
# udp_server_sockets(host, port) {|sockets|
|
||||||
|
# loop {
|
||||||
|
# readable, _, _ = IO.select(sockets)
|
||||||
|
# udp_server_recv(readable) {|msg, msg_src| ... }
|
||||||
|
# }
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
def self.udp_server_recv(sockets)
|
||||||
|
sockets.each {|r|
|
||||||
|
begin
|
||||||
|
msg, sender_addrinfo, rflags, *controls = r.recvmsg_nonblock
|
||||||
|
rescue IO::WaitReadable
|
||||||
|
next
|
||||||
|
end
|
||||||
|
ai = r.local_address
|
||||||
|
if ai.ipv6? and pktinfo = controls.find {|c| c.cmsg_is?(:IPV6, :PKTINFO) }
|
||||||
|
ai = Addrinfo.udp(pktinfo.ipv6_pktinfo_addr.ip_address, ai.ip_port)
|
||||||
|
yield msg, UDPSource.new(sender_addrinfo, ai) {|reply_msg|
|
||||||
|
r.sendmsg reply_msg, 0, sender_addrinfo, pktinfo
|
||||||
|
}
|
||||||
|
else
|
||||||
|
yield msg, UDPSource.new(sender_addrinfo, ai) {|reply_msg|
|
||||||
|
r.send reply_msg, 0, sender_addrinfo
|
||||||
|
}
|
||||||
|
end
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
# :call-seq:
|
# :call-seq:
|
||||||
# Socket.udp_server_loop_on(sockets) {|msg, msg_src| ... }
|
# Socket.udp_server_loop_on(sockets) {|msg, msg_src| ... }
|
||||||
#
|
#
|
||||||
|
@ -561,27 +601,10 @@ class Socket
|
||||||
#
|
#
|
||||||
# It calls the block for each message received.
|
# It calls the block for each message received.
|
||||||
#
|
#
|
||||||
def self.udp_server_loop_on(sockets) # :yield: msg, msg_src
|
def self.udp_server_loop_on(sockets, &b) # :yield: msg, msg_src
|
||||||
loop {
|
loop {
|
||||||
readable, _, _ = IO.select(sockets)
|
readable, _, _ = IO.select(sockets)
|
||||||
readable.each {|r|
|
udp_server_recv(sockets, &b)
|
||||||
begin
|
|
||||||
msg, sender_addrinfo, rflags, *controls = r.recvmsg_nonblock
|
|
||||||
rescue IO::WaitReadable
|
|
||||||
next
|
|
||||||
end
|
|
||||||
ai = r.local_address
|
|
||||||
if ai.ipv6? and pktinfo = controls.find {|c| c.cmsg_is?(:IPV6, :PKTINFO) }
|
|
||||||
ai = Addrinfo.udp(pktinfo.ipv6_pktinfo_addr.ip_address, ai.ip_port)
|
|
||||||
yield msg, UDPSource.new(sender_addrinfo, ai) {|reply_msg|
|
|
||||||
r.sendmsg reply_msg, 0, sender_addrinfo, pktinfo
|
|
||||||
}
|
|
||||||
else
|
|
||||||
yield msg, UDPSource.new(sender_addrinfo, ai) {|reply_msg|
|
|
||||||
r.send reply_msg, 0, sender_addrinfo
|
|
||||||
}
|
|
||||||
end
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue