lib/ionian/server.rb in ionian-0.6.6 vs lib/ionian/server.rb in ionian-0.6.7
- old
+ new
@@ -1,8 +1,11 @@
require 'socket'
require 'ionian/socket'
+Thread.abort_on_exception = true
+
+
module Ionian
# A convenient wrapper for TCP, UDP, and Unix server sockets.
class Server
@@ -57,11 +60,10 @@
# 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')
@@ -83,30 +85,29 @@
# be run when a connection is accepted.
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
+ loop do
+ # Package in an Ionian::Socket
+ begin
+ client = Ionian::Socket.new @server.accept
+ @accept_listeners.each { |listener| listener.call client }
+ 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
- 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
# Shutdown the server socket and stop listening for connections.
def close
@server.close if @server
- @accept_thread.join if @accept_thread
+ @accept_thread.kill if @accept_thread
@accept_thread = nil
end
# Returns true if the server listener socket is closed.
def closed?
\ No newline at end of file