Class: Diameter::Internals::StackHelper
- Inherits:
-
Object
- Object
- Diameter::Internals::StackHelper
- Defined in:
- lib/diameter/stack_transport_helpers.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(stack) ⇒ StackHelper
constructor
A new instance of StackHelper.
- #main_loop ⇒ Object
- #read_from(connection, bytes) ⇒ Object
- #send(bytes, connection) ⇒ Object
- #start_main_loop ⇒ Object
- #wakeup ⇒ Object
Constructor Details
#initialize(stack) ⇒ StackHelper
Returns a new instance of StackHelper.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/diameter/stack_transport_helpers.rb', line 14 def initialize(stack) @all_connections = [] @listen_connections = [] @data = {} @stack = stack @loop_thread = nil @accept_loop_thread = nil @connection_lock = Mutex.new @wakeup_pipe_rd, @wakeup_pipe_wr = IO.pipe end |
Instance Method Details
#main_loop ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/diameter/stack_transport_helpers.rb', line 47 def main_loop rs, _ws, es = IO.select(@all_connections + [@wakeup_pipe_rd], [], @all_connections) es.each do |e| # :nocov: Diameter.logger.log(Logger::WARN, "Exception on connection #{e}") # :nocov: end rs.each do |r| if r == @wakeup_pipe_rd r.gets next end existing_data = @data[r] if existing_data.length < 4 msg, _src = read_from(r, 4 - existing_data.length) existing_data += msg end expected_len = -1 if existing_data.length >= 4 expected_len = Message.length_from_header(existing_data[0..4]) Diameter.logger.debug("Read 4 bytes #{existing_data[0..4].inspect}, " \ "reading full message of length #{expected_len}") msg, _src = read_from(r, expected_len - existing_data.length) existing_data += msg end if existing_data.length == expected_len @stack.(existing_data, r) @data[r] = '' else @data[r] = existing_data end end end |
#read_from(connection, bytes) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/diameter/stack_transport_helpers.rb', line 38 def read_from(connection, bytes) msg, src = connection.recv_nonblock(bytes) if msg == '' Diameter.logger.warn('Received 0 bytes on read, closing connection') close(connection) end return msg, src end |
#send(bytes, connection) ⇒ Object
86 87 88 |
# File 'lib/diameter/stack_transport_helpers.rb', line 86 def send(bytes, connection) connection.send(bytes, 0) end |
#start_main_loop ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/diameter/stack_transport_helpers.rb', line 25 def start_main_loop @loop_thread = Thread.new do loop do main_loop end end @loop_thread.abort_on_exception = true end |
#wakeup ⇒ Object
34 35 36 |
# File 'lib/diameter/stack_transport_helpers.rb', line 34 def wakeup @wakeup_pipe_wr.puts "wakeup" end |