def self.event_callback conn_binding, opcode, data
if opcode == ConnectionUnbound
if c = @conns.delete( conn_binding )
begin
if c.original_method(:unbind).arity != 0
c.unbind(data == 0 ? nil : EventMachine::ERRNOS[data])
else
c.unbind
end
if c.instance_variable_defined?(:@io) and !c.instance_variable_get(:@watch_mode)
io = c.instance_variable_get(:@io)
begin
io.close
rescue Errno::EBADF, IOError
end
end
rescue
@wrapped_exception = $!
stop
end
elsif c = @acceptors.delete( conn_binding )
else
if $!
@wrapped_exception = $!
EM.stop
else
raise ConnectionNotBound, "received ConnectionUnbound for an unknown signature: #{conn_binding}"
end
end
elsif opcode == ConnectionAccepted
accep,args,blk = @acceptors[conn_binding]
raise NoHandlerForAcceptedConnection unless accep
c = accep.new data, *args
@conns[data] = c
blk and blk.call(c)
c
elsif opcode == ConnectionCompleted
c = @conns[conn_binding] or raise ConnectionNotBound, "received ConnectionCompleted for unknown signature: #{conn_binding}"
c.connection_completed
elsif opcode == TimerFired
t = @timers.delete( data )
return if t == false
t or raise UnknownTimerFired, "timer data: #{data}"
t.call
elsif opcode == ConnectionData
c = @conns[conn_binding] or raise ConnectionNotBound, "received data #{data} for unknown signature: #{conn_binding}"
c.receive_data data
elsif opcode == LoopbreakSignalled
run_deferred_callbacks
elsif opcode == ConnectionNotifyReadable
c = @conns[conn_binding] or raise ConnectionNotBound
c.notify_readable
elsif opcode == ConnectionNotifyWritable
c = @conns[conn_binding] or raise ConnectionNotBound
c.notify_writable
end
end