lib/plezi/eventmachine/io.rb in plezi-0.8.2 vs lib/plezi/eventmachine/io.rb in plezi-0.8.3

- old
+ new

@@ -3,14 +3,14 @@ # a basic IO listening socket wrapper. class BasicIO # attribute readers - attr_reader :io, :params + attr_reader :io, :params, :protocol # initialize the listener with the actual socket and the properties. def initialize io, params - @io, @params = io, params + @io, @params, @protocol = io, params, false end # returns true if the socket is closed and the object can be cleared. def clear? @io.closed? end @@ -27,10 +27,17 @@ rescue => e Plezi.error e end end + def on_disconnect + true + end + def close + false + end + alias :disconnect :close end module_function # the IO stack. EM_IO = {} @@ -45,13 +52,15 @@ # def add_io io, job IO_LOCKER.synchronize { EM_IO[io] = job } end + # the proc for async IO removal, in case of IO exceptions raised by unexpectedly closed sockets. + IO_CLEAR_ASYNC_PROC = Proc.new {|c| c.on_disconnect rescue false } # removes an IO from the event machine. def remove_io io - IO_LOCKER.synchronize { (EM_IO.delete io).close rescue true } + IO_LOCKER.synchronize { queue [(EM_IO.delete io)], IO_CLEAR_ASYNC_PROC; (io.close unless io.closed? rescue true); } end # deletes any connections that are closed or timed out. def clear_connections IO_LOCKER.synchronize { EM_IO.delete_if { |io, c| c.clear? } } @@ -62,11 +71,11 @@ IO_LOCKER.synchronize { EM_IO.clear } end # stops all the connections without stopping the lisntener IO's. def stop_connections - IO_LOCKER.synchronize { EM_IO.each { |io, c| Plezi.run_async { c.disconnect rescue true } } } + IO_LOCKER.synchronize { EM_IO.each { |io, c| c.close } } end # set the default idle waiting time. @io_timeout = 0.1 @@ -86,12 +95,9 @@ # # set the current idle setting def io_timeout= value @io_timeout = value end - - # the proc for async IO removal, in case of IO exceptions raised by unexpectedly closed sockets. - IO_CLEAR_ASYNC_PROC = Proc.new {|c| c.protocol.on_disconnect if (c.protocol rescue false) } # hangs for IO data or io_timeout def review_io return false if IO_LOCKER.locked? IO_LOCKER.synchronize do \ No newline at end of file