annotate oscpack/examples/SimpleReceive.cpp @ 101:52e44ee1c791 tip master

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