annotate trunk/external/oscpack/osc/OscOutboundPacketStream.cpp @ 706:f8e90b5d85fd tip

Delete CARFAC code from this repository. It has been moved to https://github.com/google/carfac Please email me with your github username to get access. I've also created a new mailing list to discuss CARFAC development: https://groups.google.com/forum/#!forum/carfac-dev
author ronw@google.com
date Thu, 18 Jul 2013 20:56:51 +0000
parents 4b37b53105a3
children
rev   line source
tomwalters@570 1 /*
tomwalters@570 2 oscpack -- Open Sound Control packet manipulation library
tomwalters@570 3 http://www.audiomulch.com/~rossb/oscpack
tomwalters@570 4
tomwalters@570 5 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>
tomwalters@570 6
tomwalters@570 7 Permission is hereby granted, free of charge, to any person obtaining
tomwalters@570 8 a copy of this software and associated documentation files
tomwalters@570 9 (the "Software"), to deal in the Software without restriction,
tomwalters@570 10 including without limitation the rights to use, copy, modify, merge,
tomwalters@570 11 publish, distribute, sublicense, and/or sell copies of the Software,
tomwalters@570 12 and to permit persons to whom the Software is furnished to do so,
tomwalters@570 13 subject to the following conditions:
tomwalters@570 14
tomwalters@570 15 The above copyright notice and this permission notice shall be
tomwalters@570 16 included in all copies or substantial portions of the Software.
tomwalters@570 17
tomwalters@570 18 Any person wishing to distribute modifications to the Software is
tomwalters@570 19 requested to send the modifications to the original developer so that
tomwalters@570 20 they can be incorporated into the canonical version.
tomwalters@570 21
tomwalters@570 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
tomwalters@570 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tomwalters@570 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tomwalters@570 25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
tomwalters@570 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
tomwalters@570 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
tomwalters@570 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tomwalters@570 29 */
tomwalters@570 30 #include "OscOutboundPacketStream.h"
tomwalters@570 31
tomwalters@570 32 #include <string.h>
tomwalters@570 33 #include <stdlib.h>
tomwalters@570 34 #include <assert.h>
tomwalters@570 35
tomwalters@570 36 #if defined(__WIN32__) || defined(WIN32)
tomwalters@570 37 #include <malloc.h> // for alloca
tomwalters@570 38 #endif
tomwalters@570 39
tomwalters@570 40 #include "OscHostEndianness.h"
tomwalters@570 41
tomwalters@570 42
tomwalters@570 43 namespace osc{
tomwalters@570 44
tomwalters@570 45 static void FromInt32( char *p, int32 x )
tomwalters@570 46 {
tomwalters@570 47 #ifdef OSC_HOST_LITTLE_ENDIAN
tomwalters@570 48 union{
tomwalters@570 49 osc::int32 i;
tomwalters@570 50 char c[4];
tomwalters@570 51 } u;
tomwalters@570 52
tomwalters@570 53 u.i = x;
tomwalters@570 54
tomwalters@570 55 p[3] = u.c[0];
tomwalters@570 56 p[2] = u.c[1];
tomwalters@570 57 p[1] = u.c[2];
tomwalters@570 58 p[0] = u.c[3];
tomwalters@570 59 #else
tomwalters@570 60 *reinterpret_cast<int32*>(p) = x;
tomwalters@570 61 #endif
tomwalters@570 62 }
tomwalters@570 63
tomwalters@570 64
tomwalters@570 65 static void FromUInt32( char *p, uint32 x )
tomwalters@570 66 {
tomwalters@570 67 #ifdef OSC_HOST_LITTLE_ENDIAN
tomwalters@570 68 union{
tomwalters@570 69 osc::uint32 i;
tomwalters@570 70 char c[4];
tomwalters@570 71 } u;
tomwalters@570 72
tomwalters@570 73 u.i = x;
tomwalters@570 74
tomwalters@570 75 p[3] = u.c[0];
tomwalters@570 76 p[2] = u.c[1];
tomwalters@570 77 p[1] = u.c[2];
tomwalters@570 78 p[0] = u.c[3];
tomwalters@570 79 #else
tomwalters@570 80 *reinterpret_cast<uint32*>(p) = x;
tomwalters@570 81 #endif
tomwalters@570 82 }
tomwalters@570 83
tomwalters@570 84
tomwalters@570 85 static void FromInt64( char *p, int64 x )
tomwalters@570 86 {
tomwalters@570 87 #ifdef OSC_HOST_LITTLE_ENDIAN
tomwalters@570 88 union{
tomwalters@570 89 osc::int64 i;
tomwalters@570 90 char c[8];
tomwalters@570 91 } u;
tomwalters@570 92
tomwalters@570 93 u.i = x;
tomwalters@570 94
tomwalters@570 95 p[7] = u.c[0];
tomwalters@570 96 p[6] = u.c[1];
tomwalters@570 97 p[5] = u.c[2];
tomwalters@570 98 p[4] = u.c[3];
tomwalters@570 99 p[3] = u.c[4];
tomwalters@570 100 p[2] = u.c[5];
tomwalters@570 101 p[1] = u.c[6];
tomwalters@570 102 p[0] = u.c[7];
tomwalters@570 103 #else
tomwalters@570 104 *reinterpret_cast<int64*>(p) = x;
tomwalters@570 105 #endif
tomwalters@570 106 }
tomwalters@570 107
tomwalters@570 108
tomwalters@570 109 static void FromUInt64( char *p, uint64 x )
tomwalters@570 110 {
tomwalters@570 111 #ifdef OSC_HOST_LITTLE_ENDIAN
tomwalters@570 112 union{
tomwalters@570 113 osc::uint64 i;
tomwalters@570 114 char c[8];
tomwalters@570 115 } u;
tomwalters@570 116
tomwalters@570 117 u.i = x;
tomwalters@570 118
tomwalters@570 119 p[7] = u.c[0];
tomwalters@570 120 p[6] = u.c[1];
tomwalters@570 121 p[5] = u.c[2];
tomwalters@570 122 p[4] = u.c[3];
tomwalters@570 123 p[3] = u.c[4];
tomwalters@570 124 p[2] = u.c[5];
tomwalters@570 125 p[1] = u.c[6];
tomwalters@570 126 p[0] = u.c[7];
tomwalters@570 127 #else
tomwalters@570 128 *reinterpret_cast<uint64*>(p) = x;
tomwalters@570 129 #endif
tomwalters@570 130 }
tomwalters@570 131
tomwalters@570 132
tomwalters@570 133 static inline long RoundUp4( long x )
tomwalters@570 134 {
tomwalters@570 135 return ((x-1) & (~0x03L)) + 4;
tomwalters@570 136 }
tomwalters@570 137
tomwalters@570 138
tomwalters@570 139 OutboundPacketStream::OutboundPacketStream( char *buffer, unsigned long capacity )
tomwalters@570 140 : data_( buffer )
tomwalters@570 141 , end_( data_ + capacity )
tomwalters@570 142 , typeTagsCurrent_( end_ )
tomwalters@570 143 , messageCursor_( data_ )
tomwalters@570 144 , argumentCurrent_( data_ )
tomwalters@570 145 , elementSizePtr_( 0 )
tomwalters@570 146 , messageIsInProgress_( false )
tomwalters@570 147 {
tomwalters@570 148
tomwalters@570 149 }
tomwalters@570 150
tomwalters@570 151
tomwalters@570 152 OutboundPacketStream::~OutboundPacketStream()
tomwalters@570 153 {
tomwalters@570 154
tomwalters@570 155 }
tomwalters@570 156
tomwalters@570 157
tomwalters@570 158 char *OutboundPacketStream::BeginElement( char *beginPtr )
tomwalters@570 159 {
tomwalters@570 160 if( elementSizePtr_ == 0 ){
tomwalters@570 161
tomwalters@570 162 elementSizePtr_ = reinterpret_cast<uint32*>(data_);
tomwalters@570 163
tomwalters@570 164 return beginPtr;
tomwalters@570 165
tomwalters@570 166 }else{
tomwalters@570 167 // store an offset to the old element size ptr in the element size slot
tomwalters@570 168 // we store an offset rather than the actual pointer to be 64 bit clean.
tomwalters@570 169 *reinterpret_cast<uint32*>(beginPtr) =
tomwalters@570 170 (uint32)(reinterpret_cast<char*>(elementSizePtr_) - data_);
tomwalters@570 171
tomwalters@570 172 elementSizePtr_ = reinterpret_cast<uint32*>(beginPtr);
tomwalters@570 173
tomwalters@570 174 return beginPtr + 4;
tomwalters@570 175 }
tomwalters@570 176 }
tomwalters@570 177
tomwalters@570 178
tomwalters@570 179 void OutboundPacketStream::EndElement( char *endPtr )
tomwalters@570 180 {
tomwalters@570 181 assert( elementSizePtr_ != 0 );
tomwalters@570 182
tomwalters@570 183 if( elementSizePtr_ == reinterpret_cast<uint32*>(data_) ){
tomwalters@570 184
tomwalters@570 185 elementSizePtr_ = 0;
tomwalters@570 186
tomwalters@570 187 }else{
tomwalters@570 188 // while building an element, an offset to the containing element's
tomwalters@570 189 // size slot is stored in the elements size slot (or a ptr to data_
tomwalters@570 190 // if there is no containing element). We retrieve that here
tomwalters@570 191 uint32 *previousElementSizePtr =
tomwalters@570 192 (uint32*)(data_ + *reinterpret_cast<uint32*>(elementSizePtr_));
tomwalters@570 193
tomwalters@570 194 // then we store the element size in the slot, note that the element
tomwalters@570 195 // size does not include the size slot, hence the - 4 below.
tomwalters@570 196 uint32 elementSize =
tomwalters@570 197 (endPtr - reinterpret_cast<char*>(elementSizePtr_)) - 4;
tomwalters@570 198 FromUInt32( reinterpret_cast<char*>(elementSizePtr_), elementSize );
tomwalters@570 199
tomwalters@570 200 // finally, we reset the element size ptr to the containing element
tomwalters@570 201 elementSizePtr_ = previousElementSizePtr;
tomwalters@570 202 }
tomwalters@570 203 }
tomwalters@570 204
tomwalters@570 205
tomwalters@570 206 bool OutboundPacketStream::ElementSizeSlotRequired() const
tomwalters@570 207 {
tomwalters@570 208 return (elementSizePtr_ != 0);
tomwalters@570 209 }
tomwalters@570 210
tomwalters@570 211
tomwalters@570 212 void OutboundPacketStream::CheckForAvailableBundleSpace()
tomwalters@570 213 {
tomwalters@570 214 unsigned long required = Size() + ((ElementSizeSlotRequired())?4:0) + 16;
tomwalters@570 215
tomwalters@570 216 if( required > Capacity() )
tomwalters@570 217 throw OutOfBufferMemoryException();
tomwalters@570 218 }
tomwalters@570 219
tomwalters@570 220
tomwalters@570 221 void OutboundPacketStream::CheckForAvailableMessageSpace( const char *addressPattern )
tomwalters@570 222 {
tomwalters@570 223 // plus 4 for at least four bytes of type tag
tomwalters@570 224 unsigned long required = Size() + ((ElementSizeSlotRequired())?4:0)
tomwalters@570 225 + RoundUp4(strlen(addressPattern) + 1) + 4;
tomwalters@570 226
tomwalters@570 227 if( required > Capacity() )
tomwalters@570 228 throw OutOfBufferMemoryException();
tomwalters@570 229 }
tomwalters@570 230
tomwalters@570 231
tomwalters@570 232 void OutboundPacketStream::CheckForAvailableArgumentSpace( long argumentLength )
tomwalters@570 233 {
tomwalters@570 234 // plus three for extra type tag, comma and null terminator
tomwalters@570 235 unsigned long required = (argumentCurrent_ - data_) + argumentLength
tomwalters@570 236 + RoundUp4( (end_ - typeTagsCurrent_) + 3 );
tomwalters@570 237
tomwalters@570 238 if( required > Capacity() )
tomwalters@570 239 throw OutOfBufferMemoryException();
tomwalters@570 240 }
tomwalters@570 241
tomwalters@570 242
tomwalters@570 243 void OutboundPacketStream::Clear()
tomwalters@570 244 {
tomwalters@570 245 typeTagsCurrent_ = end_;
tomwalters@570 246 messageCursor_ = data_;
tomwalters@570 247 argumentCurrent_ = data_;
tomwalters@570 248 elementSizePtr_ = 0;
tomwalters@570 249 messageIsInProgress_ = false;
tomwalters@570 250 }
tomwalters@570 251
tomwalters@570 252
tomwalters@570 253 unsigned int OutboundPacketStream::Capacity() const
tomwalters@570 254 {
tomwalters@570 255 return end_ - data_;
tomwalters@570 256 }
tomwalters@570 257
tomwalters@570 258
tomwalters@570 259 unsigned int OutboundPacketStream::Size() const
tomwalters@570 260 {
tomwalters@570 261 unsigned int result = argumentCurrent_ - data_;
tomwalters@570 262 if( IsMessageInProgress() ){
tomwalters@570 263 // account for the length of the type tag string. the total type tag
tomwalters@570 264 // includes an initial comma, plus at least one terminating \0
tomwalters@570 265 result += RoundUp4( (end_ - typeTagsCurrent_) + 2 );
tomwalters@570 266 }
tomwalters@570 267
tomwalters@570 268 return result;
tomwalters@570 269 }
tomwalters@570 270
tomwalters@570 271
tomwalters@570 272 const char *OutboundPacketStream::Data() const
tomwalters@570 273 {
tomwalters@570 274 return data_;
tomwalters@570 275 }
tomwalters@570 276
tomwalters@570 277
tomwalters@570 278 bool OutboundPacketStream::IsReady() const
tomwalters@570 279 {
tomwalters@570 280 return (!IsMessageInProgress() && !IsBundleInProgress());
tomwalters@570 281 }
tomwalters@570 282
tomwalters@570 283
tomwalters@570 284 bool OutboundPacketStream::IsMessageInProgress() const
tomwalters@570 285 {
tomwalters@570 286 return messageIsInProgress_;
tomwalters@570 287 }
tomwalters@570 288
tomwalters@570 289
tomwalters@570 290 bool OutboundPacketStream::IsBundleInProgress() const
tomwalters@570 291 {
tomwalters@570 292 return (elementSizePtr_ != 0);
tomwalters@570 293 }
tomwalters@570 294
tomwalters@570 295
tomwalters@570 296 OutboundPacketStream& OutboundPacketStream::operator<<( const BundleInitiator& rhs )
tomwalters@570 297 {
tomwalters@570 298 if( IsMessageInProgress() )
tomwalters@570 299 throw MessageInProgressException();
tomwalters@570 300
tomwalters@570 301 CheckForAvailableBundleSpace();
tomwalters@570 302
tomwalters@570 303 messageCursor_ = BeginElement( messageCursor_ );
tomwalters@570 304
tomwalters@570 305 memcpy( messageCursor_, "#bundle\0", 8 );
tomwalters@570 306 FromUInt64( messageCursor_ + 8, rhs.timeTag );
tomwalters@570 307
tomwalters@570 308 messageCursor_ += 16;
tomwalters@570 309 argumentCurrent_ = messageCursor_;
tomwalters@570 310
tomwalters@570 311 return *this;
tomwalters@570 312 }
tomwalters@570 313
tomwalters@570 314
tomwalters@570 315 OutboundPacketStream& OutboundPacketStream::operator<<( const BundleTerminator& rhs )
tomwalters@570 316 {
tomwalters@570 317 (void) rhs;
tomwalters@570 318
tomwalters@570 319 if( !IsBundleInProgress() )
tomwalters@570 320 throw BundleNotInProgressException();
tomwalters@570 321 if( IsMessageInProgress() )
tomwalters@570 322 throw MessageInProgressException();
tomwalters@570 323
tomwalters@570 324 EndElement( messageCursor_ );
tomwalters@570 325
tomwalters@570 326 return *this;
tomwalters@570 327 }
tomwalters@570 328
tomwalters@570 329
tomwalters@570 330 OutboundPacketStream& OutboundPacketStream::operator<<( const BeginMessage& rhs )
tomwalters@570 331 {
tomwalters@570 332 if( IsMessageInProgress() )
tomwalters@570 333 throw MessageInProgressException();
tomwalters@570 334
tomwalters@570 335 CheckForAvailableMessageSpace( rhs.addressPattern );
tomwalters@570 336
tomwalters@570 337 messageCursor_ = BeginElement( messageCursor_ );
tomwalters@570 338
tomwalters@570 339 strcpy( messageCursor_, rhs.addressPattern );
tomwalters@570 340 unsigned long rhsLength = strlen(rhs.addressPattern);
tomwalters@570 341 messageCursor_ += rhsLength + 1;
tomwalters@570 342
tomwalters@570 343 // zero pad to 4-byte boundary
tomwalters@570 344 unsigned long i = rhsLength + 1;
tomwalters@570 345 while( i & 0x3 ){
tomwalters@570 346 *messageCursor_++ = '\0';
tomwalters@570 347 ++i;
tomwalters@570 348 }
tomwalters@570 349
tomwalters@570 350 argumentCurrent_ = messageCursor_;
tomwalters@570 351 typeTagsCurrent_ = end_;
tomwalters@570 352
tomwalters@570 353 messageIsInProgress_ = true;
tomwalters@570 354
tomwalters@570 355 return *this;
tomwalters@570 356 }
tomwalters@570 357
tomwalters@570 358
tomwalters@570 359 OutboundPacketStream& OutboundPacketStream::operator<<( const MessageTerminator& rhs )
tomwalters@570 360 {
tomwalters@570 361 (void) rhs;
tomwalters@570 362
tomwalters@570 363 if( !IsMessageInProgress() )
tomwalters@570 364 throw MessageNotInProgressException();
tomwalters@570 365
tomwalters@570 366 int typeTagsCount = end_ - typeTagsCurrent_;
tomwalters@570 367
tomwalters@570 368 if( typeTagsCount ){
tomwalters@570 369
tomwalters@570 370 char *tempTypeTags = (char*)alloca(typeTagsCount);
tomwalters@570 371 memcpy( tempTypeTags, typeTagsCurrent_, typeTagsCount );
tomwalters@570 372
tomwalters@570 373 // slot size includes comma and null terminator
tomwalters@570 374 int typeTagSlotSize = RoundUp4( typeTagsCount + 2 );
tomwalters@570 375
tomwalters@570 376 uint32 argumentsSize = argumentCurrent_ - messageCursor_;
tomwalters@570 377
tomwalters@570 378 memmove( messageCursor_ + typeTagSlotSize, messageCursor_, argumentsSize );
tomwalters@570 379
tomwalters@570 380 messageCursor_[0] = ',';
tomwalters@570 381 // copy type tags in reverse (really forward) order
tomwalters@570 382 for( int i=0; i < typeTagsCount; ++i )
tomwalters@570 383 messageCursor_[i+1] = tempTypeTags[ (typeTagsCount-1) - i ];
tomwalters@570 384
tomwalters@570 385 char *p = messageCursor_ + 1 + typeTagsCount;
tomwalters@570 386 for( int i=0; i < (typeTagSlotSize - (typeTagsCount + 1)); ++i )
tomwalters@570 387 *p++ = '\0';
tomwalters@570 388
tomwalters@570 389 typeTagsCurrent_ = end_;
tomwalters@570 390
tomwalters@570 391 // advance messageCursor_ for next message
tomwalters@570 392 messageCursor_ += typeTagSlotSize + argumentsSize;
tomwalters@570 393
tomwalters@570 394 }else{
tomwalters@570 395 // send an empty type tags string
tomwalters@570 396 memcpy( messageCursor_, ",\0\0\0", 4 );
tomwalters@570 397
tomwalters@570 398 // advance messageCursor_ for next message
tomwalters@570 399 messageCursor_ += 4;
tomwalters@570 400 }
tomwalters@570 401
tomwalters@570 402 argumentCurrent_ = messageCursor_;
tomwalters@570 403
tomwalters@570 404 EndElement( messageCursor_ );
tomwalters@570 405
tomwalters@570 406 messageIsInProgress_ = false;
tomwalters@570 407
tomwalters@570 408 return *this;
tomwalters@570 409 }
tomwalters@570 410
tomwalters@570 411
tomwalters@570 412 OutboundPacketStream& OutboundPacketStream::operator<<( bool rhs )
tomwalters@570 413 {
tomwalters@570 414 CheckForAvailableArgumentSpace(0);
tomwalters@570 415
tomwalters@570 416 *(--typeTagsCurrent_) = (char)((rhs) ? TRUE_TYPE_TAG : FALSE_TYPE_TAG);
tomwalters@570 417
tomwalters@570 418 return *this;
tomwalters@570 419 }
tomwalters@570 420
tomwalters@570 421
tomwalters@570 422 OutboundPacketStream& OutboundPacketStream::operator<<( const NilType& rhs )
tomwalters@570 423 {
tomwalters@570 424 (void) rhs;
tomwalters@570 425 CheckForAvailableArgumentSpace(0);
tomwalters@570 426
tomwalters@570 427 *(--typeTagsCurrent_) = NIL_TYPE_TAG;
tomwalters@570 428
tomwalters@570 429 return *this;
tomwalters@570 430 }
tomwalters@570 431
tomwalters@570 432
tomwalters@570 433 OutboundPacketStream& OutboundPacketStream::operator<<( const InfinitumType& rhs )
tomwalters@570 434 {
tomwalters@570 435 (void) rhs;
tomwalters@570 436 CheckForAvailableArgumentSpace(0);
tomwalters@570 437
tomwalters@570 438 *(--typeTagsCurrent_) = INFINITUM_TYPE_TAG;
tomwalters@570 439
tomwalters@570 440 return *this;
tomwalters@570 441 }
tomwalters@570 442
tomwalters@570 443
tomwalters@570 444 OutboundPacketStream& OutboundPacketStream::operator<<( int32 rhs )
tomwalters@570 445 {
tomwalters@570 446 CheckForAvailableArgumentSpace(4);
tomwalters@570 447
tomwalters@570 448 *(--typeTagsCurrent_) = INT32_TYPE_TAG;
tomwalters@570 449 FromInt32( argumentCurrent_, rhs );
tomwalters@570 450 argumentCurrent_ += 4;
tomwalters@570 451
tomwalters@570 452 return *this;
tomwalters@570 453 }
tomwalters@570 454
tomwalters@570 455
tomwalters@570 456 OutboundPacketStream& OutboundPacketStream::operator<<( float rhs )
tomwalters@570 457 {
tomwalters@570 458 CheckForAvailableArgumentSpace(4);
tomwalters@570 459
tomwalters@570 460 *(--typeTagsCurrent_) = FLOAT_TYPE_TAG;
tomwalters@570 461
tomwalters@570 462 #ifdef OSC_HOST_LITTLE_ENDIAN
tomwalters@570 463 union{
tomwalters@570 464 float f;
tomwalters@570 465 char c[4];
tomwalters@570 466 } u;
tomwalters@570 467
tomwalters@570 468 u.f = rhs;
tomwalters@570 469
tomwalters@570 470 argumentCurrent_[3] = u.c[0];
tomwalters@570 471 argumentCurrent_[2] = u.c[1];
tomwalters@570 472 argumentCurrent_[1] = u.c[2];
tomwalters@570 473 argumentCurrent_[0] = u.c[3];
tomwalters@570 474 #else
tomwalters@570 475 *reinterpret_cast<float*>(argumentCurrent_) = rhs;
tomwalters@570 476 #endif
tomwalters@570 477
tomwalters@570 478 argumentCurrent_ += 4;
tomwalters@570 479
tomwalters@570 480 return *this;
tomwalters@570 481 }
tomwalters@570 482
tomwalters@570 483
tomwalters@570 484 OutboundPacketStream& OutboundPacketStream::operator<<( char rhs )
tomwalters@570 485 {
tomwalters@570 486 CheckForAvailableArgumentSpace(4);
tomwalters@570 487
tomwalters@570 488 *(--typeTagsCurrent_) = CHAR_TYPE_TAG;
tomwalters@570 489 FromInt32( argumentCurrent_, rhs );
tomwalters@570 490 argumentCurrent_ += 4;
tomwalters@570 491
tomwalters@570 492 return *this;
tomwalters@570 493 }
tomwalters@570 494
tomwalters@570 495
tomwalters@570 496 OutboundPacketStream& OutboundPacketStream::operator<<( const RgbaColor& rhs )
tomwalters@570 497 {
tomwalters@570 498 CheckForAvailableArgumentSpace(4);
tomwalters@570 499
tomwalters@570 500 *(--typeTagsCurrent_) = RGBA_COLOR_TYPE_TAG;
tomwalters@570 501 FromUInt32( argumentCurrent_, rhs );
tomwalters@570 502 argumentCurrent_ += 4;
tomwalters@570 503
tomwalters@570 504 return *this;
tomwalters@570 505 }
tomwalters@570 506
tomwalters@570 507
tomwalters@570 508 OutboundPacketStream& OutboundPacketStream::operator<<( const MidiMessage& rhs )
tomwalters@570 509 {
tomwalters@570 510 CheckForAvailableArgumentSpace(4);
tomwalters@570 511
tomwalters@570 512 *(--typeTagsCurrent_) = MIDI_MESSAGE_TYPE_TAG;
tomwalters@570 513 FromUInt32( argumentCurrent_, rhs );
tomwalters@570 514 argumentCurrent_ += 4;
tomwalters@570 515
tomwalters@570 516 return *this;
tomwalters@570 517 }
tomwalters@570 518
tomwalters@570 519
tomwalters@570 520 OutboundPacketStream& OutboundPacketStream::operator<<( int64 rhs )
tomwalters@570 521 {
tomwalters@570 522 CheckForAvailableArgumentSpace(8);
tomwalters@570 523
tomwalters@570 524 *(--typeTagsCurrent_) = INT64_TYPE_TAG;
tomwalters@570 525 FromInt64( argumentCurrent_, rhs );
tomwalters@570 526 argumentCurrent_ += 8;
tomwalters@570 527
tomwalters@570 528 return *this;
tomwalters@570 529 }
tomwalters@570 530
tomwalters@570 531
tomwalters@570 532 OutboundPacketStream& OutboundPacketStream::operator<<( const TimeTag& rhs )
tomwalters@570 533 {
tomwalters@570 534 CheckForAvailableArgumentSpace(8);
tomwalters@570 535
tomwalters@570 536 *(--typeTagsCurrent_) = TIME_TAG_TYPE_TAG;
tomwalters@570 537 FromUInt64( argumentCurrent_, rhs );
tomwalters@570 538 argumentCurrent_ += 8;
tomwalters@570 539
tomwalters@570 540 return *this;
tomwalters@570 541 }
tomwalters@570 542
tomwalters@570 543
tomwalters@570 544 OutboundPacketStream& OutboundPacketStream::operator<<( double rhs )
tomwalters@570 545 {
tomwalters@570 546 CheckForAvailableArgumentSpace(8);
tomwalters@570 547
tomwalters@570 548 *(--typeTagsCurrent_) = DOUBLE_TYPE_TAG;
tomwalters@570 549
tomwalters@570 550 #ifdef OSC_HOST_LITTLE_ENDIAN
tomwalters@570 551 union{
tomwalters@570 552 double f;
tomwalters@570 553 char c[8];
tomwalters@570 554 } u;
tomwalters@570 555
tomwalters@570 556 u.f = rhs;
tomwalters@570 557
tomwalters@570 558 argumentCurrent_[7] = u.c[0];
tomwalters@570 559 argumentCurrent_[6] = u.c[1];
tomwalters@570 560 argumentCurrent_[5] = u.c[2];
tomwalters@570 561 argumentCurrent_[4] = u.c[3];
tomwalters@570 562 argumentCurrent_[3] = u.c[4];
tomwalters@570 563 argumentCurrent_[2] = u.c[5];
tomwalters@570 564 argumentCurrent_[1] = u.c[6];
tomwalters@570 565 argumentCurrent_[0] = u.c[7];
tomwalters@570 566 #else
tomwalters@570 567 *reinterpret_cast<double*>(argumentCurrent_) = rhs;
tomwalters@570 568 #endif
tomwalters@570 569
tomwalters@570 570 argumentCurrent_ += 8;
tomwalters@570 571
tomwalters@570 572 return *this;
tomwalters@570 573 }
tomwalters@570 574
tomwalters@570 575
tomwalters@570 576 OutboundPacketStream& OutboundPacketStream::operator<<( const char *rhs )
tomwalters@570 577 {
tomwalters@570 578 CheckForAvailableArgumentSpace( RoundUp4(strlen(rhs) + 1) );
tomwalters@570 579
tomwalters@570 580 *(--typeTagsCurrent_) = STRING_TYPE_TAG;
tomwalters@570 581 strcpy( argumentCurrent_, rhs );
tomwalters@570 582 unsigned long rhsLength = strlen(rhs);
tomwalters@570 583 argumentCurrent_ += rhsLength + 1;
tomwalters@570 584
tomwalters@570 585 // zero pad to 4-byte boundary
tomwalters@570 586 unsigned long i = rhsLength + 1;
tomwalters@570 587 while( i & 0x3 ){
tomwalters@570 588 *argumentCurrent_++ = '\0';
tomwalters@570 589 ++i;
tomwalters@570 590 }
tomwalters@570 591
tomwalters@570 592 return *this;
tomwalters@570 593 }
tomwalters@570 594
tomwalters@570 595
tomwalters@570 596 OutboundPacketStream& OutboundPacketStream::operator<<( const Symbol& rhs )
tomwalters@570 597 {
tomwalters@570 598 CheckForAvailableArgumentSpace( RoundUp4(strlen(rhs) + 1) );
tomwalters@570 599
tomwalters@570 600 *(--typeTagsCurrent_) = SYMBOL_TYPE_TAG;
tomwalters@570 601 strcpy( argumentCurrent_, rhs );
tomwalters@570 602 unsigned long rhsLength = strlen(rhs);
tomwalters@570 603 argumentCurrent_ += rhsLength + 1;
tomwalters@570 604
tomwalters@570 605 // zero pad to 4-byte boundary
tomwalters@570 606 unsigned long i = rhsLength + 1;
tomwalters@570 607 while( i & 0x3 ){
tomwalters@570 608 *argumentCurrent_++ = '\0';
tomwalters@570 609 ++i;
tomwalters@570 610 }
tomwalters@570 611
tomwalters@570 612 return *this;
tomwalters@570 613 }
tomwalters@570 614
tomwalters@570 615
tomwalters@570 616 OutboundPacketStream& OutboundPacketStream::operator<<( const Blob& rhs )
tomwalters@570 617 {
tomwalters@570 618 CheckForAvailableArgumentSpace( 4 + RoundUp4(rhs.size) );
tomwalters@570 619
tomwalters@570 620 *(--typeTagsCurrent_) = BLOB_TYPE_TAG;
tomwalters@570 621 FromUInt32( argumentCurrent_, rhs.size );
tomwalters@570 622 argumentCurrent_ += 4;
tomwalters@570 623
tomwalters@570 624 memcpy( argumentCurrent_, rhs.data, rhs.size );
tomwalters@570 625 argumentCurrent_ += rhs.size;
tomwalters@570 626
tomwalters@570 627 // zero pad to 4-byte boundary
tomwalters@570 628 unsigned long i = rhs.size;
tomwalters@570 629 while( i & 0x3 ){
tomwalters@570 630 *argumentCurrent_++ = '\0';
tomwalters@570 631 ++i;
tomwalters@570 632 }
tomwalters@570 633
tomwalters@570 634 return *this;
tomwalters@570 635 }
tomwalters@570 636
tomwalters@570 637 } // namespace osc
tomwalters@570 638
tomwalters@570 639