Module OSC::Packet
In: lib/osc.rb

Unit of transmission. Really needs revamping

Methods

decode   encode   pad   tag  

Public Class methods

Takes a string containing one packet

[Source]

     # File lib/osc.rb, line 242
242:     def self.decode(packet)
243:       # XXX I think it would have been better to use a StringScanner. Maybe I
244:       # will convert it someday...
245:       io = StringIO.new(packet)
246:       id = decode_string(io)
247:       if id == '#bundle'
248:         b = Bundle.new(decode_timetag(io))
249:         until io.eof?
250:           l = io.read(4).unpack('N')[0]
251:           s = io.read(l)
252:           b << decode(s)
253:         end
254:         b
255:       elsif id =~ /^\//
256:         m = Message.new(id)
257:         if io.getc == ?,
258:           tags = decode_string(io)
259:           tags.scan(/./) do |t|
260:             case t
261:             when 'i'
262:               m << decode_int32(io)
263:             when 'f'
264:               m << decode_float32(io)
265:             when 's'
266:               m << decode_string(io)
267:             when 'b'
268:               m << decode_blob(io)
269: 
270:             # right now we skip over nonstandard datatypes, but we'll want to
271:             # add these datatypes too.
272:             when /[htd]/; io.read(8)
273:             when 'S'; decode_string(io)
274:             when /[crm]/; io.read(4)
275:             when /[TFNI\[\]]/;
276:             end
277:           end
278:         end
279:         m
280:       end
281:     end

[Source]

     # File lib/osc.rb, line 298
298:     def self.encode(o)
299:       case o
300:       when Fixnum;  [o].pack 'N'
301:       when Float;   [o].pack 'g'
302:       when Blob;    pad([o.size].pack('N') + o)
303:       when String;  pad(o.sub(/\000.*\Z/, '') + "\000")
304:       when TimeTag; o.to_a.pack('NN')
305: 
306:       when Message
307:         s = encode(o.address)
308:         s << encode(','+o.types)
309:         s << o.args.collect{|x| encode(x)}.join
310: 
311:       when Bundle
312:         s = encode('#bundle')
313:         s << encode(o.timetag)
314:         s << o.args.collect { |x| 
315:           x2 = encode(x); [x2.size].pack('N') + x2 
316:         }.join
317:       end
318:     end

[Source]

     # File lib/osc.rb, line 283
283:     def self.pad(s)
284:       s + ("\000" * ((4 - s.size)%4))
285:     end

[Source]

     # File lib/osc.rb, line 287
287:     def self.tag(o)
288:       case o
289:       when Fixnum;  'i'
290:       when TimeTag; 't'
291:       when Float;   'f'
292:       when Blob;    'b'
293:       when String;  's'
294:       else;         nil
295:       end
296:     end

[Validate]