Class: StackHelper

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

Direct Known Subclasses

TCPStackHelper

Instance Method Summary (collapse)

Constructor Details

- (StackHelper) initialize(stack)

Returns a new instance of StackHelper



8
9
10
11
12
13
# File 'lib/diameter/stack_transport_helpers.rb', line 8

def initialize(stack)
  @all_connections = []
  @data = {}
  @stack = stack
  @loop_thread = nil
end

Instance Method Details

- (Object) main_loop



27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/diameter/stack_transport_helpers.rb', line 27

def main_loop
  begin
    rs, _ws, es = IO.select(@all_connections, [], @all_connections)
  rescue RuntimeError
    return
  end

  es.each do |e|
    Diameter.logger.log(Logger::WARN, "Exception on connection #{e}")
  end

  rs.each do |r|

    existing_data = @data[r]
    if existing_data.length < 4
      msg, _src = r.recvfrom_nonblock(4 - existing_data.length)
      if msg == ''
        Diameter.logger.warn('Received 0 bytes on read, closing connection')
        r.close
        @all_connections.delete r
        @data.delete r
      else
        existing_data += msg
      end
    end

    expected_len = -1
    if existing_data.length >= 4
      expected_len = DiameterMessage.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.recvfrom_nonblock(expected_len - existing_data.length)
      existing_data += msg
      if msg == ''
        # Connection closed
        Diameter.logger.warn('Received 0 bytes on read, closing connection')
        r.close
        @all_connections.delete r
        @data.delete 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

- (Object) send(bytes, connection)



78
79
80
# File 'lib/diameter/stack_transport_helpers.rb', line 78

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

- (Object) start_main_loop



15
16
17
18
19
20
21
# File 'lib/diameter/stack_transport_helpers.rb', line 15

def start_main_loop
  @loop_thread = Thread.new do
    loop do
      main_loop
    end
  end
end

- (Object) wakeup



23
24
25
# File 'lib/diameter/stack_transport_helpers.rb', line 23

def wakeup
  @loop_thread.raise
end