Class: Rlyeh::Connection
- Inherits:
-
Object
- Object
- Rlyeh::Connection
- Extended by:
- Forwardable
- Defined in:
- lib/rlyeh/connection.rb
Constant Summary collapse
- BUFFER_SIZE =
4096
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
- #attach(session) ⇒ Object
- #attached? ⇒ Boolean
- #close ⇒ Object
- #detach(session) ⇒ Object
-
#initialize(server, socket) ⇒ Connection
constructor
A new instance of Connection.
- #process(data) ⇒ Object
- #read_each(&block) ⇒ Object
- #run ⇒ Object
- #send_data(data, multicast = true) ⇒ Object
Methods included from Worker
included, #invoke, setup, worker_pool
Methods included from Sender
#send_message, #send_numeric_reply
Methods included from Logger
Constructor Details
#initialize(server, socket) ⇒ Connection
Returns a new instance of Connection.
22 23 24 25 26 27 28 29 |
# File 'lib/rlyeh/connection.rb', line 22 def initialize(server, socket) @server = server @socket = socket _, @port, @host = @socket.peeraddr @app = @server.app_class.new nil debug "Connection started: #{@host}:#{@port}" end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
16 17 18 |
# File 'lib/rlyeh/connection.rb', line 16 def app @app end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
16 17 18 |
# File 'lib/rlyeh/connection.rb', line 16 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
16 17 18 |
# File 'lib/rlyeh/connection.rb', line 16 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
15 16 17 |
# File 'lib/rlyeh/connection.rb', line 15 def server @server end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
16 17 18 |
# File 'lib/rlyeh/connection.rb', line 16 def session @session end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
15 16 17 |
# File 'lib/rlyeh/connection.rb', line 15 def socket @socket end |
Instance Method Details
#attach(session) ⇒ Object
97 98 99 |
# File 'lib/rlyeh/connection.rb', line 97 def attach(session) @session = session end |
#attached? ⇒ Boolean
105 106 107 |
# File 'lib/rlyeh/connection.rb', line 105 def attached? !!@session end |
#close ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rlyeh/connection.rb', line 31 def close @socket.close unless @socket.closed? if attached? @session.detach self if @session.empty? @session.close @server.sessions.delete @session.id end end debug "Connection closed: #{@host}:#{@port}" end |
#detach(session) ⇒ Object
101 102 103 |
# File 'lib/rlyeh/connection.rb', line 101 def detach(session) @session = nil end |
#process(data) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rlyeh/connection.rb', line 71 def process(data) env = Rlyeh::Environment.new env.version = Rlyeh::VERSION env.data = data env.server = @server env.connection = self env.settings = @server.app_class.settings catch :halt do begin @app.call env rescue Exception => e crash e end end end |
#read_each(&block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rlyeh/connection.rb', line 56 def read_each(&block) loop do @buffer ||= ''.force_encoding('ASCII-8BIT') @buffer << @socket.readpartial(BUFFER_SIZE).force_encoding('ASCII-8BIT') while data = @buffer.slice!(/(.+)\n/, 1) block.call data.chomp if block end end rescue EOFError => e # client disconnected rescue Celluloid::Task::TerminatedError # kill a running task end |
#run ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/rlyeh/connection.rb', line 46 def run catch :quit do read_each do |data| invoke do process data end end end end |
#send_data(data, multicast = true) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/rlyeh/connection.rb', line 88 def send_data(data, multicast = true) data = data.to_s if multicast && attached? @session.send_data data else @socket.write data end end |