Class: Swee::Connection

Inherits:
EventMachine::Connection
  • Object
show all
Defined in:
lib/swee/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#requestObject

Returns the value of attribute request.



6
7
8
# File 'lib/swee/connection.rb', line 6

def request
  @request
end

#responseObject

Returns the value of attribute response.



7
8
9
# File 'lib/swee/connection.rb', line 7

def response
  @response
end

#serverObject

Returns the value of attribute server.



5
6
7
# File 'lib/swee/connection.rb', line 5

def server
  @server
end

Instance Method Details

#post_initObject

EM 初始化请求



10
11
12
13
# File 'lib/swee/connection.rb', line 10

def post_init
  @request  = ::Thin::Request.new
  @response = ::Thin::Response.new
end

#receive_data(data) ⇒ Object

EM 接收 data



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/swee/connection.rb', line 16

def receive_data(data)
  EM.defer do  
    # thin -> 解析 http
    @request.parse(data)

    # 获取 env
    env = @request.env

    # 默认 middlewave 先被执行
    app = AppExecutor.method(:run)
    # 代码重新加载
    app = Reloader.new(app,Engine.server.logger) if @server.code_reload
    # rack -> 代码异常
    app = Rack::ShowExceptions.new(app)
    # rack -> 请求日志
    app = Rack::CommonLogger.new(app,Engine.server.logger)
    # rack -> 自动分配 Content-Encoding
    app = Rack::Deflater.new(app)
    # 计算 content-length
    app = ContentLength.new(app)

    @response.status, @response.headers, @response.body = app.call(env)
    @response.each do |chunk|
      send_data chunk
    end
  end
end

#unbindObject



44
45
46
# File 'lib/swee/connection.rb', line 44

def unbind
  @server.connection_finished(self)
end