Sha256: cfb4c2783da3e56ed67c525b503c0daab09623cc0ef60bccc2811e4f27e8c1e0

Contents?: true

Size: 1011 Bytes

Versions: 7

Compression:

Stored size: 1011 Bytes

Contents

module Reel
  class Server
    include Celluloid::IO

    # How many connections to backlog in the TCP accept queue
    DEFAULT_BACKLOG = 100

    execute_block_on_receiver :initialize
    finalizer :shutdown

    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

    def shutdown
      @server.close if @server
    end

    def run
      loop { async.handle_connection @server.accept }
    end

    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
reel-0.4.0 lib/reel/server.rb
reel-0.4.0.pre7 lib/reel/server.rb
reel-0.4.0.pre6 lib/reel/server.rb
reel-0.4.0.pre5 lib/reel/server.rb
reel-0.4.0.pre4 lib/reel/server.rb
reel-0.4.0.pre3 lib/reel/server.rb
reel-0.4.0.pre2 lib/reel/server.rb