Class: H2::Server::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/h2/server/connection.rb

Overview

handles reading data from the @socket into the HTTP2::Server @parser, callbacks from the @parser, and closing of the @socket

Constant Summary collapse

PARSER_EVENTS =

each @parser event method is wrapped in a block to call a local instance method of the same name

[
  :frame,
  :stream,
  :goaway
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket:, server:) {|_self| ... } ⇒ Connection

Returns a new instance of Connection.

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/h2/server/connection.rb', line 24

def initialize socket:, server:
  @socket   = socket
  @server   = server
  @parser   = ::HTTP2::Server.new
  @attached = true

  yield self if block_given?

  bind_events

  Logger.debug "new H2::Connection: #{self}" if H2.verbose?
end

Instance Attribute Details

#parserObject (readonly)

include FrameDebugger



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

def parser
  @parser
end

#serverObject (readonly)

include FrameDebugger



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

def server
  @server
end

#socketObject (readonly)

include FrameDebugger



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

def socket
  @socket
end

Instance Method Details

#attached?Boolean

is this connection still attached to the server reactor?

Returns:

  • (Boolean)


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

def attached?
  @attached
end

#bind_eventsObject

bind parser events to this instance



45
46
47
48
49
50
# File 'lib/h2/server/connection.rb', line 45

def bind_events
  PARSER_EVENTS.each do |e|
    on = "on_#{e}".to_sym
    @parser.on(e) { |x| __send__ on, x }
  end
end

#closeObject

closes this connection’s socket if attached



54
55
56
# File 'lib/h2/server/connection.rb', line 54

def close
  socket.close if socket && attached?
end

#closed?Boolean

is this connection’s socket closed?

Returns:

  • (Boolean)


60
61
62
# File 'lib/h2/server/connection.rb', line 60

def closed?
  socket.closed?
end

#detachObject

prevent this server reactor from handling this connection



66
67
68
69
# File 'lib/h2/server/connection.rb', line 66

def detach
  @attached = false
  self
end

#each_stream(&block) ⇒ Object

accessor for stream handler



73
74
75
76
# File 'lib/h2/server/connection.rb', line 73

def each_stream &block
  @each_stream = block if block_given?
  @each_stream
end

#goawayObject

queue a goaway frame



80
81
82
# File 'lib/h2/server/connection.rb', line 80

def goaway
  server.async.goaway self
end

#readObject

begins the read loop, handling all errors with a log message, backtrace, and closing the @socket



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/h2/server/connection.rb', line 87

def read
  begin
    while attached? && !@socket.closed? && !(@socket.eof? rescue true)
      data = @socket.readpartial(4096)
      @parser << data
    end
    close

  rescue => e
    Logger.error "Exception: #{e.message} - closing socket"
    STDERR.puts e.backtrace if H2.verbose?
    close

  end
end