1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
# frozen_string_literal: true
require "ipaddr"
module HTTPX
class UDP
include Loggable
def initialize(ip, port, options)
@host = ip
@port = port
@io = UDPSocket.new(IPAddr.new(ip).family)
@options = options
end
def to_io
@io.to_io
end
def connect; end
def connected?
true
end
def close
@io.close
end
if RUBY_ENGINE == "jruby"
# In JRuby, sendmsg_nonblock is not implemented
def write(buffer)
siz = @io.send(buffer.to_s, 0, @host, @port)
log { "WRITE: #{siz} bytes..." }
buffer.shift!(siz)
siz
end
else
def write(buffer)
siz = @io.sendmsg_nonblock(buffer.to_s, 0, Socket.sockaddr_in(@port, @host.to_s), exception: false)
return 0 if siz == :wait_writable
return if siz.nil?
log { "WRITE: #{siz} bytes..." }
buffer.shift!(siz)
siz
end
end
def read(size, buffer)
ret = @io.recvfrom_nonblock(size, 0, buffer, exception: false)
return 0 if ret == :wait_readable
return if ret.nil?
log { "READ: #{buffer.bytesize} bytes..." }
buffer.bytesize
rescue IOError
end
end
end
|