Class: Diameter::Internals::StackHelper

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

Direct Known Subclasses

TCPStackHelper

Instance Method Summary collapse

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_loopObject



38
39
40
41
42
43
44
45
46
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
# File 'lib/diameter/stack_transport_helpers.rb', line 38

def main_loop
    rs, _ws, es = IO.select(@all_connections + [@wakeup_pipe_rd], [], @all_connections)

  es.each do |e|
    Diameter.logger.log(Logger::WARN, "Exception on connection #{e}")
  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 = r.recv_nonblock(4 - existing_data.length)
      if msg == ''
        Diameter.logger.warn('Received 0 bytes on read, closing connection')
        close(r)
      else
        existing_data += msg
      end
    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 = r.recv_nonblock(expected_len - existing_data.length)
      existing_data += msg
      if msg == ''
        # Connection closed
        Diameter.logger.warn('Received 0 bytes on read, closing connection')
        close(r)
      end
    end

    if existing_data.length == expected_len
      @stack.handle_message(existing_data, r)
      @data[r] = ''
    else
      @data[r] = existing_data
    end
  end
end

#send(bytes, connection) ⇒ Object



85
86
87
# File 'lib/diameter/stack_transport_helpers.rb', line 85

def send(bytes, connection)
  connection.send(bytes, 0)
end

#start_main_loopObject



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

#wakeupObject



34
35
36
# File 'lib/diameter/stack_transport_helpers.rb', line 34

def wakeup
  @wakeup_pipe_wr.puts "wakeup"
end