Class: Ionian::Server
- Inherits:
-
Object
- Object
- Ionian::Server
- Defined in:
- lib/ionian/server.rb
Overview
A convenient wrapper for TCP, UDP, and Unix server sockets.
Instance Attribute Summary (collapse)
-
- (Object) interface
readonly
Interface to listen for clients.
-
- (Object) port
readonly
Port number to listen for clients.
-
- (Object) protocol
(also: #protocol?)
readonly
Returns a symbol of the type of protocol this socket uses: :tcp, :udp, :unix.
Instance Method Summary (collapse)
-
- (Object) close
Shutdown the server socket and stop listening for connections.
-
- (Boolean) closed?
Returns true if the server listener socket is closed.
-
- (Server) initialize(**kwargs, &block)
constructor
A convenient wrapper for TCP and Unix server sockets (UDP doesn't use a server).
-
- (Object) listen(&block)
Starts the socket server listening for connections.
-
- (Object) register_accept_listener(&block)
(also: #on_accept)
Register a block to be run when server accepts a client connection.
-
- (Object) unregister_accept_listener(proc)
Unregisters a socket accept notifier block.
Constructor Details
- (Server) initialize(**kwargs, &block)
A convenient wrapper for TCP and Unix server sockets (UDP doesn't use a server).
Accepts an optional block that is passed to #register_accept_listener. Server opens listening socket on instantiation if this block is provided.
Args:
port: Port number to listen for clients.
interface: The address of the network interface to bind to.
Defaults to all.
protocol: :tcp, :unix. Default is :tcp.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ionian/server.rb', line 32 def initialize **kwargs, &block @accept_listeners = [] register_accept_listener &block if block_given? # @interface = kwargs.fetch :interface, nil @interface = kwargs.fetch :interface, nil @port = kwargs.fetch :port, nil # Automatically select UDP for the multicast range. Otherwise default to TCP. default_protocol = :tcp if @interface # TODO: This ivar may be incorrect for UDP -- bound interface is not destination. default_protocol = :udp if Ionian::Extension::Socket.multicast? @interface default_protocol = :unix if @interface.start_with? '/' end @protocol = kwargs.fetch :protocol, default_protocol # TODO: Move this to #listen. case @protocol when :tcp @interface ||= '0.0.0.0' # All interfaces. # Parse host out of "host:port" if specified. host_port_ary = @interface.to_s.split ':' @interface = host_port_ary[0] @port ||= host_port_ary[1] # TODO: Parse port from interface if TCP. raise ArgumentError, "Port not specified." unless @port @port = @port.to_i @server = TCPServer.new @interface, @port @server.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [1].pack('i') when :udp raise ArgumentError, "UDP should be implemented with Ionian::Socket." when :unix raise ArgumentError, "Path not specified." unless @interface @server = UNIXServer.new @interface end listen if block_given? end |
Instance Attribute Details
- (Object) interface (readonly)
Interface to listen for clients.
10 11 12 |
# File 'lib/ionian/server.rb', line 10 def interface @interface end |
- (Object) port (readonly)
Port number to listen for clients.
13 14 15 |
# File 'lib/ionian/server.rb', line 13 def port @port end |
- (Object) protocol (readonly) Also known as: protocol?
Returns a symbol of the type of protocol this socket uses: :tcp, :udp, :unix
17 18 19 |
# File 'lib/ionian/server.rb', line 17 def protocol @protocol end |
Instance Method Details
- (Object) close
Shutdown the server socket and stop listening for connections.
106 107 108 109 110 |
# File 'lib/ionian/server.rb', line 106 def close @server.close if @server @accept_thread.join if @accept_thread @accept_thread = nil end |
- (Boolean) closed?
Returns true if the server listener socket is closed.
113 114 115 |
# File 'lib/ionian/server.rb', line 113 def closed? @server.closed? end |
- (Object) listen(&block)
Starts the socket server listening for connections. Blocks registered with #register_accept_listener will be run when a connection is accepted.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ionian/server.rb', line 84 def listen &block register_accept_listener &block if block_given? @accept_thread ||= Thread.new do # Package in an Ionian::Socket begin client = Ionian::Socket.new @server.accept @accept_listeners.each do |listener| listener.call client end rescue Errno::EBADF # This ignores the connection if the client closed it before it # could be accepted. rescue IOError # This ignores the connection if the client closed it before it # could be accepted. end end end |
- (Object) register_accept_listener(&block) Also known as: on_accept
Register a block to be run when server accepts a client connection. The connected client is passed to the block as an Ionain::Client.
119 120 121 122 |
# File 'lib/ionian/server.rb', line 119 def register_accept_listener &block @accept_listeners << block unless @accept_listeners.include? block block end |
- (Object) unregister_accept_listener(proc)
Unregisters a socket accept notifier block.
127 128 129 130 |
# File 'lib/ionian/server.rb', line 127 def unregister_accept_listener proc @accept_listeners.delete_if { |o| o == proc } proc end |