andrew@0: /* andrew@0: andrew@0: Copyright (c) 2007-2009, Damian Stewart andrew@0: All rights reserved. andrew@0: andrew@0: Redistribution and use in source and binary forms, with or without andrew@0: modification, are permitted provided that the following conditions are met: andrew@0: * Redistributions of source code must retain the above copyright andrew@0: notice, this list of conditions and the following disclaimer. andrew@0: * Redistributions in binary form must reproduce the above copyright andrew@0: notice, this list of conditions and the following disclaimer in the andrew@0: documentation and/or other materials provided with the distribution. andrew@0: * Neither the name of the developer nor the andrew@0: names of its contributors may be used to endorse or promote products andrew@0: derived from this software without specific prior written permission. andrew@0: andrew@0: THIS SOFTWARE IS PROVIDED BY DAMIAN STEWART ''AS IS'' AND ANY andrew@0: EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED andrew@0: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE andrew@0: DISCLAIMED. IN NO EVENT SHALL DAMIAN STEWART BE LIABLE FOR ANY andrew@0: DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES andrew@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; andrew@0: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND andrew@0: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT andrew@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS andrew@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. andrew@0: */ andrew@0: andrew@0: andrew@0: #ifndef _ofxOscRECEIVER_H andrew@0: #define _ofxOscRECEIVER_H andrew@0: andrew@0: #include andrew@0: #include "ofMain.h" andrew@0: andrew@0: #ifdef TARGET_WIN32 andrew@0: // threads andrew@0: #include andrew@0: #else andrew@0: // threads andrew@0: #include andrew@0: #endif andrew@0: andrew@0: // osc andrew@0: #include "OscTypes.h" andrew@0: #include "OscPacketListener.h" andrew@0: #include "UdpSocket.h" andrew@0: andrew@0: // ofxOsc andrew@0: #include "ofxOscMessage.h" andrew@0: andrew@0: class ofxOscReceiver : public osc::OscPacketListener andrew@0: { andrew@0: public: andrew@0: ofxOscReceiver(); andrew@0: ~ofxOscReceiver(); andrew@0: andrew@0: /// listen_port is the port to listen for messages on andrew@0: void setup( int listen_port ); andrew@0: andrew@0: /// returns true if there are any messages waiting for collection andrew@0: bool hasWaitingMessages(); andrew@0: /// take the next message on the queue of received messages, copy its details into message, and andrew@0: /// remove it from the queue. return false if there are no more messages to be got, otherwise andrew@0: /// return true andrew@0: bool getNextMessage( ofxOscMessage* ); andrew@0: andrew@0: protected: andrew@0: /// process an incoming osc message and add it to the queue andrew@0: virtual void ProcessMessage( const osc::ReceivedMessage &m, const IpEndpointName& remoteEndpoint ); andrew@0: andrew@0: private: andrew@0: // shutdown the listener andrew@0: void shutdown(); andrew@0: andrew@0: // start the listening thread andrew@0: #ifdef TARGET_WIN32 andrew@0: static DWORD WINAPI startThread( void* ofxOscReceiverInstance ); andrew@0: #else andrew@0: static void* startThread( void* ofxOscReceiverInstance ); andrew@0: #endif andrew@0: // queue of osc messages andrew@0: std::deque< ofxOscMessage* > messages; andrew@0: andrew@0: // socket to listen on andrew@0: UdpListeningReceiveSocket* listen_socket; andrew@0: andrew@0: // mutex helpers andrew@0: void grabMutex(); andrew@0: void releaseMutex(); andrew@0: andrew@0: #ifdef TARGET_WIN32 andrew@0: // thread to listen with andrew@0: HANDLE thread; andrew@0: // mutex for the thread queue andrew@0: HANDLE mutex; andrew@0: #else andrew@0: // thread to listen with andrew@0: pthread_t thread; andrew@0: // mutex for the message queue andrew@0: pthread_mutex_t mutex; andrew@0: #endif andrew@0: // ready to be deleted andrew@0: bool socketHasShutdown; andrew@0: andrew@0: }; andrew@0: andrew@0: #endif