2 Example of two different ways to process received OSC messages using oscpack.
3 Receives the messages from the SimpleSend.cpp example.
9 #include "osc/OscReceivedElements.h"
10 #include "osc/OscPacketListener.h"
11 #include "ip/UdpSocket.h"
16 class ExamplePacketListener
: public osc::OscPacketListener
{
19 virtual void ProcessMessage( const osc::ReceivedMessage
& m
,
20 const IpEndpointName
& remoteEndpoint
)
23 // example of parsing single messages. osc::OsckPacketListener
24 // handles the bundle traversal.
26 if( strcmp( m
.AddressPattern(), "/test1" ) == 0 ){
27 // example #1 -- argument stream interface
28 osc::ReceivedMessageArgumentStream args
= m
.ArgumentStream();
33 args
>> a1
>> a2
>> a3
>> a4
>> osc::EndMessage
;
35 std::cout
<< "received '/test1' message with arguments: "
36 << a1
<< " " << a2
<< " " << a3
<< " " << a4
<< "\n";
38 }else if( strcmp( m
.AddressPattern(), "/test2" ) == 0 ){
39 // example #2 -- argument iterator interface, supports
40 // reflection for overloaded messages (eg you can call
41 // (*arg)->IsBool() to check if a bool was passed etc).
42 osc::ReceivedMessage::const_iterator arg
= m
.ArgumentsBegin();
43 bool a1
= (arg
++)->AsBool();
44 int a2
= (arg
++)->AsInt32();
45 float a3
= (arg
++)->AsFloat();
46 const char *a4
= (arg
++)->AsString();
47 if( arg
!= m
.ArgumentsEnd() )
48 throw osc::ExcessArgumentException();
50 std::cout
<< "received '/test2' message with arguments: "
51 << a1
<< " " << a2
<< " " << a3
<< " " << a4
<< "\n";
53 }catch( osc::Exception
& e
){
54 // any parsing errors such as unexpected argument types, or
55 // missing arguments get thrown as exceptions.
56 std::cout
<< "error while parsing message: "
57 << m
.AddressPattern() << ": " << e
.what() << "\n";
62 int main(int argc
, char* argv
[])
64 ExamplePacketListener listener
;
65 UdpListeningReceiveSocket
s(
66 IpEndpointName( IpEndpointName::ANY_ADDRESS
, PORT
),
69 std::cout
<< "press ctrl-c to end\n";