Sha256: 457c272e0ec4017443424474cd3702589ab717215e46945ba9fff5c53baf5e66

Contents?: true

Size: 998 Bytes

Versions: 5

Compression:

Stored size: 998 Bytes

Contents

# frozen_string_literal: true

require "socket"
require "ipaddr"

module HTTPX
  class UDP
    include Loggable

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

    def to_io
      @io.to_io
    end

    def connect; end

    def connected?
      true
    end

    def close
      @io.close
    end

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

    if RUBY_VERSION < "2.3"
      def read(size, buffer)
        data, _ = @io.recvfrom_nonblock(size)
        buffer.replace(data)
        buffer.bytesize
      rescue ::IO::WaitReadable
        0
      rescue IOError
      end
    else
      def read(size, buffer)
        ret = @io.recvfrom_nonblock(size, 0, buffer, exception: false)
        return 0 if ret == :wait_readable
        return if ret.nil?

        buffer.bytesize
      rescue IOError
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
httpx-0.6.3 lib/httpx/io/udp.rb
httpx-0.6.2 lib/httpx/io/udp.rb
httpx-0.6.1 lib/httpx/io/udp.rb
httpx-0.6.0 lib/httpx/io/udp.rb
httpx-0.5.1 lib/httpx/io/udp.rb