Module: EM::SocketConnection
- Includes:
- Deferrable
- Defined in:
- lib/memcache/event_machine.rb
Constant Summary
- SEP =
"\r\n"
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Boolean) can_read?(size)
- - (Object) close
- - (Boolean) closed?
- - (Object) gets
-
- (SocketConnection) initialize
A new instance of SocketConnection.
- - (Object) post_init
- - (Object) read(size)
-
- (Object) receive_data(data)
EM callbacks.
- - (Object) unbind
- - (Object) write(buf)
Class Method Details
+ (Object) connect(host, port, timeout)
67 68 69 70 71 |
# File 'lib/memcache/event_machine.rb', line 67 def self.connect(host, port, timeout) EM.connect(host, port, self) do |conn| conn.pending_connect_timeout = timeout end end |
Instance Method Details
- (Boolean) can_read?(size)
127 128 129 |
# File 'lib/memcache/event_machine.rb', line 127 def can_read?(size) @buf.size >= @index + size end |
- (Object) close
83 84 85 86 |
# File 'lib/memcache/event_machine.rb', line 83 def close @connected = false close_connection(true) end |
- (Boolean) closed?
79 80 81 |
# File 'lib/memcache/event_machine.rb', line 79 def closed? !@connected end |
- (Object) gets
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/memcache/event_machine.rb', line 109 def gets while true # Read to ensure we have some data in the buffer line = read(2) # Reset the buffer index to zero @buf = @buf.slice(@index..-1) @index = 0 if eol = @buf.index(SEP) line << yank(eol + SEP.size) break else # EOL not in the current buffer line << yank(@buf.size) end end line end |
- (SocketConnection) initialize
A new instance of SocketConnection
73 74 75 76 77 |
# File 'lib/memcache/event_machine.rb', line 73 def initialize @connected = false @index = 0 @buf = '' end |
- (Object) post_init
144 145 146 147 |
# File 'lib/memcache/event_machine.rb', line 144 def post_init @connected = true succeed end |
- (Object) read(size)
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/memcache/event_machine.rb', line 92 def read(size) if can_read?(size) yank(size) else fiber = Fiber.current @size = size @callback = proc { |data| fiber.resume(data) } # TODO Can leak fiber if the connection dies while # this fiber is yielded, waiting for data Fiber.yield end end |
- (Object) receive_data(data)
EM callbacks
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/memcache/event_machine.rb', line 133 def receive_data(data) @buf << data if @callback and can_read?(@size) callback = @callback data = yank(@size) @callback = @size = nil callback.call(data) end end |
- (Object) unbind
149 150 151 152 153 154 155 |
# File 'lib/memcache/event_machine.rb', line 149 def unbind if @connected @connected = false else fail end end |
- (Object) write(buf)
88 89 90 |
# File 'lib/memcache/event_machine.rb', line 88 def write(buf) send_data(buf) end |