Sha256: 4713feb75f628a43e6c33d6dbf2ba80ee19d1e63978b78cfc307f4826cb4ec16

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

# 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

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

    # :nocov:
    if (RUBY_ENGINE == "truffleruby" && RUBY_ENGINE_VERSION < "21.1.0") ||
       RUBY_VERSION < "2.3"
      def write(buffer)
        siz = @io.sendmsg_nonblock(buffer.to_s, 0, Socket.sockaddr_in(@port, @host.to_s))
        log { "WRITE: #{siz} bytes..." }
        buffer.shift!(siz)
        siz
      rescue ::IO::WaitWritable
        0
      rescue EOFError
        nil
      end

      def read(size, buffer)
        data, _ = @io.recvfrom_nonblock(size)
        buffer.replace(data)
        log { "READ: #{buffer.bytesize} bytes..." }
        buffer.bytesize
      rescue ::IO::WaitReadable
        0
      rescue IOError
      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

      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

    # 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 if RUBY_ENGINE == "jruby"
    # :nocov:
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
httpx-0.22.5 lib/httpx/io/udp.rb
httpx-0.22.4 lib/httpx/io/udp.rb
httpx-0.22.3 lib/httpx/io/udp.rb