Skip to content

Instantly share code, notes, and snippets.

@xumingyong
Created February 23, 2012 07:09
Show Gist options
  • Select an option

  • Save xumingyong/1891239 to your computer and use it in GitHub Desktop.

Select an option

Save xumingyong/1891239 to your computer and use it in GitHub Desktop.
tips on EventMachine::start_server
# Following comments on eventmachine.rb - V1.0.0-beta4.1
# at line:502
def self.start_server server, port=nil, handler=nil, *args, &block
begin
port = Integer(port)
rescue ArgumentError, TypeError
# there was no port, so server must be a unix domain socket
# the port argument is actually the handler, and the handler is one of the args
args.unshift handler if handler
handler = port
port = nil
end if port
klass = klass_from_handler(Connection, handler, *args)
s = if port
# ===============================================================
# all secret is in [pure_ruby.rb] file.
# start_tcp_server() is a singlton method of EventMachine module:
# def start_tcp_server host, port
# (s = EvmaTCPServer.start_server host, port) or raise "no acceptor"
# s.uuid
# end
# EvmaTCPServer is a class under EventMachine,
# start_server() is a singleton method, see its definition
# class EvmaTCPServer < Selectable
# class << self
# def start_server host, port
# sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
# sd.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true )
# sd.bind( Socket.pack_sockaddr_in( port, host ))
# sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough.
# # EvmaTCPServer.new(sd) call EvmaTCPServer.initialize(io),
# # which inheritted from class Selectable,
# # and set the socket fd attribute.
# EvmaTCPServer.new sd
# end
# end
# end
# Hah,
# ===============================================================
start_tcp_server server, port
else
start_unix_server server
end
@acceptors[s] = [klass,args,block]
# ================================================================
# returnd variable <s> is returned by EvmaTCPServer#start_tcp_server,
# which is a uuid, increase from 0
# ================================================================
s
end
# at line:1459
def self.klass_from_handler(klass = Connection, handler = nil, *args)
klass = if handler and handler.is_a?(Class)
raise ArgumentError, "must provide module or subclass of #{klass.name}" unless klass >= handler
handler
# ===============================================================
# Here, means that handler is a module
# then a anoymous class initilized which mix-in handler module
# and set :EM_CONNECTION_CLASS const to this new anonymous class
# ===============================================================
elsif handler
begin
handler::EM_CONNECTION_CLASS
rescue NameError
handler::const_set(:EM_CONNECTION_CLASS, Class.new(klass) {include handler})
end
else
klass
end
arity = klass.instance_method(:initialize).arity
expected = arity >= 0 ? arity : -(arity + 1)
if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
end
klass
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment