Class: Stenohttp2::Server::Server

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/stenohttp2/server/server.rb

Constant Summary collapse

BUFFER_SIZE =
1024

Instance Method Summary collapse

Constructor Details

#initialize(url:) ⇒ Server

Returns a new instance of Server.



15
16
17
18
19
# File 'lib/stenohttp2/server/server.rb', line 15

def initialize(url:)
  @url = url
  @port = @url.port
  @server = ServerFactory.new(@port).start
end

Instance Method Details

#startObject

rubocop:disable Metrics/AbcSize



22
23
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
# File 'lib/stenohttp2/server/server.rb', line 22

def start
  log.info "Server listening on #{url}"

  loop do
    client_connection = server.accept

    Thread.start(client_connection) do |connection|
      connection_handler = ::Stenohttp2::Server::Handler.new(connection).setup

      while !connection.closed? && !(begin
        connection.eof?
      rescue StandardError
        true
      end)
        data = connection.readpartial(BUFFER_SIZE)

        begin
          connection_handler.receive(data)
        rescue StandardError => e
          log.error "#{e.class} exception: #{e.message} - closing connectionet."
          T.must(e.backtrace).each { |l| puts "\t#{l}" }
          connection.close
        end
      end
    end.join
  end
end