Class: Plum::Connection

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

Direct Known Subclasses

HTTPConnection, HTTPSConnection

Constant Summary collapse

CLIENT_CONNECTION_PREFACE =
"PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".freeze
DEFAULT_SETTINGS =
{
  header_table_size:      4096,     # octets
  enable_push:            1,        # 1: enabled, 0: disabled
  max_concurrent_streams: 1 << 30,  # (1 << 31) / 2
  initial_window_size:    65535,    # octets; <= 2 ** 31 - 1
  max_frame_size:         16384,    # octets; <= 2 ** 24 - 1
  max_header_list_size:   (1 << 32) - 1 # Fixnum
}.freeze

Instance Attribute Summary collapse

Attributes included from FlowControl

#recv_remaining_window, #send_remaining_window

Instance Method Summary collapse

Methods included from ConnectionUtils

#goaway, #ping, #push_enabled?, #settings

Methods included from FlowControl

#send, #window_update

Methods included from EventEmitter

#callback, #on

Instance Attribute Details

#hpack_decoderObject (readonly)

Returns the value of attribute hpack_decoder.



20
21
22
# File 'lib/plum/connection.rb', line 20

def hpack_decoder
  @hpack_decoder
end

#hpack_encoderObject (readonly)

Returns the value of attribute hpack_encoder.



20
21
22
# File 'lib/plum/connection.rb', line 20

def hpack_encoder
  @hpack_encoder
end

#local_settingsObject (readonly)

Returns the value of attribute local_settings.



21
22
23
# File 'lib/plum/connection.rb', line 21

def local_settings
  @local_settings
end

#remote_settingsObject (readonly)

Returns the value of attribute remote_settings.



21
22
23
# File 'lib/plum/connection.rb', line 21

def remote_settings
  @remote_settings
end

#stateObject (readonly)

Returns the value of attribute state.



22
23
24
# File 'lib/plum/connection.rb', line 22

def state
  @state
end

#streamsObject (readonly)

Returns the value of attribute streams.



22
23
24
# File 'lib/plum/connection.rb', line 22

def streams
  @streams
end

Instance Method Details

#closeObject

Emits :close event. Doesn’t actually close socket.



41
42
43
44
# File 'lib/plum/connection.rb', line 41

def close
  # TODO: server MAY wait streams
  callback(:close)
end

#receive(new_data) ⇒ Object Also known as: <<

Receives the specified data and process.

Parameters:

  • new_data (String)

    The data received from the peer.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/plum/connection.rb', line 48

def receive(new_data)
  return if new_data.empty?
  @buffer << new_data

  negotiate! if @state == :negotiation

  if @state != :negotiation
    while frame = Frame.parse!(@buffer)
      callback(:frame, frame)
      receive_frame(frame)
    end
  end
rescue ConnectionError => e
  callback(:connection_error, e)
  goaway(e.http2_error_type)
  close
end

#reserve_stream(**args) ⇒ Object

Reserves a new stream to server push.

Parameters:

  • args (Hash)

    The argument to pass to Stram.new.



69
70
71
72
73
# File 'lib/plum/connection.rb', line 69

def reserve_stream(**args)
  next_id = @max_even_stream_id + 2
  stream = new_stream(next_id, state: :reserved_local, **args)
  stream
end