]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/ircsocket.rb
core/config: remove leftover heavy-load debug line
[user/henk/code/ruby/rbot.git] / lib / rbot / ircsocket.rb
index e3586b70f49f07a0c1407e90f518070a097d7a61..4c6c74bb9e0140d70d288fd7f88266a6f40aface 100644 (file)
@@ -285,10 +285,8 @@ module Irc
     # host::   optional local host to bind to (ruby 1.7+ required)
     # create a new IrcSocket
     def initialize(server_list, host, sendq_delay=2, sendq_burst=4, opts={})
-      @timer = Timer::Timer.new
-      @timer.add(0.2) do
-        spool
-      end
+      @timer = Timer.new
+      @act_id = @timer.add(0.2, :blocked => true) { spool }
       @server_list = server_list.dup
       @server_uri = nil
       @conn_count = 0
@@ -331,7 +329,7 @@ module Irc
         return
       end
       srv_uri = @server_list[@conn_count % @server_list.size].dup
-      srv_uri = 'irc://' + srv_uri if !srv_uri =~ /:\/\//
+      srv_uri = 'irc://' + srv_uri if !(srv_uri =~ /:\/\//)
       @conn_count += 1
       @server_uri = URI.parse(srv_uri)
       @server_uri.port = 6667 if !@server_uri.port
@@ -370,9 +368,9 @@ module Irc
         @sendq_delay = newfreq
         if newfreq == 0
           clearq
-          @timer.stop
+          @timer.block(@act_id)
         else
-          @timer.start
+          @timer.unblock(@act_id)
         end
       end
     end
@@ -395,13 +393,12 @@ module Irc
       end
     end
 
-    def handle_socket_error(string, err)
-      error "#{string} failed: #{err.inspect}"
-      debug err.backtrace.join("\n")
+    def handle_socket_error(string, e)
+      error "#{string} failed: #{e.pretty_inspect}"
       # We assume that an error means that there are connection
       # problems and that we should reconnect, so we
       shutdown
-      raise SocketError.new(err.inspect)
+      raise SocketError.new(e.inspect)
     end
 
     # get the next line from the server (blocks)
@@ -416,7 +413,7 @@ module Irc
         reply.strip! if reply
         debug "RECV: #{reply.inspect}"
         return reply
-      rescue => e
+      rescue Exception => e
         handle_socket_error(:RECV, e)
       end
     end
@@ -425,7 +422,7 @@ module Irc
       if @sendq_delay > 0
         @qmutex.synchronize do
           @sendq.push msg, chan, ring
-          @timer.start
+          @timer.unblock(@act_id)
         end
       else
         # just send it if queueing is disabled
@@ -439,7 +436,7 @@ module Irc
         begin
           debug "in spooler"
           if @sendq.empty?
-            @timer.stop
+            @timer.block(@act_id)
             return
           end
           now = Time.new
@@ -449,7 +446,7 @@ module Irc
           elsif (@burst > @sendq_burst)
             # nope. can't send anything, come back to us next tick...
             debug "can't send yet"
-            @timer.start
+            @timer.unblock(@act_id)
             return
           end
           @flood_send = now if @flood_send < now
@@ -459,11 +456,11 @@ module Irc
             puts_critical(@sendq.shift, true)
           end
           if @sendq.empty?
-            @timer.stop
+            @timer.block(@act_id)
           end
-        rescue => e
-          error "Spooling failed: #{e.inspect}"
-          error e.backtrace.join("\n")
+        rescue Exception => e
+          error "Spooling failed: #{e.pretty_inspect}"
+          raise e
         end
       end
     end
@@ -495,13 +492,13 @@ module Irc
       return unless connected?
       begin
         @sock.close
-      rescue => err
-        error "error while shutting down: #{err.inspect}"
-        debug err.backtrace.join("\n")
+      rescue Exception => e
+        error "error while shutting down: #{e.pretty_inspect}"
       end
       @rawsock = nil if @ssl
       @sock = nil
       @burst = 0
+      @sendq.clear
     end
 
     private
@@ -520,7 +517,7 @@ module Irc
           @lines_sent += 1
           @burst += 1
         end
-      rescue => e
+      rescue Exception => e
         handle_socket_error(:SEND, e)
       end
     end