Class: Librevox::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handler, endpoint) ⇒ Server

Returns a new instance of Server.



7
8
9
10
# File 'lib/librevox/server.rb', line 7

def initialize(handler, endpoint)
  @handler = handler
  @endpoint = endpoint
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



12
13
14
# File 'lib/librevox/server.rb', line 12

def endpoint
  @endpoint
end

Instance Method Details

#accept(socket, _address) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/librevox/server.rb', line 14

def accept(socket, _address)
  stream = IO::Stream(socket)
  connection = Protocol::Connection.new(stream)

  listener = @handler.new(connection)

  session_task = Async { listener.run_session }
  connection.read_loop { |msg| listener.receive_message(msg) }
rescue => e
  Librevox.logger.error "Session error: #{e.full_message}"
ensure
  session_task&.stop
  connection&.close
end

#runObject



29
30
31
32
33
34
# File 'lib/librevox/server.rb', line 29

def run
  Async do |task|
    @endpoint.accept(&method(:accept))
    task.children.each(&:wait)
  end
end