Class: Select::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Server


320
321
322
323
324
# File 'lib/select.rb', line 320

def initialize opts
  raise "You can't use this class directly. Create a subclass"  if self.class.superclass == Object
  init opts
  select.read_set @sock, &self.method( :event_conn)
end

Instance Attribute Details

#clientclassObject (readonly)

Returns the value of attribute clientclass


318
319
320
# File 'lib/select.rb', line 318

def clientclass
  @clientclass
end

#selectObject (readonly)

Returns the value of attribute select


318
319
320
# File 'lib/select.rb', line 318

def select
  @select
end

Instance Method Details

#clients_closeObject


369
370
371
# File 'lib/select.rb', line 369

def clients_close
  @clients.each {|c| c.close }
end

#closeObject


363
364
365
366
367
# File 'lib/select.rb', line 363

def close
  @select.del @sock
  @sock.close
rescue IOError
end

#delete(sock) ⇒ Object


337
338
339
# File 'lib/select.rb', line 337

def delete sock
  @select.del sock
end

#event_client_closed(client) ⇒ Object


359
360
361
# File 'lib/select.rb', line 359

def event_client_closed client
  @clients.delete client
end

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


341
342
343
344
345
346
347
348
349
350
# File 'lib/select.rb', line 341

def event_conn sock = @sock, event = :read
  a = sock.accept
  c = event_new_client a
  if c.kind_of? Hash
    cc = c[ :clientclass] || @clientclass
    h = { :sock => a, :select => @select, :parent => self }.update c
    c = cc.new h
  end
  @clients.push c
end

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


352
353
# File 'lib/select.rb', line 352

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

#event_new_client(sock) ⇒ Object


355
356
357
# File 'lib/select.rb', line 355

def event_new_client sock
  Hash.new
end

#init(sock:, select: nil, clientclass: nil) ⇒ Object


326
327
328
329
330
331
# File 'lib/select.rb', line 326

def init sock: , select: nil, clientclass: nil
  @sock = sock
  @select = select || Select.new
  @clientclass = clientclass || Select::Socket
  @clients = []
end

#runObject


333
334
335
# File 'lib/select.rb', line 333

def run
  @select.run
end