Mercurial > hg > nodescore
comparison oscpack/examples/SimpleReceive.cpp @ 76:0ae87af84e2f
added oscgroups
author | Rob Canning <rob@foo.net> |
---|---|
date | Sun, 13 Jul 2014 10:07:41 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
75:3a2845e3156e | 76:0ae87af84e2f |
---|---|
1 /* | |
2 Example of two different ways to process received OSC messages using oscpack. | |
3 Receives the messages from the SimpleSend.cpp example. | |
4 */ | |
5 | |
6 #include <iostream> | |
7 #include <cstring> | |
8 | |
9 #if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug | |
10 namespace std { | |
11 using ::__strcmp__; // avoid error: E2316 '__strcmp__' is not a member of 'std'. | |
12 } | |
13 #endif | |
14 | |
15 #include "osc/OscReceivedElements.h" | |
16 #include "osc/OscPacketListener.h" | |
17 #include "ip/UdpSocket.h" | |
18 | |
19 | |
20 #define PORT 7000 | |
21 | |
22 class ExamplePacketListener : public osc::OscPacketListener { | |
23 protected: | |
24 | |
25 virtual void ProcessMessage( const osc::ReceivedMessage& m, | |
26 const IpEndpointName& remoteEndpoint ) | |
27 { | |
28 (void) remoteEndpoint; // suppress unused parameter warning | |
29 | |
30 try{ | |
31 // example of parsing single messages. osc::OsckPacketListener | |
32 // handles the bundle traversal. | |
33 | |
34 if( std::strcmp( m.AddressPattern(), "/test1" ) == 0 ){ | |
35 // example #1 -- argument stream interface | |
36 osc::ReceivedMessageArgumentStream args = m.ArgumentStream(); | |
37 bool a1; | |
38 osc::int32 a2; | |
39 float a3; | |
40 const char *a4; | |
41 args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage; | |
42 | |
43 std::cout << "received '/test1' message with arguments: " | |
44 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; | |
45 | |
46 }else if( std::strcmp( m.AddressPattern(), "/test2" ) == 0 ){ | |
47 // example #2 -- argument iterator interface, supports | |
48 // reflection for overloaded messages (eg you can call | |
49 // (*arg)->IsBool() to check if a bool was passed etc). | |
50 osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); | |
51 bool a1 = (arg++)->AsBool(); | |
52 int a2 = (arg++)->AsInt32(); | |
53 float a3 = (arg++)->AsFloat(); | |
54 const char *a4 = (arg++)->AsString(); | |
55 if( arg != m.ArgumentsEnd() ) | |
56 throw osc::ExcessArgumentException(); | |
57 | |
58 std::cout << "received '/test2' message with arguments: " | |
59 << a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; | |
60 } | |
61 }catch( osc::Exception& e ){ | |
62 // any parsing errors such as unexpected argument types, or | |
63 // missing arguments get thrown as exceptions. | |
64 std::cout << "error while parsing message: " | |
65 << m.AddressPattern() << ": " << e.what() << "\n"; | |
66 } | |
67 } | |
68 }; | |
69 | |
70 int main(int argc, char* argv[]) | |
71 { | |
72 (void) argc; // suppress unused parameter warnings | |
73 (void) argv; // suppress unused parameter warnings | |
74 | |
75 ExamplePacketListener listener; | |
76 UdpListeningReceiveSocket s( | |
77 IpEndpointName( IpEndpointName::ANY_ADDRESS, PORT ), | |
78 &listener ); | |
79 | |
80 std::cout << "press ctrl-c to end\n"; | |
81 | |
82 s.RunUntilSigInt(); | |
83 | |
84 return 0; | |
85 } | |
86 |