Class: Async::IO::SharedEndpoint

Inherits:
Endpoint
  • Object
show all
Defined in:
lib/async/io/shared_endpoint.rb

Instance Attribute Summary

Attributes inherited from Endpoint

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Endpoint

each, #each, #hostname, parse, socket, ssl, tcp, try_convert, udp, unix

Constructor Details

#initialize(endpoint, wrappers) ⇒ SharedEndpoint

Returns a new instance of SharedEndpoint.



52
53
54
55
# File 'lib/async/io/shared_endpoint.rb', line 52

def initialize(endpoint, wrappers)
  @wrappers = wrappers
  @endpoint = endpoint
end

Class Method Details

.bound(endpoint, backlog = Socket::SOMAXCONN) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/async/io/shared_endpoint.rb', line 26

def self.bound(endpoint, backlog = Socket::SOMAXCONN)
  wrappers = []
  
  endpoint.each do |endpoint|
    server = endpoint.bind
    
    server.listen(backlog)
    
    server.close_on_exec = false
    server.reactor = nil
    
    wrappers << server
  end
  
  self.new(endpoint, wrappers)
end

.connected(endpoint) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/async/io/shared_endpoint.rb', line 43

def self.connected(endpoint)
  peer = endpoint.connect
  
  peer.close_on_exec = false
  peer.reactor = nil
  
  self.new(endpoint, [peer])
end

Instance Method Details

#accept(backlog = nil, &block) ⇒ Object



97
98
99
100
101
# File 'lib/async/io/shared_endpoint.rb', line 97

def accept(backlog = nil, &block)
  bind do |server|
    server.accept_each(&block)
  end
end

#bindObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/async/io/shared_endpoint.rb', line 61

def bind
  task = Async::Task.current
  
  @wrappers.each do |server|
    server = server.dup
    
    task.async do |task|
      task.annotate "binding to #{server.inspect}"
      
      begin
        yield server, task
      ensure
        server.close
      end
    end
  end
end

#closeObject



57
58
59
# File 'lib/async/io/shared_endpoint.rb', line 57

def close
  @wrappers.each(&:close)
end

#connectObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/async/io/shared_endpoint.rb', line 79

def connect
  task = Async::Task.current
  
  @wrappers.each do |peer|
    peer = peer.dup
    
    task.async do |task|
      task.annotate "connected to #{peer.inspect}"
      
      begin
        yield peer, task
      ensure
        peer.close
      end
    end
  end
end

#to_sObject



103
104
105
# File 'lib/async/io/shared_endpoint.rb', line 103

def to_s
  "\#<#{self.class} #{@wrappers.count} descriptors for #{@endpoint}>"
end