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(uri, _, _) ⇒ UDP

Returns a new instance of UDP.


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

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

Instance Method Details

#closeObject


27
28
29
# File 'lib/httpx/io/udp.rb', line 27

def close
  @io.close
end

#connectObject


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

def connect; end

#connected?Boolean

Returns:

  • (Boolean)

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

def connected?
  true
end

#read(size, buffer) ⇒ Object


38
39
40
41
42
43
44
45
# File 'lib/httpx/io/udp.rb', line 38

def read(size, buffer)
  data, _ = @io.recvfrom_nonblock(size)
  buffer.replace(data)
  buffer.bytesize
rescue ::IO::WaitReadable
  0
rescue IOError
end

#to_ioObject


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

def to_io
  @io.to_io
end

#write(buffer) ⇒ Object


31
32
33
34
35
# File 'lib/httpx/io/udp.rb', line 31

def write(buffer)
  siz = @io.send(buffer, 0, @host, @port)
  buffer.slice!(0, siz)
  siz
end