OscOutboundPacketStream.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. oscpack -- Open Sound Control packet manipulation library
  3. http://www.audiomulch.com/~rossb/oscpack
  4. Copyright (c) 2004-2005 Ross Bencina <[email protected]>
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. Any person wishing to distribute modifications to the Software is
  15. requested to send the modifications to the original developer so that
  16. they can be incorporated into the canonical version.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  21. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "OscOutboundPacketStream.h"
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <assert.h>
  29. #if defined(__WIN32__) || defined(WIN32)
  30. #include <malloc.h> // for alloca
  31. #endif
  32. #include "OscHostEndianness.h"
  33. namespace osc{
  34. static void FromInt32( char *p, int32 x )
  35. {
  36. #ifdef OSC_HOST_LITTLE_ENDIAN
  37. union{
  38. osc::int32 i;
  39. char c[4];
  40. } u;
  41. u.i = x;
  42. p[3] = u.c[0];
  43. p[2] = u.c[1];
  44. p[1] = u.c[2];
  45. p[0] = u.c[3];
  46. #else
  47. *reinterpret_cast<int32*>(p) = x;
  48. #endif
  49. }
  50. static void FromUInt32( char *p, uint32 x )
  51. {
  52. #ifdef OSC_HOST_LITTLE_ENDIAN
  53. union{
  54. osc::uint32 i;
  55. char c[4];
  56. } u;
  57. u.i = x;
  58. p[3] = u.c[0];
  59. p[2] = u.c[1];
  60. p[1] = u.c[2];
  61. p[0] = u.c[3];
  62. #else
  63. *reinterpret_cast<uint32*>(p) = x;
  64. #endif
  65. }
  66. static void FromInt64( char *p, int64 x )
  67. {
  68. #ifdef OSC_HOST_LITTLE_ENDIAN
  69. union{
  70. osc::int64 i;
  71. char c[8];
  72. } u;
  73. u.i = x;
  74. p[7] = u.c[0];
  75. p[6] = u.c[1];
  76. p[5] = u.c[2];
  77. p[4] = u.c[3];
  78. p[3] = u.c[4];
  79. p[2] = u.c[5];
  80. p[1] = u.c[6];
  81. p[0] = u.c[7];
  82. #else
  83. *reinterpret_cast<int64*>(p) = x;
  84. #endif
  85. }
  86. static void FromUInt64( char *p, uint64 x )
  87. {
  88. #ifdef OSC_HOST_LITTLE_ENDIAN
  89. union{
  90. osc::uint64 i;
  91. char c[8];
  92. } u;
  93. u.i = x;
  94. p[7] = u.c[0];
  95. p[6] = u.c[1];
  96. p[5] = u.c[2];
  97. p[4] = u.c[3];
  98. p[3] = u.c[4];
  99. p[2] = u.c[5];
  100. p[1] = u.c[6];
  101. p[0] = u.c[7];
  102. #else
  103. *reinterpret_cast<uint64*>(p) = x;
  104. #endif
  105. }
  106. static inline long RoundUp4( long x )
  107. {
  108. return ((x-1) & (~0x03L)) + 4;
  109. }
  110. OutboundPacketStream::OutboundPacketStream( char *buffer, unsigned long capacity )
  111. : data_( buffer )
  112. , end_( data_ + capacity )
  113. , typeTagsCurrent_( end_ )
  114. , messageCursor_( data_ )
  115. , argumentCurrent_( data_ )
  116. , elementSizePtr_( 0 )
  117. , messageIsInProgress_( false )
  118. {
  119. }
  120. OutboundPacketStream::~OutboundPacketStream()
  121. {
  122. }
  123. char *OutboundPacketStream::BeginElement( char *beginPtr )
  124. {
  125. if( elementSizePtr_ == 0 ){
  126. elementSizePtr_ = reinterpret_cast<uint32*>(data_);
  127. return beginPtr;
  128. }else{
  129. // store an offset to the old element size ptr in the element size slot
  130. // we store an offset rather than the actual pointer to be 64 bit clean.
  131. *reinterpret_cast<uint32*>(beginPtr) =
  132. (uint32)(reinterpret_cast<char*>(elementSizePtr_) - data_);
  133. elementSizePtr_ = reinterpret_cast<uint32*>(beginPtr);
  134. return beginPtr + 4;
  135. }
  136. }
  137. void OutboundPacketStream::EndElement( char *endPtr )
  138. {
  139. assert( elementSizePtr_ != 0 );
  140. if( elementSizePtr_ == reinterpret_cast<uint32*>(data_) ){
  141. elementSizePtr_ = 0;
  142. }else{
  143. // while building an element, an offset to the containing element's
  144. // size slot is stored in the elements size slot (or a ptr to data_
  145. // if there is no containing element). We retrieve that here
  146. uint32 *previousElementSizePtr =
  147. (uint32*)(data_ + *reinterpret_cast<uint32*>(elementSizePtr_));
  148. // then we store the element size in the slot, note that the element
  149. // size does not include the size slot, hence the - 4 below.
  150. uint32 elementSize =
  151. (endPtr - reinterpret_cast<char*>(elementSizePtr_)) - 4;
  152. FromUInt32( reinterpret_cast<char*>(elementSizePtr_), elementSize );
  153. // finally, we reset the element size ptr to the containing element
  154. elementSizePtr_ = previousElementSizePtr;
  155. }
  156. }
  157. bool OutboundPacketStream::ElementSizeSlotRequired() const
  158. {
  159. return (elementSizePtr_ != 0);
  160. }
  161. void OutboundPacketStream::CheckForAvailableBundleSpace()
  162. {
  163. unsigned long required = Size() + ((ElementSizeSlotRequired())?4:0) + 16;
  164. if( required > Capacity() )
  165. throw OutOfBufferMemoryException();
  166. }
  167. void OutboundPacketStream::CheckForAvailableMessageSpace( const char *addressPattern )
  168. {
  169. // plus 4 for at least four bytes of type tag
  170. unsigned long required = Size() + ((ElementSizeSlotRequired())?4:0)
  171. + RoundUp4(strlen(addressPattern) + 1) + 4;
  172. if( required > Capacity() )
  173. throw OutOfBufferMemoryException();
  174. }
  175. void OutboundPacketStream::CheckForAvailableArgumentSpace( long argumentLength )
  176. {
  177. // plus three for extra type tag, comma and null terminator
  178. unsigned long required = (argumentCurrent_ - data_) + argumentLength
  179. + RoundUp4( (end_ - typeTagsCurrent_) + 3 );
  180. if( required > Capacity() )
  181. throw OutOfBufferMemoryException();
  182. }
  183. void OutboundPacketStream::Clear()
  184. {
  185. typeTagsCurrent_ = end_;
  186. messageCursor_ = data_;
  187. argumentCurrent_ = data_;
  188. elementSizePtr_ = 0;
  189. messageIsInProgress_ = false;
  190. }
  191. unsigned int OutboundPacketStream::Capacity() const
  192. {
  193. return end_ - data_;
  194. }
  195. unsigned int OutboundPacketStream::Size() const
  196. {
  197. unsigned int result = argumentCurrent_ - data_;
  198. if( IsMessageInProgress() ){
  199. // account for the length of the type tag string. the total type tag
  200. // includes an initial comma, plus at least one terminating \0
  201. result += RoundUp4( (end_ - typeTagsCurrent_) + 2 );
  202. }
  203. return result;
  204. }
  205. const char *OutboundPacketStream::Data() const
  206. {
  207. return data_;
  208. }
  209. bool OutboundPacketStream::IsReady() const
  210. {
  211. return (!IsMessageInProgress() && !IsBundleInProgress());
  212. }
  213. bool OutboundPacketStream::IsMessageInProgress() const
  214. {
  215. return messageIsInProgress_;
  216. }
  217. bool OutboundPacketStream::IsBundleInProgress() const
  218. {
  219. return (elementSizePtr_ != 0);
  220. }
  221. OutboundPacketStream& OutboundPacketStream::operator<<( const BundleInitiator& rhs )
  222. {
  223. if( IsMessageInProgress() )
  224. throw MessageInProgressException();
  225. CheckForAvailableBundleSpace();
  226. messageCursor_ = BeginElement( messageCursor_ );
  227. memcpy( messageCursor_, "#bundle\0", 8 );
  228. FromUInt64( messageCursor_ + 8, rhs.timeTag );
  229. messageCursor_ += 16;
  230. argumentCurrent_ = messageCursor_;
  231. return *this;
  232. }
  233. OutboundPacketStream& OutboundPacketStream::operator<<( const BundleTerminator& rhs )
  234. {
  235. (void) rhs;
  236. if( !IsBundleInProgress() )
  237. throw BundleNotInProgressException();
  238. if( IsMessageInProgress() )
  239. throw MessageInProgressException();
  240. EndElement( messageCursor_ );
  241. return *this;
  242. }
  243. OutboundPacketStream& OutboundPacketStream::operator<<( const BeginMessage& rhs )
  244. {
  245. if( IsMessageInProgress() )
  246. throw MessageInProgressException();
  247. CheckForAvailableMessageSpace( rhs.addressPattern );
  248. messageCursor_ = BeginElement( messageCursor_ );
  249. strcpy( messageCursor_, rhs.addressPattern );
  250. unsigned long rhsLength = strlen(rhs.addressPattern);
  251. messageCursor_ += rhsLength + 1;
  252. // zero pad to 4-byte boundary
  253. unsigned long i = rhsLength + 1;
  254. while( i & 0x3 ){
  255. *messageCursor_++ = '\0';
  256. ++i;
  257. }
  258. argumentCurrent_ = messageCursor_;
  259. typeTagsCurrent_ = end_;
  260. messageIsInProgress_ = true;
  261. return *this;
  262. }
  263. OutboundPacketStream& OutboundPacketStream::operator<<( const MessageTerminator& rhs )
  264. {
  265. (void) rhs;
  266. if( !IsMessageInProgress() )
  267. throw MessageNotInProgressException();
  268. int typeTagsCount = end_ - typeTagsCurrent_;
  269. if( typeTagsCount ){
  270. char *tempTypeTags = (char*)alloca(typeTagsCount);
  271. memcpy( tempTypeTags, typeTagsCurrent_, typeTagsCount );
  272. // slot size includes comma and null terminator
  273. int typeTagSlotSize = RoundUp4( typeTagsCount + 2 );
  274. uint32 argumentsSize = argumentCurrent_ - messageCursor_;
  275. memmove( messageCursor_ + typeTagSlotSize, messageCursor_, argumentsSize );
  276. messageCursor_[0] = ',';
  277. // copy type tags in reverse (really forward) order
  278. for( int i=0; i < typeTagsCount; ++i )
  279. messageCursor_[i+1] = tempTypeTags[ (typeTagsCount-1) - i ];
  280. char *p = messageCursor_ + 1 + typeTagsCount;
  281. for( int i=0; i < (typeTagSlotSize - (typeTagsCount + 1)); ++i )
  282. *p++ = '\0';
  283. typeTagsCurrent_ = end_;
  284. // advance messageCursor_ for next message
  285. messageCursor_ += typeTagSlotSize + argumentsSize;
  286. }else{
  287. // send an empty type tags string
  288. memcpy( messageCursor_, ",\0\0\0", 4 );
  289. // advance messageCursor_ for next message
  290. messageCursor_ += 4;
  291. }
  292. argumentCurrent_ = messageCursor_;
  293. EndElement( messageCursor_ );
  294. messageIsInProgress_ = false;
  295. return *this;
  296. }
  297. OutboundPacketStream& OutboundPacketStream::operator<<( bool rhs )
  298. {
  299. CheckForAvailableArgumentSpace(0);
  300. *(--typeTagsCurrent_) = (char)((rhs) ? TRUE_TYPE_TAG : FALSE_TYPE_TAG);
  301. return *this;
  302. }
  303. OutboundPacketStream& OutboundPacketStream::operator<<( const NilType& rhs )
  304. {
  305. (void) rhs;
  306. CheckForAvailableArgumentSpace(0);
  307. *(--typeTagsCurrent_) = NIL_TYPE_TAG;
  308. return *this;
  309. }
  310. OutboundPacketStream& OutboundPacketStream::operator<<( const InfinitumType& rhs )
  311. {
  312. (void) rhs;
  313. CheckForAvailableArgumentSpace(0);
  314. *(--typeTagsCurrent_) = INFINITUM_TYPE_TAG;
  315. return *this;
  316. }
  317. OutboundPacketStream& OutboundPacketStream::operator<<( int32 rhs )
  318. {
  319. CheckForAvailableArgumentSpace(4);
  320. *(--typeTagsCurrent_) = INT32_TYPE_TAG;
  321. FromInt32( argumentCurrent_, rhs );
  322. argumentCurrent_ += 4;
  323. return *this;
  324. }
  325. OutboundPacketStream& OutboundPacketStream::operator<<( float rhs )
  326. {
  327. CheckForAvailableArgumentSpace(4);
  328. *(--typeTagsCurrent_) = FLOAT_TYPE_TAG;
  329. #ifdef OSC_HOST_LITTLE_ENDIAN
  330. union{
  331. float f;
  332. char c[4];
  333. } u;
  334. u.f = rhs;
  335. argumentCurrent_[3] = u.c[0];
  336. argumentCurrent_[2] = u.c[1];
  337. argumentCurrent_[1] = u.c[2];
  338. argumentCurrent_[0] = u.c[3];
  339. #else
  340. *reinterpret_cast<float*>(argumentCurrent_) = rhs;
  341. #endif
  342. argumentCurrent_ += 4;
  343. return *this;
  344. }
  345. OutboundPacketStream& OutboundPacketStream::operator<<( char rhs )
  346. {
  347. CheckForAvailableArgumentSpace(4);
  348. *(--typeTagsCurrent_) = CHAR_TYPE_TAG;
  349. FromInt32( argumentCurrent_, rhs );
  350. argumentCurrent_ += 4;
  351. return *this;
  352. }
  353. OutboundPacketStream& OutboundPacketStream::operator<<( const RgbaColor& rhs )
  354. {
  355. CheckForAvailableArgumentSpace(4);
  356. *(--typeTagsCurrent_) = RGBA_COLOR_TYPE_TAG;
  357. FromUInt32( argumentCurrent_, rhs );
  358. argumentCurrent_ += 4;
  359. return *this;
  360. }
  361. OutboundPacketStream& OutboundPacketStream::operator<<( const MidiMessage& rhs )
  362. {
  363. CheckForAvailableArgumentSpace(4);
  364. *(--typeTagsCurrent_) = MIDI_MESSAGE_TYPE_TAG;
  365. FromUInt32( argumentCurrent_, rhs );
  366. argumentCurrent_ += 4;
  367. return *this;
  368. }
  369. OutboundPacketStream& OutboundPacketStream::operator<<( int64 rhs )
  370. {
  371. CheckForAvailableArgumentSpace(8);
  372. *(--typeTagsCurrent_) = INT64_TYPE_TAG;
  373. FromInt64( argumentCurrent_, rhs );
  374. argumentCurrent_ += 8;
  375. return *this;
  376. }
  377. OutboundPacketStream& OutboundPacketStream::operator<<( const TimeTag& rhs )
  378. {
  379. CheckForAvailableArgumentSpace(8);
  380. *(--typeTagsCurrent_) = TIME_TAG_TYPE_TAG;
  381. FromUInt64( argumentCurrent_, rhs );
  382. argumentCurrent_ += 8;
  383. return *this;
  384. }
  385. OutboundPacketStream& OutboundPacketStream::operator<<( double rhs )
  386. {
  387. CheckForAvailableArgumentSpace(8);
  388. *(--typeTagsCurrent_) = DOUBLE_TYPE_TAG;
  389. #ifdef OSC_HOST_LITTLE_ENDIAN
  390. union{
  391. double f;
  392. char c[8];
  393. } u;
  394. u.f = rhs;
  395. argumentCurrent_[7] = u.c[0];
  396. argumentCurrent_[6] = u.c[1];
  397. argumentCurrent_[5] = u.c[2];
  398. argumentCurrent_[4] = u.c[3];
  399. argumentCurrent_[3] = u.c[4];
  400. argumentCurrent_[2] = u.c[5];
  401. argumentCurrent_[1] = u.c[6];
  402. argumentCurrent_[0] = u.c[7];
  403. #else
  404. *reinterpret_cast<double*>(argumentCurrent_) = rhs;
  405. #endif
  406. argumentCurrent_ += 8;
  407. return *this;
  408. }
  409. OutboundPacketStream& OutboundPacketStream::operator<<( const char *rhs )
  410. {
  411. CheckForAvailableArgumentSpace( RoundUp4(strlen(rhs) + 1) );
  412. *(--typeTagsCurrent_) = STRING_TYPE_TAG;
  413. strcpy( argumentCurrent_, rhs );
  414. unsigned long rhsLength = strlen(rhs);
  415. argumentCurrent_ += rhsLength + 1;
  416. // zero pad to 4-byte boundary
  417. unsigned long i = rhsLength + 1;
  418. while( i & 0x3 ){
  419. *argumentCurrent_++ = '\0';
  420. ++i;
  421. }
  422. return *this;
  423. }
  424. OutboundPacketStream& OutboundPacketStream::operator<<( const Symbol& rhs )
  425. {
  426. CheckForAvailableArgumentSpace( RoundUp4(strlen(rhs) + 1) );
  427. *(--typeTagsCurrent_) = SYMBOL_TYPE_TAG;
  428. strcpy( argumentCurrent_, rhs );
  429. unsigned long rhsLength = strlen(rhs);
  430. argumentCurrent_ += rhsLength + 1;
  431. // zero pad to 4-byte boundary
  432. unsigned long i = rhsLength + 1;
  433. while( i & 0x3 ){
  434. *argumentCurrent_++ = '\0';
  435. ++i;
  436. }
  437. return *this;
  438. }
  439. OutboundPacketStream& OutboundPacketStream::operator<<( const Blob& rhs )
  440. {
  441. CheckForAvailableArgumentSpace( 4 + RoundUp4(rhs.size) );
  442. *(--typeTagsCurrent_) = BLOB_TYPE_TAG;
  443. FromUInt32( argumentCurrent_, rhs.size );
  444. argumentCurrent_ += 4;
  445. memcpy( argumentCurrent_, rhs.data, rhs.size );
  446. argumentCurrent_ += rhs.size;
  447. // zero pad to 4-byte boundary
  448. unsigned long i = rhs.size;
  449. while( i & 0x3 ){
  450. *argumentCurrent_++ = '\0';
  451. ++i;
  452. }
  453. return *this;
  454. }
  455. } // namespace osc