class Ionian::Server
A convenient wrapper for TCP, UDP, and Unix server sockets.
Public Class Methods
new(**kwargs, &block)
click to toggle source
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:
interface: The address of the network interface to bind to. Defaults to all. protocol: :tcp, :unix. Default is :tcp.
# File lib/ionian/server.rb, line 19 def initialize **kwargs, &block @accept_listeners = [] register_accept_listener &block if block_given? @interface = kwargs.fetch :interface, '' @port = kwargs.fetch :port, nil # Automatically select UDP for the multicast range. Otherwise default to TCP. default_protocol = :tcp # 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? '/' @protocol = kwargs.fetch :protocol, default_protocol # TODO: Move this to #listen. case @protocol when :tcp @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 @server = UNIXServer.new @interface end listen if block_given? end
Public Instance Methods
close()
click to toggle source
Shutdown the server socket and stop listening for connections.
# File lib/ionian/server.rb, line 74 def close @server.close if @server @accept_thread.join if @accept_thread @accept_thread = nil end
closed?()
click to toggle source
Returns true if the server listener socket is closed.
# File lib/ionian/server.rb, line 81 def closed? @server.closed? end
listen(&block)
click to toggle source
Starts the socket server listening for connections. Blocks registered with register_accept_listener will be run when a connection is accepted.
# File lib/ionian/server.rb, line 52 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
register_accept_listener(&block)
click to toggle source
Register a block to be run when server accepts a client connection. The connected client is passed to the block as an Ionain::Client.
# File lib/ionian/server.rb, line 87 def register_accept_listener &block @accept_listeners << block unless @accept_listeners.include? block block end
Also aliased as: on_accept
unregister_accept_listener(proc)
click to toggle source
Unregisters a socket accept notifier block.
# File lib/ionian/server.rb, line 95 def unregister_accept_listener proc @accept_listeners.delete_if {|o| o == proc} proc end