Class: Protocol::HTTP2::Connection

Inherits:
Object
  • Object
show all
Includes:
FlowControlled
Defined in:
lib/protocol/http2/connection.rb

Overview

This is the core connection class that handles HTTP/2 protocol semantics including stream management, settings negotiation, and frame processing.

Direct Known Subclasses

Client, Server

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FlowControlled

#available_frame_size, #available_size, #consume_local_window, #consume_remote_window, #request_window_update, #send_window_update, #update_local_window, #window_updated

Constructor Details

#initialize(framer, local_stream_id) ⇒ Connection

Initialize a new HTTP/2 connection.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/protocol/http2/connection.rb', line 24

def initialize(framer, local_stream_id)
	super()
	
	@state = :new
	
	# Hash(Integer, Stream)
	@streams = {}
	
	@framer = framer
	
	# The next stream id to use:
	@local_stream_id = local_stream_id
	
	# The biggest remote stream id seen thus far:
	@remote_stream_id = 0
	
	@local_settings = PendingSettings.new
	@remote_settings = Settings.new
	
	@decoder = HPACK::Context.new
	@encoder = HPACK::Context.new
	
	@local_window = LocalWindow.new
	@remote_window = Window.new
	
	# The lowest Last-Stream-ID received in a GOAWAY frame, or nil if no GOAWAY frame has been received:
	@goaway_stream_id = nil
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



183
184
185
# File 'lib/protocol/http2/connection.rb', line 183

def dependencies
  @dependencies
end

#dependencyObject (readonly)

Returns the value of attribute dependency.



185
186
187
# File 'lib/protocol/http2/connection.rb', line 185

def dependency
  @dependency
end

#framerObject (readonly)

Returns the value of attribute framer.



82
83
84
# File 'lib/protocol/http2/connection.rb', line 82

def framer
  @framer
end

#goaway_stream_idObject (readonly)

The lowest Last-Stream-ID received in a GOAWAY frame.



102
103
104
# File 'lib/protocol/http2/connection.rb', line 102

def goaway_stream_id
  @goaway_stream_id
end

#local_settingsObject

Current settings value for local and peer



88
89
90
# File 'lib/protocol/http2/connection.rb', line 88

def local_settings
  @local_settings
end

#local_windowObject (readonly)

Our window for receiving data. When we receive data, it reduces this window. If the window gets too small, we must send a window update.



93
94
95
# File 'lib/protocol/http2/connection.rb', line 93

def local_window
  @local_window
end

#remote_settingsObject

Returns the value of attribute remote_settings.



89
90
91
# File 'lib/protocol/http2/connection.rb', line 89

def remote_settings
  @remote_settings
end

#remote_stream_idObject (readonly)

The highest stream_id that has been successfully accepted by this connection.



99
100
101
# File 'lib/protocol/http2/connection.rb', line 99

def remote_stream_id
  @remote_stream_id
end

#remote_windowObject (readonly)

Our window for sending data. When we send data, it reduces this window.



96
97
98
# File 'lib/protocol/http2/connection.rb', line 96

def remote_window
  @remote_window
end

#stateObject

Connection state (:new, :open, :closed).



85
86
87
# File 'lib/protocol/http2/connection.rb', line 85

def state
  @state
end

#streamsObject (readonly)

Returns the value of attribute streams.



181
182
183
# File 'lib/protocol/http2/connection.rb', line 181

def streams
  @streams
end

Instance Method Details

#[](id) ⇒ Object

Access streams by ID, with 0 returning the connection itself.



62
63
64
65
66
67
68
# File 'lib/protocol/http2/connection.rb', line 62

def [] id
	if id.zero?
		self
	else
		@streams[id]
	end
end

#accept_push_promise_stream(stream_id, &block) ⇒ Object

Accept an incoming push promise from the other side of the connection. On the client side, we accept push promise streams. On the server side, existing streams create push promise streams.



471
472
473
# File 'lib/protocol/http2/connection.rb', line 471

def accept_push_promise_stream(stream_id, &block)
	accept_stream(stream_id, &block)
end

#accept_stream(stream_id, &block) ⇒ Object

Accept an incoming stream from the other side of the connnection. On the server side, we accept requests.



460
461
462
463
464
465
466
# File 'lib/protocol/http2/connection.rb', line 460

def accept_stream(stream_id, &block)
	unless valid_remote_stream_id?(stream_id)
		raise ProtocolError, "Invalid stream id: #{stream_id}"
	end
	
	create_stream(stream_id, &block)
end

#client_stream_id?(id) ⇒ Boolean

Check if the given stream ID represents a client-initiated stream. Client streams always have odd numbered IDs.

Returns:

  • (Boolean)


557
558
559
# File 'lib/protocol/http2/connection.rb', line 557

def client_stream_id?(id)
	id.odd?
end

#close(error = nil) ⇒ Object

Close the underlying framer and all streams.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/protocol/http2/connection.rb', line 139

def close(error = nil)
	# The underlying socket may already be closed by this point.
	
	# If there are active streams when the connection closes, it's an error for those streams, even if the connection itself closed cleanly:
	if @streams.any? and error.nil?
		error = EOFError.new("Connection closed with #{@streams.size} active stream(s)!")
	end
	
	@streams.each_value{|stream| stream.close(error)}
	@streams.clear
	
ensure
	if @framer
		@framer.close
		@framer = nil
	end
end

#close!Object

Transition the connection into the closed state.



244
245
246
247
248
# File 'lib/protocol/http2/connection.rb', line 244

def close!
	@state = :closed
	
	return self
end

#close_if_drained!Object

Transition the connection into the closed state if a graceful GOAWAY was received and there is nothing left to drain.

As with #close!, this is a state transition only: the owner of the connection is responsible for closing the underlying framer.



118
119
120
121
122
# File 'lib/protocol/http2/connection.rb', line 118

def close_if_drained!
	if self.goaway_received? && @streams.empty?
		self.close!
	end
end

#closed?Boolean

Whether the connection is effectively or actually closed.

Returns:

  • (Boolean)


105
106
107
# File 'lib/protocol/http2/connection.rb', line 105

def closed?
	@state == :closed || @framer.nil?
end

#closed_stream_id?(id) ⇒ Boolean

This is only valid if the stream doesn't exist in @streams.

Returns:

  • (Boolean)


591
592
593
594
595
596
597
598
# File 'lib/protocol/http2/connection.rb', line 591

def closed_stream_id?(id)
	if id.zero?
		# The connection "stream id" can never be closed:
		false
	else
		!idle_stream_id?(id)
	end
end

#consume_window(size = self.available_size) ⇒ Object

Traverse active streams and allow them to consume the available flow-control window.



617
618
619
620
621
622
623
624
625
626
# File 'lib/protocol/http2/connection.rb', line 617

def consume_window(size = self.available_size)
	# Return if there is no window to consume:
	return unless size > 0
	
	@streams.each_value do |stream|
		if stream.active?
			stream.window_updated(size)
		end
	end
end

#create_push_promise_stream(&block) ⇒ Object

Create a push promise stream. This method should be overridden by client/server implementations.



499
500
501
# File 'lib/protocol/http2/connection.rb', line 499

def create_push_promise_stream(&block)
	create_stream(&block)
end

#create_stream(id = next_stream_id, &block) ⇒ Stream

Create a stream, defaults to an outgoing stream. On the client side, we create requests.

Returns:

  • (Stream)

    the created stream.



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/protocol/http2/connection.rb', line 478

def create_stream(id = next_stream_id, &block)
	if self.goaway_received? and local_stream_id?(id)
		# Receivers of a GOAWAY frame MUST NOT open additional streams on the connection (RFC 9113 §6.8). A new connection has to be established for new streams.
		raise ProtocolError, "Cannot create stream #{id} after GOAWAY!"
	end
	
	if @streams.key?(id)
		raise ProtocolError, "Cannot create stream with id #{id}, already exists!"
	end
	
	if block_given?
		return yield(self, id)
	else
		return Stream.create(self, id)
	end
end

#decode_headers(data) ⇒ Object

Decode headers using HPACK decompression.



168
169
170
# File 'lib/protocol/http2/connection.rb', line 168

def decode_headers(data)
	HPACK::Decompressor.new(data, @decoder, table_size_limit: @local_settings.header_table_size).decode
end

#delete(id) ⇒ Object

Remove a stream from the active streams collection.

If the remote peer has sent a graceful GOAWAY frame, the connection is only kept open in order to drain the streams it accepted, so when the last one completes there is nothing left to read.



130
131
132
133
134
135
136
# File 'lib/protocol/http2/connection.rb', line 130

def delete(id)
	stream = @streams.delete(id)
	
	self.close_if_drained!
	
	return stream
end

#encode_headers(headers, buffer = String.new.b) ⇒ Object

Encode headers using HPACK compression.



161
162
163
# File 'lib/protocol/http2/connection.rb', line 161

def encode_headers(headers, buffer = String.new.b)
	HPACK::Compressor.new(buffer, @encoder, table_size_limit: @remote_settings.header_table_size).encode(headers)
end

#goaway_received?Boolean

Whether the remote peer has sent us a GOAWAY frame. We must not initiate any new streams on this connection, but existing streams may still be in progress.

Returns:

  • (Boolean)


111
112
113
# File 'lib/protocol/http2/connection.rb', line 111

def goaway_received?
	!@goaway_stream_id.nil?
end

#idObject

The connection stream ID (always 0 for connection-level operations).



55
56
57
# File 'lib/protocol/http2/connection.rb', line 55

def id
	0
end

#idle_stream_id?(id) ⇒ Boolean

Check if the given stream ID represents an idle stream.

Returns:

  • (Boolean)


572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/protocol/http2/connection.rb', line 572

def idle_stream_id?(id)
	if id.even?
		# Server-initiated streams are even.
		if @local_stream_id.even?
			id >= @local_stream_id
		else
			id > @remote_stream_id
		end
	elsif id.odd?
		# Client-initiated streams are odd.
		if @local_stream_id.odd?
			id >= @local_stream_id
		else
			id > @remote_stream_id
		end
	end
end

#ignore_frame?(frame) ⇒ Boolean

6.8. GOAWAY There is an inherent race condition between an endpoint starting new streams and the remote sending a GOAWAY frame. To deal with this case, the GOAWAY contains the stream identifier of the last peer-initiated stream that was or might be processed on the sending endpoint in this connection. For instance, if the server sends a GOAWAY frame, the identified stream is the highest-numbered stream initiated by the client. Once sent, the sender will ignore frames sent on streams initiated by the receiver if the stream has an identifier higher than the included last stream identifier. Receivers of a GOAWAY frame MUST NOT open additional streams on the connection, although a new connection can be established for new streams.

Returns:

  • (Boolean)


190
191
192
193
194
195
196
197
# File 'lib/protocol/http2/connection.rb', line 190

def ignore_frame?(frame)
	if self.closed?
		# puts "ignore_frame? #{frame.stream_id} -> #{valid_remote_stream_id?(frame.stream_id)} > #{@remote_stream_id}"
		if valid_remote_stream_id?(frame.stream_id)
			return frame.stream_id > @remote_stream_id
		end
	end
end

#local_stream_id?(id) ⇒ Boolean

Check if the given stream ID represents a locally-initiated stream. This method should be overridden by client/server implementations.

Returns:

  • (Boolean)


454
455
456
# File 'lib/protocol/http2/connection.rb', line 454

def local_stream_id?(id)
	false
end

#maximum_concurrent_streamsObject

The maximum number of concurrent streams that this connection can initiate. This is a setting that can be changed by the remote peer.

It is not the same as the number of streams that can be accepted by the connection. The number of streams that can be accepted is determined by the local settings, and the number of streams that can be initiated is determined by the remote settings.



78
79
80
# File 'lib/protocol/http2/connection.rb', line 78

def maximum_concurrent_streams
	@remote_settings.maximum_concurrent_streams
end

#maximum_frame_sizeObject

The size of a frame payload is limited by the maximum size that a receiver advertises in the SETTINGS_MAX_FRAME_SIZE setting.



71
72
73
# File 'lib/protocol/http2/connection.rb', line 71

def maximum_frame_size
	@remote_settings.maximum_frame_size
end

#next_stream_idObject

Streams are identified with an unsigned 31-bit integer. Streams initiated by a client MUST use odd-numbered stream identifiers; those initiated by the server MUST use even-numbered stream identifiers. A stream identifier of zero (0x0) is used for connection control messages; the stream identifier of zero cannot be used to establish a new stream.



173
174
175
176
177
178
179
# File 'lib/protocol/http2/connection.rb', line 173

def next_stream_id
	id = @local_stream_id
	
	@local_stream_id += 2
	
	return id
end

#open!Object

Transition the connection to the open state.



374
375
376
377
378
# File 'lib/protocol/http2/connection.rb', line 374

def open!
	@state = :open
	
	return self
end

#process_settings(frame) ⇒ Boolean

In addition to changing the flow-control window for streams that are not yet active, a SETTINGS frame can alter the initial flow-control window size for streams with active flow-control windows (that is, streams in the "open" or "half-closed (remote)" state). When the value of SETTINGS_INITIAL_WINDOW_SIZE changes, a receiver MUST adjust the size of all stream flow-control windows that it maintains by the difference between the new value and the old value.

Returns:

  • (Boolean)

    whether the frame was an acknowledgement



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/protocol/http2/connection.rb', line 349

def process_settings(frame)
	if frame.acknowledgement?
		# The remote end has confirmed the settings have been received:
		changes = @local_settings.acknowledge
		
		update_local_settings(changes)
		
		return true
	else
		# The remote end is updating the settings, we reply with acknowledgement:
		reply = frame.acknowledge
		
		write_frame(reply)
		
		changes = frame.unpack
		@remote_settings.update(changes)
		
		update_remote_settings(changes)
		
		return false
	end
end

#read_frameObject

Reads one frame from the network and processes. Processing the frame updates the state of the connection and related streams. If the frame triggers an error, e.g. a protocol error, the connection will typically emit a goaway frame and re-raise the exception. You should continue processing frames until the underlying connection is closed.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/protocol/http2/connection.rb', line 207

def read_frame
	frame = @framer.read_frame(@local_settings.maximum_frame_size)
	
	# puts "#{self.class} #{@state} read_frame: class=#{frame.class} stream_id=#{frame.stream_id} flags=#{frame.flags} length=#{frame.length} (remote_stream_id=#{@remote_stream_id})"
	# puts "Windows: local_window=#{@local_window.inspect}; remote_window=#{@remote_window.inspect}"
	
	return if ignore_frame?(frame)
	
	yield frame if block_given?
	frame.apply(self)
	
	return frame
rescue GoawayError => error
	# Go directly to jail. Do not pass go, do not collect $200.
	raise
rescue ProtocolError => error
	send_goaway(error.code || PROTOCOL_ERROR, error.message)
	
	raise
rescue HPACK::Error => error
	send_goaway(COMPRESSION_ERROR, error.message)
	
	raise
end

#receive_continuation(frame) ⇒ Object

Receive and process a CONTINUATION frame.

Raises:



652
653
654
# File 'lib/protocol/http2/connection.rb', line 652

def receive_continuation(frame)
	raise ProtocolError, "Received unexpected continuation: #{frame.class}"
end

#receive_data(frame) ⇒ Object

Process a DATA frame from the remote peer.



430
431
432
433
434
435
436
437
438
439
440
# File 'lib/protocol/http2/connection.rb', line 430

def receive_data(frame)
	update_local_window(frame)
	
	if stream = @streams[frame.stream_id]
		stream.receive_data(frame)
	elsif closed_stream_id?(frame.stream_id)
		# This can occur if one end sent a stream reset, while the other end was sending a data frame. It's mostly harmless.
	else
		raise ProtocolError, "Cannot receive data for stream id #{frame.stream_id}"
	end
end

#receive_frame(frame) ⇒ Object

Receive and process a generic frame (default handler).



658
659
660
# File 'lib/protocol/http2/connection.rb', line 658

def receive_frame(frame)
	# ignore.
end

#receive_goaway(frame) ⇒ Object

Process a GOAWAY frame from the remote peer.

A GOAWAY frame with a zero error code is a graceful shutdown: the remote peer will not accept any new streams, but it is still processing the streams at or below last_stream_id and will send their responses (RFC 9113 §6.8). We must keep reading until those streams complete, otherwise requests which the remote peer has already processed - and whose side effects have already happened - fail locally. The connection is closed once the last of those streams completes, or the remote peer closes it.

A GOAWAY frame with a non-zero error code is a connection error: the connection transitions into the closed state and GoawayError is raised.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/protocol/http2/connection.rb', line 268

def receive_goaway(frame)
	# We capture the last locally-initiated stream that may have been processed by the peer.
	goaway_stream_id, error_code, message = frame.unpack
	
	# A peer can send an initial GOAWAY with a high stream ID, followed by another GOAWAY with a lower stream ID. The effective cutoff can only decrease (RFC 9113 §6.8).
	if @goaway_stream_id.nil? || goaway_stream_id < @goaway_stream_id
		@goaway_stream_id = goaway_stream_id
	end
	
	# Locally-initiated streams above the last stream ID were not processed by the remote peer and are safe to retry (RFC 9113 §6.8). They are removed from the connection before being closed, both so that what remains is exactly the set of streams we are waiting on, and so that closing them cannot mutate the collection while we are traversing it.
	refused_streams = @streams.select{|id, stream| local_stream_id?(id) && id > @goaway_stream_id}
	refused_streams.each_key{|id| @streams.delete(id)}
	
	# The state of the connection is decided before any stream is closed, so that it cannot be left undecided by a `closed` hook which raises, and cannot be influenced by one which creates a stream.
	if error_code != 0
		self.close!
	else
		self.close_if_drained!
	end
	
	unless refused_streams.empty?
		error = ::Protocol::HTTP::RefusedError.new("GOAWAY: request not processed.")
		refused_streams.each_value{|stream| stream.close(error)}
	end
	
	if error_code != 0
		raise GoawayError.new(message, error_code)
	end
end

#receive_headers(frame) ⇒ Object

On the server side, starts a new request.



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/protocol/http2/connection.rb', line 504

def receive_headers(frame)
	stream_id = frame.stream_id
	
	if stream_id.zero?
		raise ProtocolError, "Cannot receive headers for stream 0!"
	end
	
	if stream = @streams[stream_id]
		stream.receive_headers(frame)
	else
		if stream_id <= @remote_stream_id
			raise ProtocolError, "Invalid stream id: #{stream_id} <= #{@remote_stream_id}!"
		end
		
		# We need to validate that we have less streams than the specified maximum:
		if @streams.size < @local_settings.maximum_concurrent_streams
			stream = accept_stream(stream_id)
			@remote_stream_id = stream_id
			
			stream.receive_headers(frame)
		else
			raise ProtocolError, "Exceeded maximum concurrent streams"
		end
	end
end

#receive_ping(frame) ⇒ Object

Process a PING frame from the remote peer.



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/protocol/http2/connection.rb', line 410

def receive_ping(frame)
	if @state != :closed
		# This is handled in `read_payload`:
		# if frame.stream_id != 0
		# 	raise ProtocolError, "Ping received for non-zero stream!"
		# end
		
		unless frame.acknowledgement?
			reply = frame.acknowledge
			
			write_frame(reply)
		end
	else
		raise ProtocolError, "Cannot receive ping in state #{@state}"
	end
end

#receive_priority_update(frame) ⇒ Object

Receive and process a PRIORITY_UPDATE frame.



540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/protocol/http2/connection.rb', line 540

def receive_priority_update(frame)
	if frame.stream_id != 0
		raise ProtocolError, "Invalid stream id: #{frame.stream_id}"
	end
	
	stream_id, value = frame.unpack
	
	# Apparently you can set the priority of idle streams, but I'm not sure why that makes sense, so for now let's ignore it.
	if stream = @streams[stream_id]
		stream.priority = Protocol::HTTP::Header::Priority.new(value)
	end
end

#receive_push_promise(frame) ⇒ Object

Receive and process a PUSH_PROMISE frame.

Raises:



533
534
535
# File 'lib/protocol/http2/connection.rb', line 533

def receive_push_promise(frame)
	raise ProtocolError, "Unable to receive push promise!"
end

#receive_reset_stream(frame) ⇒ Object

Receive and process a RST_STREAM frame.



603
604
605
606
607
608
609
610
611
612
613
# File 'lib/protocol/http2/connection.rb', line 603

def receive_reset_stream(frame)
	if frame.connection?
		raise ProtocolError, "Cannot reset connection!"
	elsif stream = @streams[frame.stream_id]
		stream.receive_reset_stream(frame)
	elsif closed_stream_id?(frame.stream_id)
		# Ignore.
	else
		raise StreamClosed, "Cannot reset stream #{frame.stream_id}"
	end
end

#receive_settings(frame) ⇒ Object

Receive and process a SETTINGS frame from the remote peer.



383
384
385
386
387
388
389
390
391
392
# File 'lib/protocol/http2/connection.rb', line 383

def receive_settings(frame)
	if @state == :new
		# We transition to :open when we receive acknowledgement of first settings frame:
		open! if process_settings(frame)
	elsif @state != :closed
		process_settings(frame)
	else
		raise ProtocolError, "Cannot receive settings in state #{@state}"
	end
end

#receive_window_update(frame) ⇒ Object

Receive and process a WINDOW_UPDATE frame.



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/protocol/http2/connection.rb', line 630

def receive_window_update(frame)
	if frame.connection?
		super
		
		self.consume_window
	elsif stream = @streams[frame.stream_id]
		begin
			stream.receive_window_update(frame)
		rescue ProtocolError => error
			stream.send_reset_stream(error.code)
		end
	elsif closed_stream_id?(frame.stream_id)
		# Ignore.
	else
		# Receiving any frame other than HEADERS or PRIORITY on a stream in this state (idle) MUST be treated as a connection error of type PROTOCOL_ERROR.
		raise ProtocolError, "Cannot update window of idle stream #{frame.stream_id}"
	end
end

#send_goaway(error_code = 0, message = "") ⇒ Object

Tell the remote end that the connection is being shut down. If the error_code is 0, this is a graceful shutdown. The other end of the connection should not make any new streams, but existing streams may be completed.



251
252
253
254
255
256
257
258
# File 'lib/protocol/http2/connection.rb', line 251

def send_goaway(error_code = 0, message = "")
	frame = GoawayFrame.new
	frame.pack @remote_stream_id, error_code, message
	
	write_frame(frame)
ensure
	self.close!
end

#send_ping(data) ⇒ Object

Send a PING frame to the remote peer.



396
397
398
399
400
401
402
403
404
405
# File 'lib/protocol/http2/connection.rb', line 396

def send_ping(data)
	if @state != :closed
		frame = PingFrame.new
		frame.pack data
		
		write_frame(frame)
	else
		raise ProtocolError, "Cannot send ping in state #{@state}"
	end
end

#send_settings(changes) ⇒ Object

Send updated settings to the remote peer.



234
235
236
237
238
239
240
241
# File 'lib/protocol/http2/connection.rb', line 234

def send_settings(changes)
	@local_settings.append(changes)
	
	frame = SettingsFrame.new
	frame.pack(changes)
	
	write_frame(frame)
end

#server_stream_id?(id) ⇒ Boolean

Check if the given stream ID represents a server-initiated stream. Server streams always have even numbered IDs.

Returns:

  • (Boolean)


565
566
567
# File 'lib/protocol/http2/connection.rb', line 565

def server_stream_id?(id)
	id.even?
end

#synchronizeObject

Execute a block within a synchronized context. This method provides a synchronization primitive for thread safety.



202
203
204
# File 'lib/protocol/http2/connection.rb', line 202

def synchronize
	yield
end

#update_local_settings(changes) ⇒ Object

Update local settings and adjust stream window capacities.



326
327
328
329
330
331
332
333
334
# File 'lib/protocol/http2/connection.rb', line 326

def update_local_settings(changes)
	capacity = @local_settings.initial_window_size
	
	@streams.each_value do |stream|
		stream.local_window.capacity = capacity
	end
	
	@local_window.desired = capacity
end

#update_remote_settings(changes) ⇒ Object

Update remote settings and adjust stream window capacities.



338
339
340
341
342
343
344
# File 'lib/protocol/http2/connection.rb', line 338

def update_remote_settings(changes)
	capacity = @remote_settings.initial_window_size
	
	@streams.each_value do |stream|
		stream.remote_window.capacity = capacity
	end
end

#valid_remote_stream_id?(stream_id) ⇒ Boolean

Check if the given stream ID is valid for remote initiation. This method should be overridden by client/server implementations.

Returns:

  • (Boolean)


446
447
448
# File 'lib/protocol/http2/connection.rb', line 446

def valid_remote_stream_id?(stream_id)
	false
end

#write_frame(frame) ⇒ Object

Write a single frame to the connection.



300
301
302
303
304
305
306
# File 'lib/protocol/http2/connection.rb', line 300

def write_frame(frame)
	synchronize do
		@framer.write_frame(frame)
	end
	
	@framer.flush
end

#write_framesObject

Write multiple frames within a synchronized block.



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/protocol/http2/connection.rb', line 312

def write_frames
	if @framer
		synchronize do
			yield @framer
		end
		
		@framer.flush
	else
		raise EOFError, "Connection closed!"
	end
end