Class: Socketry::UDP::Socket
- Inherits:
-
Object
- Object
- Socketry::UDP::Socket
- Includes:
- Timeout
- Defined in:
- lib/socketry/udp/socket.rb
Overview
User Datagram Protocol sockets
Constant Summary
Constants included from Timeout
Timeout::DEFAULT_TIMEOUTS, Timeout::DEFAULT_TIMER
Instance Attribute Summary collapse
-
#addr_family ⇒ Object
readonly
Returns the value of attribute addr_family.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
-
#socket_class ⇒ Object
readonly
Returns the value of attribute socket_class.
-
#write_timeout ⇒ Object
readonly
Returns the value of attribute write_timeout.
Class Method Summary collapse
-
.bind(local_addr, local_port, resolver: Socketry::Resolver::DEFAULT_RESOLVER) ⇒ Socketry::UDP::Socket
Create a UDP server bound to the given address and port.
-
.connect(remote_addr, remote_port, resolver: Socketry::Resolver::DEFAULT_RESOLVER) ⇒ Socketry::UDP::Socket
Connect to the given address and port.
-
.from_addr(remote_addr, resolver: Socketry::Resolver::DEFAULT_RESOLVER) ⇒ Socketry::UDP::Socket
Create a UDP socket matching the given socket’s address family.
Instance Method Summary collapse
-
#bind(local_addr, local_port) ⇒ self
Start a UDP server bound to a particular address and port.
-
#close ⇒ true, false
Close the socket.
-
#closed? ⇒ true, false
Is the socket closed?.
-
#connect(remote_addr, remote_port) ⇒ self
Make a UDP client connection to the given address and port.
-
#initialize(addr_family: :ipv4, read_timeout: Socketry::Timeout::DEFAULT_TIMEOUTS[:read], write_timeout: Socketry::Timeout::DEFAULT_TIMEOUTS[:write], timer: Socketry::Timeout::DEFAULT_TIMER.new, resolver: Socketry::Resolver::DEFAULT_RESOLVER, socket_class: ::UDPSocket) ⇒ Socketry::UDP::Socket
constructor
Create a new UDP socket.
-
#recvfrom(maxlen, timeout: @read_timeout) ⇒ String
Perform a blocking receive.
-
#recvfrom_nonblock(maxlen) ⇒ Socketry::UDP::Datagram, :wait_readable
Perform a non-blocking receive.
-
#send(msg, host: nil, port: nil) ⇒ Fixum
Send a UDP packet to a remote host.
Methods included from Timeout
#clear_timeout, #lifetime, #set_timeout, #start_timer, #time_remaining
Constructor Details
#initialize(addr_family: :ipv4, read_timeout: Socketry::Timeout::DEFAULT_TIMEOUTS[:read], write_timeout: Socketry::Timeout::DEFAULT_TIMEOUTS[:write], timer: Socketry::Timeout::DEFAULT_TIMER.new, resolver: Socketry::Resolver::DEFAULT_RESOLVER, socket_class: ::UDPSocket) ⇒ Socketry::UDP::Socket
Create a new UDP socket
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/socketry/udp/socket.rb', line 60 def initialize( addr_family: :ipv4, read_timeout: Socketry::Timeout::DEFAULT_TIMEOUTS[:read], write_timeout: Socketry::Timeout::DEFAULT_TIMEOUTS[:write], timer: Socketry::Timeout::DEFAULT_TIMER.new, resolver: Socketry::Resolver::DEFAULT_RESOLVER, socket_class: ::UDPSocket ) @addr_family = case addr_family when :ipv4 then ::Socket::AF_INET when :ipv6 then ::Socket::AF_INET6 when ::Socket::AF_INET, ::Socket::AF_INET6 then addr_family else raise ArgumentError, "invalid address family: #{addr_family.inspect}" end @socket = socket_class.new(@addr_family) @read_timeout = read_timeout @write_timeout = write_timeout @resolver = resolver start_timer(timer) end |
Instance Attribute Details
#addr_family ⇒ Object (readonly)
Returns the value of attribute addr_family.
10 11 12 |
# File 'lib/socketry/udp/socket.rb', line 10 def addr_family @addr_family end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
10 11 12 |
# File 'lib/socketry/udp/socket.rb', line 10 def read_timeout @read_timeout end |
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
10 11 12 |
# File 'lib/socketry/udp/socket.rb', line 10 def resolver @resolver end |
#socket_class ⇒ Object (readonly)
Returns the value of attribute socket_class.
10 11 12 |
# File 'lib/socketry/udp/socket.rb', line 10 def socket_class @socket_class end |
#write_timeout ⇒ Object (readonly)
Returns the value of attribute write_timeout.
10 11 12 |
# File 'lib/socketry/udp/socket.rb', line 10 def write_timeout @write_timeout end |
Class Method Details
.bind(local_addr, local_port, resolver: Socketry::Resolver::DEFAULT_RESOLVER) ⇒ Socketry::UDP::Socket
Create a UDP server bound to the given address and port
33 34 35 |
# File 'lib/socketry/udp/socket.rb', line 33 def self.bind(local_addr, local_port, resolver: Socketry::Resolver::DEFAULT_RESOLVER) from_addr(local_addr, resolver: resolver).bind(local_addr, local_port) end |
.connect(remote_addr, remote_port, resolver: Socketry::Resolver::DEFAULT_RESOLVER) ⇒ Socketry::UDP::Socket
Connect to the given address and port
44 45 46 |
# File 'lib/socketry/udp/socket.rb', line 44 def self.connect(remote_addr, remote_port, resolver: Socketry::Resolver::DEFAULT_RESOLVER) from_addr(remote_addr, resolver: resolver).connect(remote_addr, remote_port) end |
.from_addr(remote_addr, resolver: Socketry::Resolver::DEFAULT_RESOLVER) ⇒ Socketry::UDP::Socket
Create a UDP socket matching the given socket’s address family
18 19 20 21 22 23 24 |
# File 'lib/socketry/udp/socket.rb', line 18 def self.from_addr(remote_addr, resolver: Socketry::Resolver::DEFAULT_RESOLVER) addr = resolver.resolve(remote_addr) if addr.ipv4? then new(addr_family: :ipv4) elsif addr.ipv6? then new(addr_family: :ipv6) else raise Socketry::AddressError, "unsupported IP address family: #{addr}" end end |
Instance Method Details
#bind(local_addr, local_port) ⇒ self
Start a UDP server bound to a particular address and port
89 90 91 92 93 94 95 96 |
# File 'lib/socketry/udp/socket.rb', line 89 def bind(local_addr, local_port) @socket.bind(@resolver.resolve(local_addr).to_s, local_port) self rescue Errno::EADDRINUSE => ex raise AddressInUseError, ex., ex.backtrace rescue => ex raise Socketry::Error, ex., ex.backtrace end |
#close ⇒ true, false
Close the socket
169 170 171 172 173 174 175 |
# File 'lib/socketry/udp/socket.rb', line 169 def close return false if closed? @socket.close true ensure @socket = nil end |
#closed? ⇒ true, false
Is the socket closed?
180 181 182 |
# File 'lib/socketry/udp/socket.rb', line 180 def closed? @socket.nil? end |
#connect(remote_addr, remote_port) ⇒ self
Make a UDP client connection to the given address and port
104 105 106 107 108 109 110 |
# File 'lib/socketry/udp/socket.rb', line 104 def connect(remote_addr, remote_port) @socket.connect(@resolver.resolve(remote_addr).to_s, remote_port) self rescue => ex # TODO: more specific exceptions raise Socketry::Error, ex., ex.backtrace end |
#recvfrom(maxlen, timeout: @read_timeout) ⇒ String
Perform a blocking receive
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/socketry/udp/socket.rb', line 132 def recvfrom(maxlen, timeout: @read_timeout) set_timeout(timeout) begin while (result = recvfrom_nonblock(maxlen)) == :wait_readable next if @socket.wait_readable(time_remaining(timeout)) raise Socketry::TimeoutError, "recvfrom timed out after #{timeout} seconds" end ensure clear_timeout(timeout) end result end |
#recvfrom_nonblock(maxlen) ⇒ Socketry::UDP::Datagram, :wait_readable
Perform a non-blocking receive
117 118 119 120 121 122 123 124 |
# File 'lib/socketry/udp/socket.rb', line 117 def recvfrom_nonblock(maxlen) Socketry::UDP::Datagram.new(*@socket.recvfrom_nonblock(maxlen)) rescue ::IO::WaitReadable :wait_readable rescue => ex # TODO: more specific exceptions raise Socketry::Error, ex., ex.backtrace end |
#send(msg, host: nil, port: nil) ⇒ Fixum
Send a UDP packet to a remote host
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/socketry/udp/socket.rb', line 154 def send(msg, host: nil, port: nil) host = @resolver.resolve(host).to_s if host if host || port @socket.send(msg, 0, host, port) else @socket.send(msg, 0) end rescue => ex # TODO: more specific exceptions raise Socketry::Error, ex., ex.backtrace end |