You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

685 lines
16KB

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