00001 /* 00002 oscpack -- Open Sound Control packet manipulation library 00003 http://www.audiomulch.com/~rossb/oscpack 00004 00005 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com> 00006 00007 Permission is hereby granted, free of charge, to any person obtaining 00008 a copy of this software and associated documentation files 00009 (the "Software"), to deal in the Software without restriction, 00010 including without limitation the rights to use, copy, modify, merge, 00011 publish, distribute, sublicense, and/or sell copies of the Software, 00012 and to permit persons to whom the Software is furnished to do so, 00013 subject to the following conditions: 00014 00015 The above copyright notice and this permission notice shall be 00016 included in all copies or substantial portions of the Software. 00017 00018 Any person wishing to distribute modifications to the Software is 00019 requested to send the modifications to the original developer so that 00020 they can be incorporated into the canonical version. 00021 00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00023 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00024 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00025 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00026 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00027 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00028 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00029 */ 00030 #ifndef INCLUDED_OSCOUTBOUNDPACKET_H 00031 #define INCLUDED_OSCOUTBOUNDPACKET_H 00032 00033 #include "OscTypes.h" 00034 #include "OscException.h" 00035 00036 00037 namespace osc{ 00038 00039 class OutOfBufferMemoryException : public Exception{ 00040 public: 00041 OutOfBufferMemoryException( const char *w="out of buffer memory" ) 00042 : Exception( w ) {} 00043 }; 00044 00045 class BundleNotInProgressException : public Exception{ 00046 public: 00047 BundleNotInProgressException( 00048 const char *w="call to EndBundle when bundle is not in progress" ) 00049 : Exception( w ) {} 00050 }; 00051 00052 class MessageInProgressException : public Exception{ 00053 public: 00054 MessageInProgressException( 00055 const char *w="opening or closing bundle or message while message is in progress" ) 00056 : Exception( w ) {} 00057 }; 00058 00059 class MessageNotInProgressException : public Exception{ 00060 public: 00061 MessageNotInProgressException( 00062 const char *w="call to EndMessage when message is not in progress" ) 00063 : Exception( w ) {} 00064 }; 00065 00066 00067 class OutboundPacketStream{ 00068 public: 00069 OutboundPacketStream( char *buffer, unsigned long capacity ); 00070 ~OutboundPacketStream(); 00071 00072 void Clear(); 00073 00074 unsigned int Capacity() const; 00075 00076 // invariant: size() is valid even while building a message. 00077 unsigned int Size() const; 00078 00079 const char *Data() const; 00080 00081 // indicates that all messages have been closed with a matching EndMessage 00082 // and all bundles have been closed with a matching EndBundle 00083 bool IsReady() const; 00084 00085 bool IsMessageInProgress() const; 00086 bool IsBundleInProgress() const; 00087 00088 OutboundPacketStream& operator<<( const BundleInitiator& rhs ); 00089 OutboundPacketStream& operator<<( const BundleTerminator& rhs ); 00090 00091 OutboundPacketStream& operator<<( const BeginMessage& rhs ); 00092 OutboundPacketStream& operator<<( const MessageTerminator& rhs ); 00093 00094 OutboundPacketStream& operator<<( bool rhs ); 00095 OutboundPacketStream& operator<<( const NilType& rhs ); 00096 OutboundPacketStream& operator<<( const InfinitumType& rhs ); 00097 OutboundPacketStream& operator<<( int32 rhs ); 00098 00099 #ifndef __x86_64__ 00100 OutboundPacketStream& operator<<( int rhs ) 00101 { *this << (int32)rhs; return *this; } 00102 #endif 00103 00104 OutboundPacketStream& operator<<( float rhs ); 00105 OutboundPacketStream& operator<<( char rhs ); 00106 OutboundPacketStream& operator<<( const RgbaColor& rhs ); 00107 OutboundPacketStream& operator<<( const MidiMessage& rhs ); 00108 OutboundPacketStream& operator<<( int64 rhs ); 00109 OutboundPacketStream& operator<<( const TimeTag& rhs ); 00110 OutboundPacketStream& operator<<( double rhs ); 00111 OutboundPacketStream& operator<<( const char* rhs ); 00112 OutboundPacketStream& operator<<( const Symbol& rhs ); 00113 OutboundPacketStream& operator<<( const Blob& rhs ); 00114 00115 private: 00116 00117 char *BeginElement( char *beginPtr ); 00118 void EndElement( char *endPtr ); 00119 00120 bool ElementSizeSlotRequired() const; 00121 void CheckForAvailableBundleSpace(); 00122 void CheckForAvailableMessageSpace( const char *addressPattern ); 00123 void CheckForAvailableArgumentSpace( long argumentLength ); 00124 00125 char *data_; 00126 char *end_; 00127 00128 char *typeTagsCurrent_; // stored in reverse order 00129 char *messageCursor_; 00130 char *argumentCurrent_; 00131 00132 // elementSizePtr_ has two special values: 0 indicates that a bundle 00133 // isn't open, and elementSizePtr_==data_ indicates that a bundle is 00134 // open but that it doesn't have a size slot (ie the outermost bundle) 00135 uint32 *elementSizePtr_; 00136 00137 bool messageIsInProgress_; 00138 }; 00139 00140 } // namespace osc 00141 00142 #endif /* INCLUDED_OSC_OUTBOUND_PACKET_H */