def start
begin
BasicSocket::do_not_reverse_lookup=true
server = TCPServer.new(@host, @port)
rescue Exception => e
fatal("Can't open server: #{e.class} #{e}")
exit
end
begin
@serverThread = Thread.new {
while (session = server.accept)
Thread.new(session) do |client|
begin
debug("Handling new connection from #{client.peeraddr[2]}, #{Thread.list.size} total threads ")
@@clientsMutex.synchronize {
@@totalclients += 1
}
connection = OpenStruct.new
connection.client = client
connection.timecreated = Time.new
connection.thread = Thread.current
connection.addr = client.peeraddr
@@connMutex.synchronize {
@connections << connection
debug("Pushed connection thread to @connections, now #{@connections.size} connections")
}
debug("Calling serve on connection")
serve(connection)
client.close
@@connMutex.synchronize {
@connections.delete(connection)
debug("Removed connection from @connections, now #{@connections.size} connections")
}
rescue Errno::ENOTCONN => e
warn("Unexpected disconnection while handling request")
rescue Errno::ECONNRESET => e
warn("Connection reset by peer")
rescue Exception => e
error("Unexpected #{e.class} exception while handling client connection: #{e}")
error("Unexpected #{e.class} exception while handling client connection: #{e.backtrace.join("\n")}")
client.close
end
end
end
}
rescue Exception => e
fatal("Got #{e.class} exception in main listening thread: #{e}")
end
end