Skip to content
Browse files

server: avoid some possible threading issues

  • Loading branch information...
1 parent d405bf7 commit 5f6c0fdd15ee15b2dc6e586299e32177eeb27214 @meh committed
Showing with 17 additions and 13 deletions.
  1. +8 −11 lib/failirc/server/dispatcher/client.rb
  2. +9 −2 lib/failirc/server/dispatcher/server.rb
View
19 lib/failirc/server/dispatcher/client.rb
@@ -86,16 +86,9 @@ def flush
return if @output.empty? or disconnected?(true)
begin
- @socket.write_nonblock("#{@last}\r\n") if @last
-
until @output.empty?
- @last = @output.pop
- @last.force_encoding 'ASCII-8BIT'
-
- @socket.write_nonblock("#{@last}\r\n")
+ @socket.write_nonblock("#{@output.pop}\r\n")
end
-
- @last = nil
rescue IOError
disconnect 'Input/output error'
rescue Errno::EBADF, Errno::EPIPE, OpenSSL::SSL::SSLError
@@ -112,10 +105,14 @@ def flush
end
end
+ def handling?; @handling; end
+ def handling!; @handling = true; end
+ def handled!; @handling = false; end
+
def handle
- return if disconnected?(true) or @handling or @input.empty?
+ return if disconnected?(true) or handling? or @input.empty?
- @handling = true
+ handling!
server.do {
begin
@@ -124,7 +121,7 @@ def handle
IRC.debug e
end
- @handling = false
+ handled!
dispatcher.wakeup unless @input.empty?
}
View
11 lib/failirc/server/dispatcher/server.rb
@@ -63,14 +63,21 @@ def accept
server.do {
begin
if ssl?
- socket = timeout((self.server.options[:server][:timeout] || 15).to_i) do
+ socket = timeout((server.options[:server][:timeout] || 15).to_i) do
ssl = OpenSSL::SSL::SSLSocket.new(socket, @context)
ssl.accept
ssl
end
end
- server.fire :connect, @clients.push(Dispatcher::Client.new(self, socket)).last
+ client = Dispatcher::Client.new(self, socket)
+
+ client.handling!
+ server.fire :connect, client
+ client.handled!
+
+ @clients.push(client)
+
dispatcher.wakeup reset: true
rescue OpenSSL::SSL::SSLError, Timeout::Error
socket.write_nonblock "This is an SSL connection, faggot.\r\n" rescue nil

0 comments on commit 5f6c0fd

Please sign in to comment.
Something went wrong with that request. Please try again.