lib/ruby-growl/udp.rb in ruby-growl-4.0 vs lib/ruby-growl/udp.rb in ruby-growl-4.1

- old
+ new

@@ -117,13 +117,11 @@ # # +password+ is the password needed to send notifications to +host+. def initialize(host, app_name, all_notifies, default_notifies = nil, password = nil) - @socket = UDPSocket.open - # FIXME This goes somewhere else - @socket.connect host, PORT + @socket = socket host @app_name = app_name @all_notifies = all_notifies @default_notifies = default_notifies.nil? ? all_notifies : default_notifies @password = password @@ -258,9 +256,29 @@ #-- # Is this truly necessary? def set_sndbuf(length) @socket.setsockopt Socket::SOL_SOCKET, Socket::SO_SNDBUF, length + end + + def socket host + addrinfo = Addrinfo.udp host, PORT + + socket = Socket.new addrinfo.pfamily, addrinfo.socktype, addrinfo.protocol + + if addrinfo.ip_address == '255.255.255.255' then + socket.setsockopt :SOL_SOCKET, :SO_BROADCAST, true + elsif Socket.respond_to?(:getifaddrs) and + Socket.getifaddrs.any? do |ifaddr| + ifaddr.broadaddr and + ifaddr.broadaddr.ip_address == addrinfo.ip_address + end then + socket.setsockopt :SOL_SOCKET, :SO_BROADCAST, true + end + + socket.connect addrinfo + + socket end end