Skip to content

Commit

Permalink
Add methods for handling state transitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 1, 2020
1 parent 2fdcbc2 commit f819e0b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/protocol/http2/stream.rb
Expand Up @@ -124,10 +124,6 @@ def parent=(stream)
@dependency.parent = stream.dependency
end

# The stream is being closed because the connection is being closed.
def close(error = nil)
end

def maximum_frame_size
@connection.available_frame_size
end
Expand All @@ -144,6 +140,15 @@ def closed?
@state == :closed
end

# Transition directly to closed state. Do not pass go, do not collect $200.
# This method should only be used by `Connection#close`.
def close(error = nil)
unless closed?
@state = :closed
self.closed(error)
end
end

def send_headers?
@state == :idle or @state == :reserved_local or @state == :open or @state == :half_closed_remote
end
Expand Down Expand Up @@ -228,16 +233,26 @@ def send_data(*arguments, **options)
end
end

# The stream has been opened.
def opened(error = nil)
end

def open!
if @state == :idle
@state = :open
else
raise ProtocolError, "Cannot open stream in state: #{@state}"
end

self.opened

return self
end

# The stream has been closed. If closed due to a stream reset, the error will be set.
def closed(error = nil)
end

# Transition the stream into the closed state.
# @param error_code [Integer] the error code if the stream was closed due to a stream reset.
def close!(error_code = nil)
Expand All @@ -248,7 +263,7 @@ def close!(error_code = nil)
error = StreamError.new("Stream closed!", error_code)
end

self.close(error)
self.closed(error)

return self
end
Expand Down

0 comments on commit f819e0b

Please sign in to comment.