summaryrefslogtreecommitdiff
path: root/src/oscpack/OscOutboundPacketStream.cpp
blob: a91d10af6ae1592e448c181e9295d303949dddac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
57
58
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
    oscpack -- Open Sound Control packet manipulation library
    http://www.audiomulch.com/~rossb/oscpack

    Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files
    (the "Software"), to deal in the Software without restriction,
    including without limitation the rights to use, copy, modify, merge,
    publish, distribute, sublicense, and/or sell copies of the Software,
    and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    Any person wishing to distribute modifications to the Software is
    requested to send the modifications to the original developer so that
    they can be incorporated into the canonical version.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
    ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "OscOutboundPacketStream.h"

#include <string.h>
#include <stdlib.h>
#include <assert.h>

#if defined(__WIN32__) || defined(WIN32)
#include <malloc.h> // for alloca
#endif

#include "OscHostEndianness.h"


namespace osc{

static void FromInt32( char *p, int32 x )
{
#ifdef OSC_HOST_LITTLE_ENDIAN
    union{
        osc::int32 i;
        char c[4];
    } u;

    u.i = x;

    p[3] = u.c[0];
    p[2] = u.c[1];
    p[1] = u.c[2];
    p[0] = u.c[3];
#else
    *reinterpret_cast<int32*>(p) = x;
#endif
}


static void FromUInt32( char *p, uint32 x )
{
#ifdef OSC_HOST_LITTLE_ENDIAN
    union{
        osc::uint32 i;
        char c[4];
    } u;

    u.i = x;

    p[3] = u.c[0];
    p[2] = u.c[1];
    p[1] = u.c[2];
    p[0] = u.c[3];
#else
    *reinterpret_cast<uint32*>(p) = x;
#endif
}


static void FromInt64( char *p, int64 x )
{
#ifdef OSC_HOST_LITTLE_ENDIAN
    union{
        osc::int64 i;
        char c[8];
    } u;

    u.i = x;

    p[7] = u.c[0];
    p[6] = u.c[1];
    p[5] = u.c[2];
    p[4] = u.c[3];
    p[3] = u.c[4];
    p[2] = u.c[5];
    p[1] = u.c[6];
    p[0] = u.c[7];
#else
    *reinterpret_cast<int64*>(p) = x;
#endif
}


static void FromUInt64( char *p, uint64 x )
{
#ifdef OSC_HOST_LITTLE_ENDIAN
    union{
        osc::uint64 i;
        char c[8];
    } u;

    u.i = x;

    p[7] = u.c[0];
    p[6] = u.c[1];
    p[5] = u.c[2];
    p[4] = u.c[3];
    p[3] = u.c[4];
    p[2] = u.c[5];
    p[1] = u.c[6];
    p[0] = u.c[7];
#else
    *reinterpret_cast<uint64*>(p) = x;
#endif
}


static inline long RoundUp4( long x )
{
    return ((x-1) & (~0x03L)) + 4;
}


OutboundPacketStream::OutboundPacketStream( char *buffer, unsigned long capacity )
    : data_( buffer )
    , end_( data_ + capacity )
    , typeTagsCurrent_( end_ )
    , messageCursor_( data_ )
    , argumentCurrent_( data_ )
    , elementSizePtr_( 0 )
    , messageIsInProgress_( false )
{

}


OutboundPacketStream::~OutboundPacketStream()
{

}


char *OutboundPacketStream::BeginElement( char *beginPtr )
{
    if( elementSizePtr_ == 0 ){

        elementSizePtr_ = reinterpret_cast<uint32*>(data_);

        return beginPtr;

    }else{
        // store an offset to the old element size ptr in the element size slot
        // we store an offset rather than the actual pointer to be 64 bit clean.
        *reinterpret_cast<uint32*>(beginPtr) =
                (uint32)(reinterpret_cast<char*>(elementSizePtr_) - data_);

        elementSizePtr_ = reinterpret_cast<uint32*>(beginPtr);

        return beginPtr + 4;
    }
}


void OutboundPacketStream::EndElement( char *endPtr )
{
    assert( elementSizePtr_ != 0 );

    if( elementSizePtr_ == reinterpret_cast<uint32*>(data_) ){

        elementSizePtr_ = 0;

    }else{
        // while building an element, an offset to the containing element's
        // size slot is stored in the elements size slot (or a ptr to data_
        // if there is no containing element). We retrieve that here
        uint32 *previousElementSizePtr =
                (uint32*)(data_ + *reinterpret_cast<uint32*>(elementSizePtr_));

        // then we store the element size in the slot, note that the element
        // size does not include the size slot, hence the - 4 below.
        uint32 elementSize =
                (endPtr - reinterpret_cast<char*>(elementSizePtr_)) - 4;
        FromUInt32( reinterpret_cast<char*>(elementSizePtr_), elementSize );

        // finally, we reset the element size ptr to the containing element
        elementSizePtr_ = previousElementSizePtr;
    }
}


bool OutboundPacketStream::ElementSizeSlotRequired() const
{
    return (elementSizePtr_ != 0);
}


void OutboundPacketStream::CheckForAvailableBundleSpace()
{
    unsigned long required = Size() + ((ElementSizeSlotRequired())?4:0) + 16;

    if( required > Capacity() )
        throw OutOfBufferMemoryException();
}


void OutboundPacketStream::CheckForAvailableMessageSpace( const char *addressPattern )
{
    // plus 4 for at least four bytes of type tag
     unsigned long required = Size() + ((ElementSizeSlotRequired())?4:0)
            + RoundUp4(strlen(addressPattern) + 1) + 4;

    if( required > Capacity() )
        throw OutOfBufferMemoryException();
}


void OutboundPacketStream::CheckForAvailableArgumentSpace( long argumentLength )
{
    // plus three for extra type tag, comma and null terminator
     unsigned long required = (argumentCurrent_ - data_) + argumentLength
            + RoundUp4( (end_ - typeTagsCurrent_) + 3 );

    if( required > Capacity() )
        throw OutOfBufferMemoryException();
}


void OutboundPacketStream::Clear()
{
    typeTagsCurrent_ = end_;
    messageCursor_ = data_;
    argumentCurrent_ = data_;
    elementSizePtr_ = 0;
    messageIsInProgress_ = false;
}


unsigned int OutboundPacketStream::Capacity() const
{
    return end_ - data_;
}


unsigned int OutboundPacketStream::Size() const
{
    unsigned int result = argumentCurrent_ - data_;
    if( IsMessageInProgress() ){
        // account for the length of the type tag string. the total type tag
        // includes an initial comma, plus at least one terminating \0
        result += RoundUp4( (end_ - typeTagsCurrent_) + 2 );
    }

    return result;
}


const char *OutboundPacketStream::Data() const
{
    return data_;
}


bool OutboundPacketStream::IsReady() const
{
    return (!IsMessageInProgress() && !IsBundleInProgress());
}


bool OutboundPacketStream::IsMessageInProgress() const
{
    return messageIsInProgress_;
}


bool OutboundPacketStream::IsBundleInProgress() const
{
    return (elementSizePtr_ != 0);
}


OutboundPacketStream& OutboundPacketStream::operator<<( const BundleInitiator& rhs )
{
    if( IsMessageInProgress() )
        throw MessageInProgressException();

    CheckForAvailableBundleSpace();

    messageCursor_ = BeginElement( messageCursor_ );

    memcpy( messageCursor_, "#bundle\0", 8 );
    FromUInt64( messageCursor_ + 8, rhs.timeTag );

    messageCursor_ += 16;
    argumentCurrent_ = messageCursor_;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const BundleTerminator& rhs )
{
    (void) rhs;

    if( !IsBundleInProgress() )
        throw BundleNotInProgressException();
    if( IsMessageInProgress() )
        throw MessageInProgressException();

    EndElement( messageCursor_ );

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const BeginMessage& rhs )
{
    if( IsMessageInProgress() )
        throw MessageInProgressException();

    CheckForAvailableMessageSpace( rhs.addressPattern );

    messageCursor_ = BeginElement( messageCursor_ );

    strcpy( messageCursor_, rhs.addressPattern );
    unsigned long rhsLength = strlen(rhs.addressPattern);
    messageCursor_ += rhsLength + 1;

    // zero pad to 4-byte boundary
    unsigned long i = rhsLength + 1;
    while( i & 0x3 ){
        *messageCursor_++ = '\0';
        ++i;
    }

    argumentCurrent_ = messageCursor_;
    typeTagsCurrent_ = end_;

    messageIsInProgress_ = true;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const MessageTerminator& rhs )
{
    (void) rhs;

    if( !IsMessageInProgress() )
        throw MessageNotInProgressException();

    int typeTagsCount = end_ - typeTagsCurrent_;

    if( typeTagsCount ){

        char *tempTypeTags = (char*)alloca(typeTagsCount);
        memcpy( tempTypeTags, typeTagsCurrent_, typeTagsCount );

        // slot size includes comma and null terminator
        int typeTagSlotSize = RoundUp4( typeTagsCount + 2 );

        uint32 argumentsSize = argumentCurrent_ - messageCursor_;

        memmove( messageCursor_ + typeTagSlotSize, messageCursor_, argumentsSize );

        messageCursor_[0] = ',';
        // copy type tags in reverse (really forward) order
        for( int i=0; i < typeTagsCount; ++i )
            messageCursor_[i+1] = tempTypeTags[ (typeTagsCount-1) - i ];

        char *p = messageCursor_ + 1 + typeTagsCount;
        for( int i=0; i < (typeTagSlotSize - (typeTagsCount + 1)); ++i )
            *p++ = '\0';

        typeTagsCurrent_ = end_;

        // advance messageCursor_ for next message
        messageCursor_ += typeTagSlotSize + argumentsSize;

    }else{
        // send an empty type tags string
        memcpy( messageCursor_, ",\0\0\0", 4 );

        // advance messageCursor_ for next message
        messageCursor_ += 4;
    }

    argumentCurrent_ = messageCursor_;

    EndElement( messageCursor_ );

    messageIsInProgress_ = false;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( bool rhs )
{
    CheckForAvailableArgumentSpace(0);

    *(--typeTagsCurrent_) = (char)((rhs) ? TRUE_TYPE_TAG : FALSE_TYPE_TAG);

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const NilType& rhs )
{
    (void) rhs;
    CheckForAvailableArgumentSpace(0);

    *(--typeTagsCurrent_) = NIL_TYPE_TAG;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const InfinitumType& rhs )
{
    (void) rhs;
    CheckForAvailableArgumentSpace(0);

    *(--typeTagsCurrent_) = INFINITUM_TYPE_TAG;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( int32 rhs )
{
    CheckForAvailableArgumentSpace(4);

    *(--typeTagsCurrent_) = INT32_TYPE_TAG;
    FromInt32( argumentCurrent_, rhs );
    argumentCurrent_ += 4;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( float rhs )
{
    CheckForAvailableArgumentSpace(4);

    *(--typeTagsCurrent_) = FLOAT_TYPE_TAG;

#ifdef OSC_HOST_LITTLE_ENDIAN
    union{
        float f;
        char c[4];
    } u;

    u.f = rhs;

    argumentCurrent_[3] = u.c[0];
    argumentCurrent_[2] = u.c[1];
    argumentCurrent_[1] = u.c[2];
    argumentCurrent_[0] = u.c[3];
#else
    *reinterpret_cast<float*>(argumentCurrent_) = rhs;
#endif

    argumentCurrent_ += 4;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( char rhs )
{
    CheckForAvailableArgumentSpace(4);

    *(--typeTagsCurrent_) = CHAR_TYPE_TAG;
    FromInt32( argumentCurrent_, rhs );
    argumentCurrent_ += 4;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const RgbaColor& rhs )
{
    CheckForAvailableArgumentSpace(4);

    *(--typeTagsCurrent_) = RGBA_COLOR_TYPE_TAG;
    FromUInt32( argumentCurrent_, rhs );
    argumentCurrent_ += 4;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const MidiMessage& rhs )
{
    CheckForAvailableArgumentSpace(4);

    *(--typeTagsCurrent_) = MIDI_MESSAGE_TYPE_TAG;
    FromUInt32( argumentCurrent_, rhs );
    argumentCurrent_ += 4;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( int64 rhs )
{
    CheckForAvailableArgumentSpace(8);

    *(--typeTagsCurrent_) = INT64_TYPE_TAG;
    FromInt64( argumentCurrent_, rhs );
    argumentCurrent_ += 8;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const TimeTag& rhs )
{
    CheckForAvailableArgumentSpace(8);

    *(--typeTagsCurrent_) = TIME_TAG_TYPE_TAG;
    FromUInt64( argumentCurrent_, rhs );
    argumentCurrent_ += 8;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( double rhs )
{
    CheckForAvailableArgumentSpace(8);

    *(--typeTagsCurrent_) = DOUBLE_TYPE_TAG;

#ifdef OSC_HOST_LITTLE_ENDIAN
    union{
        double f;
        char c[8];
    } u;

    u.f = rhs;

    argumentCurrent_[7] = u.c[0];
    argumentCurrent_[6] = u.c[1];
    argumentCurrent_[5] = u.c[2];
    argumentCurrent_[4] = u.c[3];
    argumentCurrent_[3] = u.c[4];
    argumentCurrent_[2] = u.c[5];
    argumentCurrent_[1] = u.c[6];
    argumentCurrent_[0] = u.c[7];
#else
    *reinterpret_cast<double*>(argumentCurrent_) = rhs;
#endif

    argumentCurrent_ += 8;

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const char *rhs )
{
    CheckForAvailableArgumentSpace( RoundUp4(strlen(rhs) + 1) );

    *(--typeTagsCurrent_) = STRING_TYPE_TAG;
    strcpy( argumentCurrent_, rhs );
    unsigned long rhsLength = strlen(rhs);
    argumentCurrent_ += rhsLength + 1;

    // zero pad to 4-byte boundary
    unsigned long i = rhsLength + 1;
    while( i & 0x3 ){
        *argumentCurrent_++ = '\0';
        ++i;
    }

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const Symbol& rhs )
{
    CheckForAvailableArgumentSpace( RoundUp4(strlen(rhs) + 1) );

    *(--typeTagsCurrent_) = SYMBOL_TYPE_TAG;
    strcpy( argumentCurrent_, rhs );
    unsigned long rhsLength = strlen(rhs);
    argumentCurrent_ += rhsLength + 1;

    // zero pad to 4-byte boundary
    unsigned long i = rhsLength + 1;
    while( i & 0x3 ){
        *argumentCurrent_++ = '\0';
        ++i;
    }

    return *this;
}


OutboundPacketStream& OutboundPacketStream::operator<<( const Blob& rhs )
{
    CheckForAvailableArgumentSpace( 4 + RoundUp4(rhs.size) );

    *(--typeTagsCurrent_) = BLOB_TYPE_TAG;
    FromUInt32( argumentCurrent_, rhs.size );
    argumentCurrent_ += 4;
    
    memcpy( argumentCurrent_, rhs.data, rhs.size );
    argumentCurrent_ += rhs.size;

    // zero pad to 4-byte boundary
    unsigned long i = rhs.size;
    while( i & 0x3 ){
        *argumentCurrent_++ = '\0';
        ++i;
    }

    return *this;
}

} // namespace osc