Module | OSC::Packet |
In: |
lib/osc.rb
|
Takes a string containing one packet
# File lib/osc.rb, line 239 239: def self.decode(packet) 240: # XXX I think it would have been better to use a StringScanner. Maybe I 241: # will convert it someday... 242: io = StringIO.new(packet) 243: id = decode_string(io) 244: if id == '#bundle' 245: b = Bundle.new(decode_timetag(io)) 246: until io.eof? 247: l = io.read(4).unpack('N')[0] 248: s = io.read(l) 249: b << decode(s) 250: end 251: b 252: elsif id =~ /^\// 253: m = Message.new(id) 254: if io.getc == ?, 255: tags = decode_string(io) 256: tags.scan(/./) do |t| 257: case t 258: when 'i' 259: m << decode_int32(io) 260: when 'f' 261: m << decode_float32(io) 262: when 's' 263: m << decode_string(io) 264: when 'b' 265: m << decode_blob(io) 266: 267: # right now we skip over nonstandard datatypes, but we'll want to 268: # add these datatypes too. 269: when /[htd]/; io.read(8) 270: when 'S'; decode_string(io) 271: when /[crm]/; io.read(4) 272: when /[TFNI\[\]]/; 273: end 274: end 275: end 276: m 277: end 278: end
# File lib/osc.rb, line 295 295: def self.encode(o) 296: case o 297: when Fixnum; [o].pack 'N' 298: when Float; [o].pack 'g' 299: when Blob; pad([o.size].pack('N') + o) 300: when String; pad(o.sub(/\000.*\Z/, '') + "\000") 301: when TimeTag; o.to_a.pack('NN') 302: 303: when Message 304: s = encode(o.address) 305: s << encode(','+o.types) 306: s << o.args.collect{|x| encode(x)}.join 307: 308: when Bundle 309: s = encode('#bundle') 310: s << encode(o.timetag) 311: s << o.args.collect { |x| 312: x2 = encode(x); [x2.size].pack('N') + x2 313: }.join 314: end 315: end