Class: Revactor::TCP::Listener

Inherits:
Rev::TCPListener
  • Object
show all
Defined in:
lib/revactor/tcp.rb

Overview

TCP Listener returned from Revactor::TCP.listen

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, options = {}) ⇒ Listener

Listen on the specified address and port. Accepts the following options:

:active - Default active setting for new connections.  See the 
          documentation Rev::TCP::Socket#active= for more info

:controller - The controlling actor, default Actor.current

:filter - An symbol/class or array of symbols/classes which implement 
          #encode and #decode methods to transform data sent and 
          received data respectively via Revactor::TCP::Socket.
          See the "Filters" section in the README for more information


358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/revactor/tcp.rb', line 358

def initialize(host, port, options = {})
  super(host, port)
  opts = {
    :active     => false,
    :controller => Actor.current
  }.merge(options)
  
  @active, @controller = opts[:active], opts[:controller]
  @filterset = options[:filter]
  
  @accepting = false
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



344
345
346
# File 'lib/revactor/tcp.rb', line 344

def controller
  @controller
end

Instance Method Details

#acceptObject

Accept an incoming connection



394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'lib/revactor/tcp.rb', line 394

def accept
  raise "another actor is already accepting" if @accepting
  
  @accepting = true
  @receiver = Actor.current
  enable
  
  Actor.receive do |filter|
    filter.when(T[:tcp_connection, self]) do |_, _, sock|
      @accepting = false            
      return sock
    end
  end
end

#active=(state) ⇒ Object

Change the default active setting for newly accepted connections



376
377
378
379
380
381
382
# File 'lib/revactor/tcp.rb', line 376

def active=(state)
  unless [true, false, :once].include? state
    raise ArgumentError, "must be true, false, or :once" 
  end

  @active = state
end

#active?Boolean

Will newly accepted connections be active?

Returns:

  • (Boolean)


385
# File 'lib/revactor/tcp.rb', line 385

def active?; @active; end

#inspectObject



371
372
373
# File 'lib/revactor/tcp.rb', line 371

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)}>"
end