Sha256: 895d079a019e258e426ded23e8628d2fabecc093dbfd2895390620c865019058

Contents?: true

Size: 1.28 KB

Versions: 18

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

require 'ds9'

module GrpcKit
  module Session
    class IO
      def initialize(io)
        @io = io
      end

      def close
        @io.close
      end

      # @param length [Integer]
      # @return [DS9::ERR_WOULDBLOCK, DS9::ERR_EOF, String]
      def recv_event(length)
        data = @io.read_nonblock(length, nil, exception: false)

        case data
        when :wait_readable
          DS9::ERR_WOULDBLOCK
        when nil # nil means EOF
          DS9::ERR_EOF
        else
          data
        end
      end

      # @param data [String]
      # @return [DS9::ERR_WOULDBLOCK, Integer]
      def send_event(data)
        return 0 if data.empty?

        bytes = @io.write_nonblock(data, exception: false)
        if bytes == :wait_writable
          DS9::ERR_WOULDBLOCK
        else
          bytes
        end
      end

      # Blocking until io object is readable
      # @return [void]
      def wait_readable
        ::IO.select([@io], [], [])
        true
      rescue IOError
        false
      end

      # Blocking until io object is readable or writable
      # @return [void]
      def select(timeout = 1)
        ::IO.select([@io], [@io], [], timeout)
      end

      # @return [void]
      def flush
        @io.flush
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
grpc_kit-0.3.9 lib/grpc_kit/session/io.rb
grpc_kit-0.3.8 lib/grpc_kit/session/io.rb
grpc_kit-0.3.7 lib/grpc_kit/session/io.rb
grpc_kit-0.3.6 lib/grpc_kit/session/io.rb
grpc_kit-0.3.5 lib/grpc_kit/session/io.rb
grpc_kit-0.3.4 lib/grpc_kit/session/io.rb
grpc_kit-0.3.3 lib/grpc_kit/session/io.rb
grpc_kit-0.3.2 lib/grpc_kit/session/io.rb
grpc_kit-0.3.1 lib/grpc_kit/session/io.rb
grpc_kit-0.3.0 lib/grpc_kit/session/io.rb
grpc_kit-0.2.1 lib/grpc_kit/session/io.rb
grpc_kit-0.2.0 lib/grpc_kit/session/io.rb
grpc_kit-0.1.14 lib/grpc_kit/session/io.rb
grpc_kit-0.1.13 lib/grpc_kit/session/io.rb
grpc_kit-0.1.12 lib/grpc_kit/session/io.rb
grpc_kit-0.1.11 lib/grpc_kit/session/io.rb
grpc_kit-0.1.10 lib/grpc_kit/session/io.rb
grpc_kit-0.1.9 lib/grpc_kit/session/io.rb