Class: H2::Stream

Inherits:
Object
  • Object
show all
Includes:
Blockable, On
Defined in:
lib/h2/stream.rb

Constant Summary collapse

STREAM_EVENTS =
[
  :close,
  :headers,
  :data
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from On

#on

Methods included from Blockable

#init_blocking, #unblock!

Constructor Details

#initialize(client:, stream:, push: false, parent: nil) {|_self| ... } ⇒ Stream

Returns a new instance of Stream.

Yields:

  • (_self)

Yield Parameters:

  • _self (H2::Stream)

    the object that the method was called on



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

def initialize client:, stream:, push: false, parent: nil
  @body    = ''
  @client  = client
  @closed  = false
  @headers = {}
  @parent  = parent
  @push    = push
  @pushes  = Set.new
  @stream  = stream

  init_blocking
  yield self if block_given?
  bind_events
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

#pushesObject (readonly)

Returns the value of attribute pushes.



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

def pushes
  @pushes
end

#streamObject (readonly)

Returns the value of attribute stream.



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

def stream
  @stream
end

Instance Method Details

#add_push(stream) ⇒ Object



47
48
49
# File 'lib/h2/stream.rb', line 47

def add_push stream
  @pushes << stream
end

#bind_eventsObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/h2/stream.rb', line 71

def bind_events
  @stream.on(:close) do
    @parent.add_push self if @parent && push?
    @client.last_stream = self
    @closed = true
    unblock!
    on :close
  end

  @stream.on(:headers) do |h|
    h = Hash[h]
    on :headers, h
    @headers.merge! h
  end

  @stream.on(:data) do |d|
    on :data, d
    @body << d
  end
end

#block!(timeout = nil) ⇒ Object



56
57
58
59
# File 'lib/h2/stream.rb', line 56

def block! timeout = nil
  @pushes.each {|p| p.block! timeout}
  super
end

#cancel!Object



51
52
53
54
# File 'lib/h2/stream.rb', line 51

def cancel!
  @stream.cancel
  unblock!
end

#closed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/h2/stream.rb', line 39

def closed?
  @closed
end

#idObject



31
32
33
# File 'lib/h2/stream.rb', line 31

def id
  @stream.id
end

#ok?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/h2/stream.rb', line 35

def ok?
  headers[STATUS_KEY] == '200'
end

#push?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/h2/stream.rb', line 43

def push?
  @push
end

#to_hObject



92
93
94
# File 'lib/h2/stream.rb', line 92

def to_h
  { headers: headers, body: body }
end