Class: HTTPX::UDP

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/httpx/io/udp.rb

Constant Summary

Constants included from Loggable

Loggable::COLORS

Instance Method Summary collapse

Methods included from Loggable

#log, #log_exception

Constructor Details

#initialize(ip, port, options) ⇒ UDP

Returns a new instance of UDP.



9
10
11
12
13
14
# File 'lib/httpx/io/udp.rb', line 9

def initialize(ip, port, options)
  @host = ip
  @port = port
  @io = UDPSocket.new(IPAddr.new(ip).family)
  @options = options
end

Instance Method Details

#closeObject

:nocov:



28
29
30
31
32
# File 'lib/httpx/io/udp.rb', line 28

def close
  @io.close
rescue StandardError
  nil
end

#connectObject



20
# File 'lib/httpx/io/udp.rb', line 20

def connect; end

#connected?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/httpx/io/udp.rb', line 22

def connected?
  true
end

#read(size, buffer) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/httpx/io/udp.rb', line 54

def read(size, buffer)
  data, _ = @io.recvfrom_nonblock(size)
  buffer.replace(data)
  log { "READ: #{buffer.bytesize} bytes..." }
  buffer.bytesize
rescue ::IO::WaitReadable
  0
rescue IOError
end

#to_ioObject



16
17
18
# File 'lib/httpx/io/udp.rb', line 16

def to_io
  @io.to_io
end

#write(buffer) ⇒ Object

In JRuby, sendmsg_nonblock is not implemented



89
90
91
92
93
94
95
96
97
98
# File 'lib/httpx/io/udp.rb', line 89

def write(buffer)
  siz = @io.sendmsg_nonblock(buffer.to_s, 0, Socket.sockaddr_in(@port, @host.to_s))
  log { "WRITE: #{siz} bytes..." }
  buffer.shift!(siz)
  siz
rescue ::IO::WaitWritable
  0
rescue EOFError
  nil
end