andrew@0
|
1 /*
|
andrew@0
|
2
|
andrew@0
|
3 Copyright (c) 2007-2009, Damian Stewart
|
andrew@0
|
4 All rights reserved.
|
andrew@0
|
5
|
andrew@0
|
6 Redistribution and use in source and binary forms, with or without
|
andrew@0
|
7 modification, are permitted provided that the following conditions are met:
|
andrew@0
|
8 * Redistributions of source code must retain the above copyright
|
andrew@0
|
9 notice, this list of conditions and the following disclaimer.
|
andrew@0
|
10 * Redistributions in binary form must reproduce the above copyright
|
andrew@0
|
11 notice, this list of conditions and the following disclaimer in the
|
andrew@0
|
12 documentation and/or other materials provided with the distribution.
|
andrew@0
|
13 * Neither the name of the developer nor the
|
andrew@0
|
14 names of its contributors may be used to endorse or promote products
|
andrew@0
|
15 derived from this software without specific prior written permission.
|
andrew@0
|
16
|
andrew@0
|
17 THIS SOFTWARE IS PROVIDED BY DAMIAN STEWART ''AS IS'' AND ANY
|
andrew@0
|
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
andrew@0
|
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
andrew@0
|
20 DISCLAIMED. IN NO EVENT SHALL DAMIAN STEWART BE LIABLE FOR ANY
|
andrew@0
|
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
andrew@0
|
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
andrew@0
|
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
andrew@0
|
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
andrew@0
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
andrew@0
|
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
andrew@0
|
27 */
|
andrew@0
|
28
|
andrew@0
|
29
|
andrew@0
|
30 #ifndef _ofxOscRECEIVER_H
|
andrew@0
|
31 #define _ofxOscRECEIVER_H
|
andrew@0
|
32
|
andrew@0
|
33 #include <deque>
|
andrew@0
|
34 #include "ofMain.h"
|
andrew@0
|
35
|
andrew@0
|
36 #ifdef TARGET_WIN32
|
andrew@0
|
37 // threads
|
andrew@0
|
38 #include <windows.h>
|
andrew@0
|
39 #else
|
andrew@0
|
40 // threads
|
andrew@0
|
41 #include <pthread.h>
|
andrew@0
|
42 #endif
|
andrew@0
|
43
|
andrew@0
|
44 // osc
|
andrew@0
|
45 #include "OscTypes.h"
|
andrew@0
|
46 #include "OscPacketListener.h"
|
andrew@0
|
47 #include "UdpSocket.h"
|
andrew@0
|
48
|
andrew@0
|
49 // ofxOsc
|
andrew@0
|
50 #include "ofxOscMessage.h"
|
andrew@0
|
51
|
andrew@0
|
52 class ofxOscReceiver : public osc::OscPacketListener
|
andrew@0
|
53 {
|
andrew@0
|
54 public:
|
andrew@0
|
55 ofxOscReceiver();
|
andrew@0
|
56 ~ofxOscReceiver();
|
andrew@0
|
57
|
andrew@0
|
58 /// listen_port is the port to listen for messages on
|
andrew@0
|
59 void setup( int listen_port );
|
andrew@0
|
60
|
andrew@0
|
61 /// returns true if there are any messages waiting for collection
|
andrew@0
|
62 bool hasWaitingMessages();
|
andrew@0
|
63 /// take the next message on the queue of received messages, copy its details into message, and
|
andrew@0
|
64 /// remove it from the queue. return false if there are no more messages to be got, otherwise
|
andrew@0
|
65 /// return true
|
andrew@0
|
66 bool getNextMessage( ofxOscMessage* );
|
andrew@0
|
67
|
andrew@0
|
68 protected:
|
andrew@0
|
69 /// process an incoming osc message and add it to the queue
|
andrew@0
|
70 virtual void ProcessMessage( const osc::ReceivedMessage &m, const IpEndpointName& remoteEndpoint );
|
andrew@0
|
71
|
andrew@0
|
72 private:
|
andrew@0
|
73 // shutdown the listener
|
andrew@0
|
74 void shutdown();
|
andrew@0
|
75
|
andrew@0
|
76 // start the listening thread
|
andrew@0
|
77 #ifdef TARGET_WIN32
|
andrew@0
|
78 static DWORD WINAPI startThread( void* ofxOscReceiverInstance );
|
andrew@0
|
79 #else
|
andrew@0
|
80 static void* startThread( void* ofxOscReceiverInstance );
|
andrew@0
|
81 #endif
|
andrew@0
|
82 // queue of osc messages
|
andrew@0
|
83 std::deque< ofxOscMessage* > messages;
|
andrew@0
|
84
|
andrew@0
|
85 // socket to listen on
|
andrew@0
|
86 UdpListeningReceiveSocket* listen_socket;
|
andrew@0
|
87
|
andrew@0
|
88 // mutex helpers
|
andrew@0
|
89 void grabMutex();
|
andrew@0
|
90 void releaseMutex();
|
andrew@0
|
91
|
andrew@0
|
92 #ifdef TARGET_WIN32
|
andrew@0
|
93 // thread to listen with
|
andrew@0
|
94 HANDLE thread;
|
andrew@0
|
95 // mutex for the thread queue
|
andrew@0
|
96 HANDLE mutex;
|
andrew@0
|
97 #else
|
andrew@0
|
98 // thread to listen with
|
andrew@0
|
99 pthread_t thread;
|
andrew@0
|
100 // mutex for the message queue
|
andrew@0
|
101 pthread_mutex_t mutex;
|
andrew@0
|
102 #endif
|
andrew@0
|
103 // ready to be deleted
|
andrew@0
|
104 bool socketHasShutdown;
|
andrew@0
|
105
|
andrew@0
|
106 };
|
andrew@0
|
107
|
andrew@0
|
108 #endif
|