Class: Reel::Server
- Inherits:
-
Object
- Object
- Reel::Server
- Includes:
- Celluloid::IO
- Defined in:
- lib/reel/server.rb
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_BACKLOG =
How many connections to backlog in the TCP accept queue
100
Instance Method Summary collapse
- #handle_connection(socket) ⇒ Object
-
#initialize(host, port, backlog = DEFAULT_BACKLOG, &callback) ⇒ Server
constructor
A new instance of Server.
- #run ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(host, port, backlog = DEFAULT_BACKLOG, &callback) ⇒ Server
Returns a new instance of Server.
11 12 13 14 15 16 17 18 |
# File 'lib/reel/server.rb', line 11 def initialize(host, port, backlog = DEFAULT_BACKLOG, &callback) # This is actually an evented Celluloid::IO::TCPServer @server = TCPServer.new(host, port) @server.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) @server.listen(backlog) @callback = callback async.run end |
Instance Method Details
#handle_connection(socket) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/reel/server.rb', line 28 def handle_connection(socket) connection = Connection.new(socket) begin @callback.call(connection) ensure if connection.attached? connection.close rescue nil end end rescue RequestError, EOFError # Client disconnected prematurely # TODO: log this? end |
#run ⇒ Object
24 25 26 |
# File 'lib/reel/server.rb', line 24 def run loop { async.handle_connection @server.accept } end |
#shutdown ⇒ Object
20 21 22 |
# File 'lib/reel/server.rb', line 20 def shutdown @server.close if @server end |