Class: Biryani::StreamsContext
- Inherits:
-
Object
- Object
- Biryani::StreamsContext
- Defined in:
- lib/biryani/streams_context.rb
Instance Attribute Summary collapse
-
#tx ⇒ Object
Returns the value of attribute tx.
Instance Method Summary collapse
- #[](stream_id) ⇒ StreamContext?
- #close_all ⇒ Object
- #closed_stream_ids ⇒ Array<Integer>
- #count_active ⇒ Integer
- #delete(stream_id) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(proc) ⇒ StreamsContext
constructor
A new instance of StreamsContext.
- #last_stream_id ⇒ Integer
- #length ⇒ Integer
- #new_context(stream_id, send_initial_window_size, recv_initial_window_size) ⇒ StreamContext
- #receiving_continuation_stream_id ⇒ Integer?
- #remove_closed(data_buffer) ⇒ Object
- #sendable_datas(stream_id, data, send_window, max_frame_size) ⇒ Array<Object>, String
Constructor Details
#initialize(proc) ⇒ StreamsContext
Returns a new instance of StreamsContext.
5 6 7 8 9 |
# File 'lib/biryani/streams_context.rb', line 5 def initialize(proc) @h = {} # Hash<Integer, StreamContext> @proc = proc @tx = Ractor::Port.new end |
Instance Attribute Details
#tx ⇒ Object
Returns the value of attribute tx.
3 4 5 |
# File 'lib/biryani/streams_context.rb', line 3 def tx @tx end |
Instance Method Details
#[](stream_id) ⇒ StreamContext?
30 31 32 |
# File 'lib/biryani/streams_context.rb', line 30 def [](stream_id) @h[stream_id] end |
#close_all ⇒ Object
98 99 100 |
# File 'lib/biryani/streams_context.rb', line 98 def close_all each(&:close) end |
#closed_stream_ids ⇒ Array<Integer>
59 60 61 |
# File 'lib/biryani/streams_context.rb', line 59 def closed_stream_ids @h.filter { |_, ctx| ctx.closed? }.keys end |
#count_active ⇒ Integer
49 50 51 |
# File 'lib/biryani/streams_context.rb', line 49 def count_active @h.values.filter(&:active?).length end |
#delete(stream_id) ⇒ Object
23 24 25 |
# File 'lib/biryani/streams_context.rb', line 23 def delete(stream_id) @h.delete(stream_id) end |
#each(&block) ⇒ Object
44 45 46 |
# File 'lib/biryani/streams_context.rb', line 44 def each(&block) @h.each_value(&block) end |
#empty? ⇒ Boolean
40 41 42 |
# File 'lib/biryani/streams_context.rb', line 40 def empty? @h.empty? end |
#last_stream_id ⇒ Integer
64 65 66 |
# File 'lib/biryani/streams_context.rb', line 64 def last_stream_id @h.reject { |_, ctx| ctx.idle? }.keys.max || 0 end |
#length ⇒ Integer
35 36 37 |
# File 'lib/biryani/streams_context.rb', line 35 def length @h.length end |
#new_context(stream_id, send_initial_window_size, recv_initial_window_size) ⇒ StreamContext
16 17 18 19 20 |
# File 'lib/biryani/streams_context.rb', line 16 def new_context(stream_id, send_initial_window_size, recv_initial_window_size) ctx = StreamContext.new(stream_id, send_initial_window_size, recv_initial_window_size, @proc, @tx) @h[stream_id] = ctx ctx end |
#receiving_continuation_stream_id ⇒ Integer?
54 55 56 |
# File 'lib/biryani/streams_context.rb', line 54 def receiving_continuation_stream_id @h.find { |_, ctx| ctx.receiving_continuation? }&.first end |
#remove_closed(data_buffer) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/biryani/streams_context.rb', line 91 def remove_closed(data_buffer) closed_ids = closed_stream_ids.filter { |id| !data_buffer.has?(id) } closed_ids.each do |id| @h.delete(id) end end |
#sendable_datas(stream_id, data, send_window, max_frame_size) ⇒ Array<Object>, String
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/biryani/streams_context.rb', line 75 def sendable_datas(stream_id, data, send_window, max_frame_size) len = [data.bytesize, send_window.length, @h[stream_id].send_window.length].min payload = data[0...len] remains = data[len..] || '' len = (len + max_frame_size - 1) / max_frame_size frames = payload.gsub(/.{1,#{max_frame_size}}/m).with_index.map do |s, index| end_stream = remains.empty? && index == len - 1 Frame::Data.new(end_stream, stream_id, s, nil) end [frames, remains] end |