Class: Diameter::Internals::TCPStackHelper

Inherits:
StackHelper
  • Object
show all
Defined in:
lib/diameter/stack_transport_helpers.rb

Instance Method Summary collapse

Methods inherited from StackHelper

#initialize, #main_loop, #send, #start_main_loop, #wakeup

Constructor Details

This class inherits a constructor from Diameter::Internals::StackHelper

Instance Method Details

#accept_loopObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/diameter/stack_transport_helpers.rb', line 135

def accept_loop
  rs, _ws, es = IO.select(@listen_connections, [], @listen_connections)
  es.each do |e|
    Diameter.logger.log(Logger::WARN, "Exception on connection #{e}")
  end

  rs.each do |r|
    sock, addr = r.accept
    Diameter.logger.debug("Accepting connection from #{addr}")
    @data[sock] = ''
    @all_connections.push sock 
    wakeup
  end
end

#close(connection) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/diameter/stack_transport_helpers.rb', line 111

def close(connection)
  connection.close
  @all_connections.delete connection
  @listen_connections.delete connection
  @data.delete connection
rescue IOError
  # It's OK if this is already closed
end

#setup_new_connection(host, port) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/diameter/stack_transport_helpers.rb', line 92

def setup_new_connection(host, port)
  sd = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  sd.connect(Socket.pack_sockaddr_in(port, host))
  @all_connections.push sd
  @data[sd] = ''
  wakeup
  sd
end

#setup_new_listen_connection(host, port) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/diameter/stack_transport_helpers.rb', line 120

def setup_new_listen_connection(host, port)
  sd = ServerSocket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
  # reuse = [1,0].pack('ii')
  sd.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
  sd.bind(Socket.pack_sockaddr_in(port, host))
  sd.listen(100)
  @listen_connections.push sd
  @accept_loop_thread = Thread.new do
    loop do
      accept_loop
    end
  end
  @accept_loop_thread.abort_on_exception = true
end

#shutdownObject



101
102
103
104
105
106
107
108
109
# File 'lib/diameter/stack_transport_helpers.rb', line 101

def shutdown
  @accept_loop_thread.kill if @accept_loop_thread
  @loop_thread.kill if @loop_thread

  @all_connections.each { |c| close(c) }
  @listen_connections.each { |c| close(c) }
  @all_connections = []
  @listen_connections = []
end