7 # wrapped TCPSocket for communication with the server.
8 # emulates a subset of TCPSocket functionality
10 # total number of lines sent to the irc server
11 attr_reader :lines_sent
13 # total number of lines received from the irc server
14 attr_reader :lines_received
16 # total number of bytes sent to the irc server
17 attr_reader :bytes_sent
19 # total number of bytes received from the irc server
20 attr_reader :bytes_received
22 # accumulator for the throttle
23 attr_reader :throttle_bytes
26 attr_reader :bytes_per
27 attr_reader :seconds_per
29 # delay between lines sent
30 attr_reader :sendq_delay
33 attr_reader :sendq_burst
35 # server:: server to connect to
37 # host:: optional local host to bind to (ruby 1.7+ required)
38 # create a new IrcSocket
39 def initialize(server, port, host, sendq_delay=2, sendq_burst=4, brt="400/2")
40 @timer = Timer::Timer.new
52 @sendq_delay = sendq_delay.to_f
56 @last_send = Time.new - @sendq_delay
57 @last_throttle = Time.new
60 @sendq_burst = sendq_burst.to_i
71 if brt.match(/(\d+)\/(\d)/)
73 @seconds_per = $2.to_i
74 debug "Byterate now #{byterate}"
77 debug "Couldn't set byterate #{brt}"
86 # open a TCP connection to the server
89 debug "reconnecting socket while connected"
94 @sock=TCPSocket.new(@server, @port, @host)
95 rescue ArgumentError => e
96 $stderr.puts "Your version of ruby does not support binding to a "
97 $stderr.puts "specific local address, please upgrade if you wish "
98 $stderr.puts "to use HOST = foo"
99 $stderr.puts "(this option has been disabled in order to continue)"
100 @sock=TCPSocket.new(@server, @port)
103 @sock=TCPSocket.new(@server, @port)
110 def sendq_delay=(newfreq)
111 debug "changing sendq frequency to #{newfreq}"
112 @qmutex.synchronize do
113 @sendq_delay = newfreq
123 def sendq_burst=(newburst)
124 @qmutex.synchronize do
125 @sendq_burst = newburst
130 return "#{@bytes_per}/#{@seconds_per}"
133 def byterate=(newrate)
134 @qmutex.synchronize do
139 # used to send lines to the remote IRCd
140 # message: IRC message to send
142 @qmutex.synchronize do
143 # debug "In puts - got mutex"
144 puts_critical(message)
148 # get the next line from the server (blocks)
151 debug "socket get attempted while closed"
157 reply.strip! if reply
158 debug "RECV: #{reply.inspect}"
161 debug "socket get failed: #{e.inspect}"
168 @qmutex.synchronize do
173 # just send it if queueing is disabled
178 # pop a message off the queue, send it
185 if @throttle_bytes > 0
186 delta = ((now - @last_throttle)*@bytes_per/@seconds_per).floor
188 @throttle_bytes -= delta
189 @throttle_bytes = 0 if @throttle_bytes < 0
193 if (now >= (@last_send + @sendq_delay))
194 # reset burst counter after @sendq_delay has passed
196 debug "in spool, resetting @burst"
197 elsif (@burst >= @sendq_burst)
198 # nope. can't send anything, come back to us next tick...
202 @qmutex.synchronize do
203 debug "(can send #{@sendq_burst - @burst} lines, there are #{@sendq.length} to send)"
204 (@sendq_burst - @burst).times do
205 break if @sendq.empty?
207 if @throttle_bytes == 0 or mess.length+@throttle_bytes < @bytes_per
208 puts_critical(@sendq.shift)
210 debug "(flood protection: throttling message of length #{mess.length})"
211 debug "(byterate: #{byterate}, throttle bytes: #{@throttle_bytes})"
224 @qmutex.synchronize do
229 debug "Clearing socket while disconnected"
233 # flush the TCPSocket
238 # Wraps Kernel.select on the socket
239 def select(timeout=nil)
240 Kernel.select([@sock], nil, nil, timeout)
243 # shutdown the connection to the server
245 @sock.shutdown(how) unless @sock.nil?
251 # same as puts, but expects to be called with a mutex held on @qmutex
252 def puts_critical(message)
253 # debug "in puts_critical"
254 debug "SEND: #{message.inspect}"
255 @sock.send(message + "\n",0)
256 @last_send = Time.new
259 @throttle_bytes += message.length + 1
260 @last_throttle = Time.new