Class: Cosmos::UdpReadSocket
Overview
Creates a UDPSocket and implements a non-blocking read.
Instance Method Summary collapse
-
#initialize(recv_port = 0, multicast_address = nil, interface_address = nil, bind_address = "0.0.0.0") ⇒ UdpReadSocket
constructor
A new instance of UdpReadSocket.
-
#method_missing(method, *args, &block) ⇒ Object
Defer all methods to the UDPSocket.
- #read(read_timeout = nil) ⇒ Object
Constructor Details
#initialize(recv_port = 0, multicast_address = nil, interface_address = nil, bind_address = "0.0.0.0") ⇒ UdpReadSocket
Returns a new instance of UdpReadSocket.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/cosmos/io/udp_sockets.rb', line 113 def initialize(recv_port = 0, multicast_address = nil, interface_address = nil, bind_address = "0.0.0.0") @socket = UDPSocket.new # Basic setup to reuse address @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) # bind to port @socket.bind(bind_address, recv_port) if UdpWriteSocket.multicast?(multicast_address) interface_address = "0.0.0.0" unless interface_address membership = IPAddr.new(multicast_address).hton + IPAddr.new(interface_address).hton @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, membership) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Defer all methods to the UDPSocket
147 148 149 |
# File 'lib/cosmos/io/udp_sockets.rb', line 147 def method_missing(method, *args, &block) @socket.__send__(method, *args, &block) end |
Instance Method Details
#read(read_timeout = nil) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cosmos/io/udp_sockets.rb', line 131 def read(read_timeout = nil) data = nil begin data, _ = @socket.recvfrom_nonblock(65536) rescue Errno::EAGAIN, Errno::EWOULDBLOCK result = IO.fast_select([@socket], nil, nil, read_timeout) if result retry else raise Timeout::Error, "Read Timeout" end end data end |