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