Included Modules

EM::SocketConnection

Constants

SEP
BUFFER_SIZE

Public Class Methods

connect(host, port, timeout) click to toggle source
    # File lib/memcache/event_machine.rb, line 67
67:     def self.connect(host, port, timeout)
68:       EM.connect(host, port, self) do |conn|
69:         conn.pending_connect_timeout = timeout
70:       end
71:     end
new() click to toggle source
    # File lib/memcache/event_machine.rb, line 73
73:     def initialize
74:       @connected = false
75:       @index = 0
76:       @buf = ''
77:     end

Public Instance Methods

can_read?(size) click to toggle source
     # File lib/memcache/event_machine.rb, line 127
127:     def can_read?(size)
128:       @buf.size >= @index + size
129:     end
close() click to toggle source
    # File lib/memcache/event_machine.rb, line 83
83:     def close
84:       @connected = false
85:       close_connection(true)
86:     end
closed?() click to toggle source
    # File lib/memcache/event_machine.rb, line 79
79:     def closed?
80:       !@connected
81:     end
gets() click to toggle source
     # File lib/memcache/event_machine.rb, line 109
109:     def gets
110:       while true
111:         # Read to ensure we have some data in the buffer
112:         line = read(2)
113:         # Reset the buffer index to zero
114:         @buf = @buf.slice(@index..1)
115:         @index = 0
116:         if eol = @buf.index(SEP)
117:           line << yank(eol + SEP.size)
118:           break
119:         else
120:           # EOL not in the current buffer
121:           line << yank(@buf.size)
122:         end
123:       end
124:       line
125:     end
post_init() click to toggle source
     # File lib/memcache/event_machine.rb, line 144
144:     def post_init
145:       @connected = true
146:       succeed
147:     end
read(size) click to toggle source
     # File lib/memcache/event_machine.rb, line 92
 92:     def read(size)
 93:       if can_read?(size)
 94:         yank(size)
 95:       else
 96:         fiber = Fiber.current
 97:         @size = size
 98:         @callback = proc { |data|
 99:           fiber.resume(data)
100:         }
101:         # TODO Can leak fiber if the connection dies while
102:         # this fiber is yielded, waiting for data
103:         Fiber.yield
104:       end
105:     end
receive_data(data) click to toggle source

EM callbacks

     # File lib/memcache/event_machine.rb, line 133
133:     def receive_data(data)
134:       @buf << data
135: 
136:       if @callback and can_read?(@size)
137:         callback = @callback
138:         data = yank(@size)
139:         @callback = @size = nil
140:         callback.call(data)
141:       end
142:     end
unbind() click to toggle source
     # File lib/memcache/event_machine.rb, line 149
149:     def unbind
150:       if @connected
151:         @connected = false
152:       else
153:         fail
154:       end
155:     end
write(buf) click to toggle source
    # File lib/memcache/event_machine.rb, line 88
88:     def write(buf)
89:       send_data(buf)
90:     end

Private Instance Methods

yank(len) click to toggle source
     # File lib/memcache/event_machine.rb, line 161
161:     def yank(len)      
162:       data = @buf.slice(@index, len)
163:       @index += len
164:       @index = @buf.size if @index > @buf.size
165:       if @index >= BUFFER_SIZE
166:         @buf = @buf.slice(@index..1)
167:         @index = 0
168:       end
169:       data
170:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.