Class: CSGOLytics::LogListener
- Inherits:
-
Object
- Object
- CSGOLytics::LogListener
- Defined in:
- lib/csgolytics/log_listener.rb
Constant Summary collapse
- REMOTE_LOG_PKT_HEADER =
"\xff\xff\xff\xffR"
Instance Method Summary collapse
-
#initialize(config) ⇒ LogListener
constructor
A new instance of LogListener.
- #listen ⇒ Object
- #on_logline(&cb) ⇒ Object
Constructor Details
#initialize(config) ⇒ LogListener
Returns a new instance of LogListener.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/csgolytics/log_listener.rb', line 9 def initialize(config) @ssock = UDPSocket.new @ssock.bind("0.0.0.0", 3764) @callbacks = [] @remotes_map = {} config["servers"].each do |server| server["remote_addrs"].each do |remote_addr| @remotes_map[remote_addr] = server["server_id"] end end end |
Instance Method Details
#listen ⇒ Object
22 23 24 25 26 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 |
# File 'lib/csgolytics/log_listener.rb', line 22 def listen while msg = @ssock.recvfrom(0xffff) payload, raddr = msg payload = payload.force_encoding("utf-8") remote_addrs = [ "#{raddr[2]}:#{raddr[1]}", "#{raddr[3]}:#{raddr[1]}" ].uniq remote_server_id = nil remote_addrs.each do |r| remote_server_id ||= @remotes_map[r] end unless remote_server_id $stderr.puts "WARNING: unknown server: #{remote_addrs.inspect}" next end unless payload.start_with?(REMOTE_LOG_PKT_HEADER) $stderr.puts "WARNING: received invalid log packet" next end logline = payload[REMOTE_LOG_PKT_HEADER.length..-1] while ["\0", "\r", "\n"].include?(logline[-1]) logline = logline[0..-2] end @callbacks.each do |cb| cb[logline, remote_server_id] end end end |
#on_logline(&cb) ⇒ Object
58 59 60 |
# File 'lib/csgolytics/log_listener.rb', line 58 def on_logline(&cb) @callbacks << cb end |