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 Attribute Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log

Constructor Details

#initialize(ip, port) ⇒ UDP

Returns a new instance of UDP.



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

def initialize(ip, port)
  @ip = ip.to_s
  @port = port
  @io = UDPSocket.new(ip.family)
  @closed = false
end

Instance Attribute Details

#ipObject (readonly)

Returns the value of attribute ip.



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

def ip
  @ip
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Instance Method Details

#closeObject



50
51
52
53
54
55
# File 'lib/httpx/io/udp.rb', line 50

def close
  return if @closed
  @io.close
ensure
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/httpx/io/udp.rb', line 57

def closed?
  @closed
end

#inspectObject



61
62
63
# File 'lib/httpx/io/udp.rb', line 61

def inspect
  "#<(fd: #{@io.fileno}): #{@ip}:#{@port})>"
end

#read(size, buffer) ⇒ Object



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

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

#to_ioObject



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

def to_io
  @io.to_io
end

#write(buffer) ⇒ Object



44
45
46
47
48
# File 'lib/httpx/io/udp.rb', line 44

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