00001 /* 00002 00003 oscpack -- Open Sound Control packet manipulation library 00004 00005 http://www.audiomulch.com/~rossb/oscpack 00006 00007 00008 00009 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com> 00010 00011 00012 00013 Permission is hereby granted, free of charge, to any person obtaining 00014 00015 a copy of this software and associated documentation files 00016 00017 (the "Software"), to deal in the Software without restriction, 00018 00019 including without limitation the rights to use, copy, modify, merge, 00020 00021 publish, distribute, sublicense, and/or sell copies of the Software, 00022 00023 and to permit persons to whom the Software is furnished to do so, 00024 00025 subject to the following conditions: 00026 00027 00028 00029 The above copyright notice and this permission notice shall be 00030 00031 included in all copies or substantial portions of the Software. 00032 00033 00034 00035 Any person wishing to distribute modifications to the Software is 00036 00037 requested to send the modifications to the original developer so that 00038 00039 they can be incorporated into the canonical version. 00040 00041 00042 00043 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00044 00045 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00046 00047 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00048 00049 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00050 00051 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00052 00053 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00054 00055 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00056 00057 */ 00058 00059 #ifndef INCLUDED_OSCOUTBOUNDPACKET_H 00060 00061 #define INCLUDED_OSCOUTBOUNDPACKET_H 00062 00063 00064 00065 #include "OscTypes.h" 00066 00067 #include "OscException.h" 00068 00069 00070 00071 00072 00073 namespace osc{ 00074 00075 00076 00077 class OutOfBufferMemoryException : public Exception{ 00078 00079 public: 00080 00081 OutOfBufferMemoryException( const char *w="out of buffer memory" ) 00082 00083 : Exception( w ) {} 00084 00085 }; 00086 00087 00088 00089 class BundleNotInProgressException : public Exception{ 00090 00091 public: 00092 00093 BundleNotInProgressException( 00094 00095 const char *w="call to EndBundle when bundle is not in progress" ) 00096 00097 : Exception( w ) {} 00098 00099 }; 00100 00101 00102 00103 class MessageInProgressException : public Exception{ 00104 00105 public: 00106 00107 MessageInProgressException( 00108 00109 const char *w="opening or closing bundle or message while message is in progress" ) 00110 00111 : Exception( w ) {} 00112 00113 }; 00114 00115 00116 00117 class MessageNotInProgressException : public Exception{ 00118 00119 public: 00120 00121 MessageNotInProgressException( 00122 00123 const char *w="call to EndMessage when message is not in progress" ) 00124 00125 : Exception( w ) {} 00126 00127 }; 00128 00129 00130 00131 00132 00133 class OutboundPacketStream{ 00134 00135 public: 00136 00137 OutboundPacketStream( char *buffer, unsigned long capacity ); 00138 00139 ~OutboundPacketStream(); 00140 00141 00142 00143 void Clear(); 00144 00145 00146 00147 unsigned int Capacity() const; 00148 00149 00150 00151 // invariant: size() is valid even while building a message. 00152 00153 unsigned int Size() const; 00154 00155 00156 00157 const char *Data() const; 00158 00159 00160 00161 // indicates that all messages have been closed with a matching EndMessage 00162 00163 // and all bundles have been closed with a matching EndBundle 00164 00165 bool IsReady() const; 00166 00167 00168 00169 bool IsMessageInProgress() const; 00170 00171 bool IsBundleInProgress() const; 00172 00173 00174 00175 OutboundPacketStream& operator<<( const BundleInitiator& rhs ); 00176 00177 OutboundPacketStream& operator<<( const BundleTerminator& rhs ); 00178 00179 00180 00181 OutboundPacketStream& operator<<( const BeginMessage& rhs ); 00182 00183 OutboundPacketStream& operator<<( const MessageTerminator& rhs ); 00184 00185 00186 00187 OutboundPacketStream& operator<<( bool rhs ); 00188 00189 OutboundPacketStream& operator<<( const NilType& rhs ); 00190 00191 OutboundPacketStream& operator<<( const InfinitumType& rhs ); 00192 00193 OutboundPacketStream& operator<<( int32 rhs ); 00194 00195 00196 00197 #ifndef __x86_64__ 00198 00199 OutboundPacketStream& operator<<( int rhs ) 00200 00201 { *this << (int32)rhs; return *this; } 00202 00203 #endif 00204 00205 00206 00207 OutboundPacketStream& operator<<( float rhs ); 00208 00209 OutboundPacketStream& operator<<( char rhs ); 00210 00211 OutboundPacketStream& operator<<( const RgbaColor& rhs ); 00212 00213 OutboundPacketStream& operator<<( const MidiMessage& rhs ); 00214 00215 OutboundPacketStream& operator<<( int64 rhs ); 00216 00217 OutboundPacketStream& operator<<( const TimeTag& rhs ); 00218 00219 OutboundPacketStream& operator<<( double rhs ); 00220 00221 OutboundPacketStream& operator<<( const char* rhs ); 00222 00223 OutboundPacketStream& operator<<( const Symbol& rhs ); 00224 00225 OutboundPacketStream& operator<<( const Blob& rhs ); 00226 00227 00228 00229 private: 00230 00231 00232 00233 char *BeginElement( char *beginPtr ); 00234 00235 void EndElement( char *endPtr ); 00236 00237 00238 00239 bool ElementSizeSlotRequired() const; 00240 00241 void CheckForAvailableBundleSpace(); 00242 00243 void CheckForAvailableMessageSpace( const char *addressPattern ); 00244 00245 void CheckForAvailableArgumentSpace( long argumentLength ); 00246 00247 00248 00249 char *data_; 00250 00251 char *end_; 00252 00253 00254 00255 char *typeTagsCurrent_; // stored in reverse order 00256 00257 char *messageCursor_; 00258 00259 char *argumentCurrent_; 00260 00261 00262 00263 // elementSizePtr_ has two special values: 0 indicates that a bundle 00264 00265 // isn't open, and elementSizePtr_==data_ indicates that a bundle is 00266 00267 // open but that it doesn't have a size slot (ie the outermost bundle) 00268 00269 uint32 *elementSizePtr_; 00270 00271 00272 00273 bool messageIsInProgress_; 00274 00275 }; 00276 00277 00278 00279 } // namespace osc 00280 00281 00282 00283 #endif /* INCLUDED_OSC_OUTBOUND_PACKET_H */ 00284