Class: Select::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/select.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Socket


230
231
232
233
234
# File 'lib/select.rb', line 230

def initialize opts
  self.init opts
  @select.read_set  @sock, &method( :event_read)
  @select.error_set @sock, &method( :event_error)
end

Instance Attribute Details

#bufsizeObject (readonly)

Returns the value of attribute bufsize


228
229
230
# File 'lib/select.rb', line 228

def bufsize
  @bufsize
end

#parentObject (readonly)

Returns the value of attribute parent


228
229
230
# File 'lib/select.rb', line 228

def parent
  @parent
end

#selectObject (readonly)

Returns the value of attribute select


228
229
230
# File 'lib/select.rb', line 228

def select
  @select
end

#sockObject (readonly)

Returns the value of attribute sock


228
229
230
# File 'lib/select.rb', line 228

def sock
  @sock
end

Instance Method Details

#closeObject


265
266
267
268
269
270
# File 'lib/select.rb', line 265

def close
  @select.del @sock
  @sock.close
  @parent.event_client_closed self  if @parent.respond_to? :event_client_closed
rescue IOError
end

#closed?Boolean


312
313
314
# File 'lib/select.rb', line 312

def closed?
  @sock.closed?
end

#delimiter=(delimiter) ⇒ Object


245
246
247
248
249
250
251
# File 'lib/select.rb', line 245

def delimiter= delimiter
  @delimiter = case delimiter
    when Regexp          then Regexp.new "^.*?"+delimiter.source
    when Fixnum, Bignum  then /^.{#{delimiter}}/
    else Regexp.new "^.*?"+Regexp.quote( delimiter.to_s)
    end
end

#event_eof(sock = @sock) ⇒ Object


297
298
299
# File 'lib/select.rb', line 297

def event_eof sock = @sock
  self.close
end

#event_errno(errno, sock = @sock, event = :error) ⇒ Object


301
302
303
# File 'lib/select.rb', line 301

def event_errno errno, sock = @sock, event = :error
  self.close
end

#event_error(sock = @sock, event = :error) ⇒ Object


305
306
307
# File 'lib/select.rb', line 305

def event_error sock = @sock, event = :error
  self.close
end

#event_ioerror(sock = @sock, event = :error) ⇒ Object


309
310
# File 'lib/select.rb', line 309

def event_ioerror sock = @sock, event = :error
end

#event_line(line) ⇒ Object


272
273
# File 'lib/select.rb', line 272

def event_line line
end

#event_read(sock = @sock, event = :read) ⇒ Object


275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/select.rb', line 275

def event_read sock = @sock, event = :read
  @linebuf += sock.readpartial @bufsize
  @linebuf.each! @delimiter, &self.method( :event_line)
rescue EOFError
  self.event_eof sock
rescue Errno::EPIPE => e
  self.event_errno e, sock, event
rescue IOError
  self.event_ioerror sock, event
rescue Errno::ECONNRESET => e
  self.event_errno e, sock, event
end

#event_write(sock = @sock, event = :write) ⇒ Object


288
289
290
291
292
293
294
295
# File 'lib/select.rb', line 288

def event_write sock = @sock, event = :write
  @writebuf.remove! sock.syswrite( @writebuf)
  @select.write_del sock  if @writebuf.empty?
  @writebuf
rescue IOError
  @select.del @sock
  @writebuf
end

#init(sock:, delimiter: nil, select: nil, bufsize: nil, parent: nil) ⇒ Object


236
237
238
239
240
241
242
243
# File 'lib/select.rb', line 236

def init sock: , delimiter: nil, select: nil, bufsize: nil, parent: nil
  @sock    = sock
  @select  = select  || Select.new
  @bufsize = bufsize || 4096
  @parent  = parent  || nil
  self.delimiter = opts[ :delimiter] || $/
  @linebuf, @writebuf = Select::Buffer.new(""), Select::Buffer.new("")
end

#puts(str) ⇒ Object


261
262
263
# File 'lib/select.rb', line 261

def puts str
  self.write "#{str}\n"
end

#write(str) ⇒ Object Also known as: print, <<


253
254
255
256
257
# File 'lib/select.rb', line 253

def write str
  e = @writebuf.empty?
  @writebuf += str
  @select.write_set @sock, &method( :event_write)  if e
end