OscOutboundPacketStream.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef INCLUDED_OSCOUTBOUNDPACKET_H
  26. #define INCLUDED_OSCOUTBOUNDPACKET_H
  27. #include "OscTypes.h"
  28. #include "OscException.h"
  29. namespace osc{
  30. class OutOfBufferMemoryException : public Exception{
  31. public:
  32. OutOfBufferMemoryException( const char *w="out of buffer memory" )
  33. : Exception( w ) {}
  34. };
  35. class BundleNotInProgressException : public Exception{
  36. public:
  37. BundleNotInProgressException(
  38. const char *w="call to EndBundle when bundle is not in progress" )
  39. : Exception( w ) {}
  40. };
  41. class MessageInProgressException : public Exception{
  42. public:
  43. MessageInProgressException(
  44. const char *w="opening or closing bundle or message while message is in progress" )
  45. : Exception( w ) {}
  46. };
  47. class MessageNotInProgressException : public Exception{
  48. public:
  49. MessageNotInProgressException(
  50. const char *w="call to EndMessage when message is not in progress" )
  51. : Exception( w ) {}
  52. };
  53. class OutboundPacketStream{
  54. public:
  55. OutboundPacketStream( char *buffer, unsigned long capacity );
  56. ~OutboundPacketStream();
  57. void Clear();
  58. unsigned int Capacity() const;
  59. // invariant: size() is valid even while building a message.
  60. unsigned int Size() const;
  61. const char *Data() const;
  62. // indicates that all messages have been closed with a matching EndMessage
  63. // and all bundles have been closed with a matching EndBundle
  64. bool IsReady() const;
  65. bool IsMessageInProgress() const;
  66. bool IsBundleInProgress() const;
  67. OutboundPacketStream& operator<<( const BundleInitiator& rhs );
  68. OutboundPacketStream& operator<<( const BundleTerminator& rhs );
  69. OutboundPacketStream& operator<<( const BeginMessage& rhs );
  70. OutboundPacketStream& operator<<( const MessageTerminator& rhs );
  71. OutboundPacketStream& operator<<( bool rhs );
  72. OutboundPacketStream& operator<<( const NilType& rhs );
  73. OutboundPacketStream& operator<<( const InfinitumType& rhs );
  74. OutboundPacketStream& operator<<( int32 rhs );
  75. #ifndef __x86_64__
  76. OutboundPacketStream& operator<<( int rhs )
  77. { *this << (int32)rhs; return *this; }
  78. #endif
  79. OutboundPacketStream& operator<<( float rhs );
  80. OutboundPacketStream& operator<<( char rhs );
  81. OutboundPacketStream& operator<<( const RgbaColor& rhs );
  82. OutboundPacketStream& operator<<( const MidiMessage& rhs );
  83. OutboundPacketStream& operator<<( int64 rhs );
  84. OutboundPacketStream& operator<<( const TimeTag& rhs );
  85. OutboundPacketStream& operator<<( double rhs );
  86. OutboundPacketStream& operator<<( const char* rhs );
  87. OutboundPacketStream& operator<<( const Symbol& rhs );
  88. OutboundPacketStream& operator<<( const Blob& rhs );
  89. private:
  90. char *BeginElement( char *beginPtr );
  91. void EndElement( char *endPtr );
  92. bool ElementSizeSlotRequired() const;
  93. void CheckForAvailableBundleSpace();
  94. void CheckForAvailableMessageSpace( const char *addressPattern );
  95. void CheckForAvailableArgumentSpace( long argumentLength );
  96. char *data_;
  97. char *end_;
  98. char *typeTagsCurrent_; // stored in reverse order
  99. char *messageCursor_;
  100. char *argumentCurrent_;
  101. // elementSizePtr_ has two special values: 0 indicates that a bundle
  102. // isn't open, and elementSizePtr_==data_ indicates that a bundle is
  103. // open but that it doesn't have a size slot (ie the outermost bundle)
  104. uint32 *elementSizePtr_;
  105. bool messageIsInProgress_;
  106. };
  107. } // namespace osc
  108. #endif /* INCLUDED_OSC_OUTBOUND_PACKET_H */