Mercurial > hg > nodescore
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/oscpack/examples/SimpleReceive.cpp Sun Jul 13 10:07:41 2014 +0100 @@ -0,0 +1,86 @@ +/* + Example of two different ways to process received OSC messages using oscpack. + Receives the messages from the SimpleSend.cpp example. +*/ + +#include <iostream> +#include <cstring> + +#if defined(__BORLANDC__) // workaround for BCB4 release build intrinsics bug +namespace std { +using ::__strcmp__; // avoid error: E2316 '__strcmp__' is not a member of 'std'. +} +#endif + +#include "osc/OscReceivedElements.h" +#include "osc/OscPacketListener.h" +#include "ip/UdpSocket.h" + + +#define PORT 7000 + +class ExamplePacketListener : public osc::OscPacketListener { +protected: + + virtual void ProcessMessage( const osc::ReceivedMessage& m, + const IpEndpointName& remoteEndpoint ) + { + (void) remoteEndpoint; // suppress unused parameter warning + + try{ + // example of parsing single messages. osc::OsckPacketListener + // handles the bundle traversal. + + if( std::strcmp( m.AddressPattern(), "/test1" ) == 0 ){ + // example #1 -- argument stream interface + osc::ReceivedMessageArgumentStream args = m.ArgumentStream(); + bool a1; + osc::int32 a2; + float a3; + const char *a4; + args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage; + + std::cout << "received '/test1' message with arguments: " + << a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; + + }else if( std::strcmp( m.AddressPattern(), "/test2" ) == 0 ){ + // example #2 -- argument iterator interface, supports + // reflection for overloaded messages (eg you can call + // (*arg)->IsBool() to check if a bool was passed etc). + osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin(); + bool a1 = (arg++)->AsBool(); + int a2 = (arg++)->AsInt32(); + float a3 = (arg++)->AsFloat(); + const char *a4 = (arg++)->AsString(); + if( arg != m.ArgumentsEnd() ) + throw osc::ExcessArgumentException(); + + std::cout << "received '/test2' message with arguments: " + << a1 << " " << a2 << " " << a3 << " " << a4 << "\n"; + } + }catch( osc::Exception& e ){ + // any parsing errors such as unexpected argument types, or + // missing arguments get thrown as exceptions. + std::cout << "error while parsing message: " + << m.AddressPattern() << ": " << e.what() << "\n"; + } + } +}; + +int main(int argc, char* argv[]) +{ + (void) argc; // suppress unused parameter warnings + (void) argv; // suppress unused parameter warnings + + ExamplePacketListener listener; + UdpListeningReceiveSocket s( + IpEndpointName( IpEndpointName::ANY_ADDRESS, PORT ), + &listener ); + + std::cout << "press ctrl-c to end\n"; + + s.RunUntilSigInt(); + + return 0; +} +