Class: Vizcore::Sync::OscMessage::Parser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/vizcore/sync/osc_message.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

BUNDLE_SIGNATURE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"#bundle"
NTP_TO_UNIX_OFFSET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

2_208_988_800

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Parser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Parser.

Parameters:

  • data (String)


33
34
35
36
# File 'lib/vizcore/sync/osc_message.rb', line 33

def initialize(data)
  @data = data.to_s.b
  @offset = 0
end

Instance Method Details

#parse_bundleObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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
85
# File 'lib/vizcore/sync/osc_message.rb', line 59

def parse_bundle
  bundle_timetag = parse_timetag(read_uint64)
  messages = []

  until @offset >= @data.bytesize
    break if @offset + 4 > @data.bytesize

    begin
      message_size = read_int32
    rescue StandardError
      break
    end

    break if message_size <= 0

    begin
      message_data = read_bytes(message_size)
    rescue StandardError
      break
    end

    parsed = Parser.new(message_data).parse_packet(default_timetag: bundle_timetag)
    messages.concat(Array(parsed))
  end

  messages
end

#parse_message(address, timetag: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



87
88
89
90
91
92
93
# File 'lib/vizcore/sync/osc_message.rb', line 87

def parse_message(address, timetag: nil)
  return nil unless address&.start_with?("/")

  tags = read_string
  arguments = read_arguments(tags)
  OscMessage.new(address: address, arguments: arguments, timetag: timetag)
end

#parse_packet(default_timetag: nil) ⇒ Vizcore::Sync::OscMessage, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
45
# File 'lib/vizcore/sync/osc_message.rb', line 39

def parse_packet(default_timetag: nil)
  signature_or_address = read_string
  return nil unless signature_or_address

  return parse_bundle if signature_or_address == BUNDLE_SIGNATURE
  parse_message(signature_or_address, timetag: default_timetag)
end

#read_arguments(tags) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • tags (String, nil)

Returns:

  • (Array)


97
98
99
100
101
102
103
104
# File 'lib/vizcore/sync/osc_message.rb', line 97

def read_arguments(tags)
  return [] if tags.to_s.empty?
  return [] unless tags.start_with?(",")

  tags[1..].to_s.each_char.map do |tag|
    read_argument(tag)
  end
end

#read_stringString?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (String, nil)


48
49
50
51
52
53
54
55
56
57
# File 'lib/vizcore/sync/osc_message.rb', line 48

def read_string
  start = @offset
  @offset += 1 while @offset < @data.bytesize && @data.getbyte(@offset) != 0
  return nil if @offset >= @data.bytesize

  value = @data.byteslice(start...@offset).to_s.force_encoding(Encoding::UTF_8)
  @offset += 1
  align_offset
  value
end