Sha256: 7c6786a81ff8c1bedf21ad75c051b0a856c875140a8ecfc8ff136b46d574b666

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

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

    if RUBY_VERSION < "2.2"
      # :nocov:
      def close
        @io.close
      rescue StandardError
        nil
      end
      # :nocov:
    else
      def close
        @io.close
      end
    end

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

    # :nocov:
    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
    # :nocov:
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
httpx-0.9.0 lib/httpx/io/udp.rb
httpx-0.8.2 lib/httpx/io/udp.rb
httpx-0.8.1 lib/httpx/io/udp.rb
httpx-0.8.0 lib/httpx/io/udp.rb
httpx-0.7.0 lib/httpx/io/udp.rb
httpx-0.6.7 lib/httpx/io/udp.rb
httpx-0.6.6 lib/httpx/io/udp.rb
httpx-0.6.5 lib/httpx/io/udp.rb