Sha256: b38ca777dbbabba4eb8ab91d0ed34d196d26585a8b0b495dd3975f7527415e38

Contents?: true

Size: 1.2 KB

Versions: 20

Compression:

Stored size: 1.2 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

    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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/io/udp.rb
httpx-1.3.3 lib/httpx/io/udp.rb
httpx-1.3.2 lib/httpx/io/udp.rb
httpx-1.3.1 lib/httpx/io/udp.rb
httpx-1.3.0 lib/httpx/io/udp.rb
httpx-1.2.6 lib/httpx/io/udp.rb
httpx-1.2.4 lib/httpx/io/udp.rb
httpx-1.2.3 lib/httpx/io/udp.rb
httpx-1.2.2 lib/httpx/io/udp.rb
httpx-1.2.1 lib/httpx/io/udp.rb
httpx-1.2.0 lib/httpx/io/udp.rb
httpx-1.1.5 lib/httpx/io/udp.rb
httpx-1.1.4 lib/httpx/io/udp.rb
httpx-1.1.3 lib/httpx/io/udp.rb
httpx-1.1.2 lib/httpx/io/udp.rb
httpx-1.1.1 lib/httpx/io/udp.rb
httpx-1.1.0 lib/httpx/io/udp.rb
httpx-1.0.2 lib/httpx/io/udp.rb
httpx-1.0.1 lib/httpx/io/udp.rb
httpx-1.0.0 lib/httpx/io/udp.rb