Sha256: 6c389d388df877b4563ffbd5eb5833aeba768afe6efd667a770466e45289ed17

Contents?: true

Size: 1005 Bytes

Versions: 1

Compression:

Stored size: 1005 Bytes

Contents

# frozen_string_literal: true

require 'ds9'

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

      def close
        @io.close
      end

      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

      def send_event(data)
        remain = data.bytesize
        size = remain
        while remain > 0
          begin
            remain -= @io.syswrite(data)
          rescue Errno::EAGAIN, Errno::EWOULDBLOCK
            unless IO.select(nil, [io], nil, 1)
              raise 'timeout writing data'
            end
          end

          data = data.byteslice(remain..-1)
        end

        size
      end

      def wait_readable
        @io.wait_writable
      end

      def flush
        @io.flush
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grpc_kit-0.1.1 lib/grpc_kit/session/io.rb