Sha256: b60290b19103316911d02b9a712a9bc5b6455c999eb2af4fe9a0ddf81febaf3d

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module Ftpd
  class Server

    include Memoizer

    # The interface to bind to (e.g. "127.0.0.1", "0.0.0.0",
    # "10.0.0.12", etc.).  Defaults to "localhost"
    # Changes made after #start have no effect.

    attr_accessor :interface

    # The port to bind to.  Defaults to 0, which causes an ephemeral
    # port to be used.  When bound to an ephemeral port, use
    # #bound_port to find out which port was actually bound to.
    # Changes made after #start have no effect.

    attr_accessor :port

    def initialize
      @interface = 'localhost'
      @port = 0
    end

    # The port the server is bound to.  Must be called until after
    # #start is called.

    def bound_port
      @server_socket.addr[1]
    end

    # Start the server.  This creates the server socket, and the
    # thread to service it.

    def start
      @server_socket = make_server_socket
      @server_thread = make_server_thread
    end

    # Stop the server.  This closes the server socket, which in turn
    # stops the thread.

    def stop
      @server_socket.close
    end

    private

    def make_server_socket
      return TCPServer.new(@interface, @port)
    end

    def make_server_thread
      Thread.new do
        Thread.abort_on_exception = true
        loop do
          begin
            begin
              socket = accept
            rescue Errno::EAGAIN, Errno::ECONNABORTED, Errno::EPROTO, Errno::EINVAL
              IO.select([@server_socket])
              sleep(0.2)
              retry
            end
            start_session_thread socket
          rescue IOError
            break
          end
        end
      end
    end

    def start_session_thread(socket)
      Thread.new do
        begin
          session(socket)
        ensure
          socket.close
        end
      end
    end

    def accept
      @server_socket.accept
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ftpd-0.2.1 lib/ftpd/server.rb
ftpd-0.2.0 lib/ftpd/server.rb
ftpd-0.1.1 lib/ftpd/server.rb