Class: GrpcKit::Session::RecvBuffer
- Inherits:
-
Object
- Object
- GrpcKit::Session::RecvBuffer
- Defined in:
- lib/grpc_kit/session/recv_buffer.rb
Defined Under Namespace
Classes: Closed
Instance Method Summary collapse
- #close ⇒ void
- #closed? ⇒ Boolean
- #empty? ⇒ Boolean
- #end_read ⇒ void
- #end_read? ⇒ Boolean
-
#initialize ⇒ RecvBuffer
constructor
A new instance of RecvBuffer.
-
#read(size = nil, last: false, blocking:) ⇒ String, ...
This method is not thread safe (as RecvBuffer is designed to be a multi-producer/single-consumer).
- #write(data) ⇒ void
Constructor Details
#initialize ⇒ RecvBuffer
Returns a new instance of RecvBuffer.
8 9 10 11 12 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 8 def initialize @buffer = +''.b @end = false @queue = Queue.new end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
33 34 35 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 33 def close @queue.close end |
#closed? ⇒ Boolean
28 29 30 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 28 def closed? @queue.closed? end |
#empty? ⇒ Boolean
23 24 25 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 23 def empty? @queue.empty? end |
#end_read ⇒ void
This method returns an undefined value.
81 82 83 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 81 def end_read @end = true end |
#end_read? ⇒ Boolean
76 77 78 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 76 def end_read? @end end |
#read(size = nil, last: false, blocking:) ⇒ String, ...
This method is not thread safe (as RecvBuffer is designed to be a multi-producer/single-consumer)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 42 def read(size = nil, last: false, blocking:) if @buffer.empty? return nil if empty? && closed? return :wait_readable if empty? && !blocking # Consume existing data as much as possible to continue (important on clients where single-threaded) loop do begin data = @queue.shift(!blocking) @buffer << data if data rescue ThreadError break end break if empty? end end buf = if size.nil? || @buffer.bytesize < size rbuf = @buffer @buffer = ''.b rbuf else @buffer.freeze rbuf = @buffer.byteslice(0, size) @buffer = @buffer.byteslice(size, @buffer.bytesize) rbuf end end_read if last buf end |
#write(data) ⇒ void
This method returns an undefined value.
16 17 18 19 20 |
# File 'lib/grpc_kit/session/recv_buffer.rb', line 16 def write(data) @queue << data rescue ClosedQueueError raise Closed, "[BUG] write to closed queue" end |