lib/grpc_kit/session/stream.rb in grpc_kit-0.1.0 vs lib/grpc_kit/session/stream.rb in grpc_kit-0.1.1

- old
+ new

@@ -1,68 +1,60 @@ # frozen_string_literal: false -require 'ds9' +require 'forwardable' +require 'grpc_kit/session/buffer' +require 'grpc_kit/session/headers' module GrpcKit module Session class Stream - attr_reader :headers - attr_accessor :data + extend Forwardable - def initialize(stream_id:, end_read_stream: false, end_write_stream: false) - @stream_id = stream_id - @end_read_stream = end_read_stream - @end_write_stream = end_write_stream - @end_stream = false - @headers = {} + delegate end_write: :@pending_send_data + delegate end_read: :@pending_recv_data - @data = '' - @write_data = StringIO.new - end + attr_reader :headers, :pending_send_data, :pending_recv_data + attr_accessor :local_end_stream, :remote_end_stream, :inflight, :stream_id - def close - @end_stream = true - end + def initialize(stream_id:, send_data: GrpcKit::Session::Buffer.new) + @stream_id = stream_id + @end_read_stream = false + @headers = GrpcKit::Session::Headers.new + @pending_send_data = send_data + @pending_recv_data = GrpcKit::Session::Buffer.new - def closed? - @end_stream + @local_end_stream = false + @remote_end_stream = false + @inflight = false end - def eq?(stream_id) - @stream_id == stream_id + def write_send_data(data, last: false) + @pending_send_data.write(data, last: last) end - def exist_data? - !@data.empty? + def read_recv_data(last: false) + @pending_recv_data.read(last: last) end - def recv(data) - @data = data # XXX + def end_write? + @local_end_stream || @pending_send_data.end_write? end - # TODO - def recv2(data) - @data << data + def end_read? + @remote_end_stream || @pending_recv_data.end_read? end - # TODO: name - def send(data) - @write_data = data - # @write_data.write(data) + def end_stream? + end_read? && end_write? end - # TODO: name - def read(len) - # @write_data.rewind - @write_data.read(len) + def end_stream + end_read + end_write end - def read_stream_end? - @end_read_stream - end - - def write_stream_end? - @end_write_stream + def add_header(name, value) + @headers.add(name, value) end end end end