aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/connection_utils.rb
blob: da89edf841190d34101a7c4a2fd285c90274253b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Plum::BinaryString

module Plum
  module ConnectionUtils
    # Sends local settings to the peer.
    #
    # @param kwargs [Hash<Symbol, Integer>]
    def settings(**kwargs)
      send_immediately Frame.settings(**kwargs)
      update_local_settings(kwargs)
    end

    # Sends a PING frame to the peer.
    #
    # @param data [String] Must be 8 octets.
    # @raise [ArgumentError] If the data is not 8 octets.
    def ping(data = "plum\x00\x00\x00\x00")
      send_immediately Frame.ping(data)
    end

    # Sends GOAWAY frame to the peer and closes the connection.
    #
    # @param error_type [Symbol] The error type to be contained in the GOAWAY frame.
    def goaway(error_type = :no_error)
      last_id = @streams.keys.max || 0
      send_immediately Frame.goaway(last_id, error_type)
    end

    private
    def update_local_settings(new_settings)
      old_settings = @local_settings.dup
      @local_settings.merge!(new_settings)

      @hpack_decoder.limit = @local_settings[:header_table_size]
      update_recv_initial_window_size(@local_settings[:initial_window_size] - old_settings[:initial_window_size])
    end
  end
end