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
36
37
38
39
# File 'lib/h2/server/connection.rb', line 24

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

  # set a default stream handler that raises +NotImplementedError+
  #
  @each_stream = ->(s){ raise NotImplementedError }

  yield self if block_given?

  bind_events

  Logger.debug "new H2::Connection: #{self}"
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)


43
44
45
# File 'lib/h2/server/connection.rb', line 43

def attached?
  @attached
end

#bind_eventsObject

bind parser events to this instance



49
50
51
52
53
54
# File 'lib/h2/server/connection.rb', line 49

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



58
59
60
# File 'lib/h2/server/connection.rb', line 58

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

#closed?Boolean

is this connection’s socket closed?

Returns:

  • (Boolean)


64
65
66
# File 'lib/h2/server/connection.rb', line 64

def closed?
  socket.closed?
end

#detachObject

prevent this server reactor from handling this connection



70
71
72
73
# File 'lib/h2/server/connection.rb', line 70

def detach
  @attached = false
  self
end

#each_stream(&block) ⇒ Object

accessor for stream handler



77
78
79
80
# File 'lib/h2/server/connection.rb', line 77

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

#goawayObject

queue a goaway frame



84
85
86
# File 'lib/h2/server/connection.rb', line 84

def goaway
  server.async.goaway self
end

#readObject

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/h2/server/connection.rb', line 91

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::Logger.level == ::Logger::DEBUG
    close

  end
end