Class: Plum::Stream

Inherits:
Object
  • Object
show all
Includes:
EventEmitter, FlowControl, StreamUtils
Defined in:
lib/plum/stream.rb

Instance Attribute Summary collapse

Attributes included from FlowControl

#recv_remaining_window, #send_remaining_window

Instance Method Summary collapse

Methods included from StreamUtils

#promise, #send_data, #send_headers

Methods included from FlowControl

#send, #window_update

Methods included from EventEmitter

#on

Constructor Details

#initialize(con, id, state: :idle, weight: 16, parent: nil, exclusive: false) ⇒ Stream

Returns a new instance of Stream.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/plum/stream.rb', line 16

def initialize(con, id, state: :idle, weight: 16, parent: nil, exclusive: false)
  @connection = con
  @id = id
  @state = state
  @continuation = []
  @children = Set.new

  initialize_flow_control(send: @connection.remote_settings[:initial_window_size],
                          recv: @connection.local_settings[:initial_window_size])
  update_dependency(weight: weight, parent: parent, exclusive: exclusive)
end

Instance Attribute Details

#childrenObject (readonly)

The child (depending on this stream) streams.



14
15
16
# File 'lib/plum/stream.rb', line 14

def children
  @children
end

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/plum/stream.rb', line 10

def connection
  @connection
end

#exclusiveObject (readonly)

Returns the value of attribute exclusive.



11
12
13
# File 'lib/plum/stream.rb', line 11

def exclusive
  @exclusive
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/plum/stream.rb', line 10

def id
  @id
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/plum/stream.rb', line 12

def parent
  @parent
end

#stateObject (readonly)

Returns the value of attribute state.



10
11
12
# File 'lib/plum/stream.rb', line 10

def state
  @state
end

#weightObject (readonly)

Returns the value of attribute weight.



11
12
13
# File 'lib/plum/stream.rb', line 11

def weight
  @weight
end

Instance Method Details

#closeObject

Closes this stream. Sends RST_STREAM frame to the peer.



61
62
63
64
# File 'lib/plum/stream.rb', line 61

def close
  @state = :closed
  callback(:close)
end

#receive_frame(frame) ⇒ Object

Processes received frames for this stream. Internal use.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/plum/stream.rb', line 30

def receive_frame(frame)
  validate_received_frame(frame)
  consume_recv_window(frame)

  case frame.type
  when :data
    receive_data(frame)
  when :headers
    receive_headers(frame)
  when :priority
    receive_priority(frame)
  when :rst_stream
    receive_rst_stream(frame)
  when :window_update
    receive_window_update(frame)
  when :continuation
    receive_continuation(frame)
  when :push_promise
    receive_push_promise(frame)
  when :ping, :goaway, :settings
    raise RemoteConnectionError.new(:protocol_error) # stream_id MUST be 0x00
  else
    # MUST ignore unknown frame
  end
rescue RemoteStreamError => e
  callback(:stream_error, e)
  send_immediately Frame.rst_stream(id, e.http2_error_type)
  close
end

#set_state(state) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
# File 'lib/plum/stream.rb', line 67

def set_state(state)
  @state = state
end

#update_dependency(weight: nil, parent: nil, exclusive: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/plum/stream.rb', line 72

def update_dependency(weight: nil, parent: nil, exclusive: nil)
  raise RemoteStreamError.new(:protocol_error, "A stream cannot depend on itself.") if parent == self

  if weight
    @weight = weight
  end

  if parent
    @parent = parent
    @parent.children << self
  end

  if exclusive != nil
    @exclusive = exclusive
    if @parent && exclusive
      @parent.children.to_a.each do |child|
        next if child == self
        @parent.children.delete(child)
        child.parent = self
        @children << child
      end
    end
  end
end