root/oscpack/trunk/examples/SimpleReceive.cpp

Revision 46, 2.6 kB (checked in by ross, 3 years ago)

added examples/SimpleReceive.cpp

  • Property svn:eol-style set to native
Line 
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
8 #include "osc/OscReceivedElements.h"
9 #include "osc/OscPacketListener.h"
10 #include "ip/UdpSocket.h"
11
12
13 #define PORT 7000
14
15 class ExamplePacketListener : public osc::OscPacketListener {
16 protected:
17
18     virtual void ProcessMessage( const osc::ReceivedMessage& m,
19                                 const IpEndpointName& remoteEndpoint )
20     {
21         try{
22             // example of parsing single messages. osc::OsckPacketListener
23             // handles the bundle traversal.
24             
25             if( strcmp( m.AddressPattern(), "/test1" ) == 0 ){
26                 // example #1 -- argument stream interface
27                 osc::ReceivedMessageArgumentStream args = m.ArgumentStream();
28                 bool a1;
29                 osc::int32 a2;
30                 float a3;
31                 const char *a4;
32                 args >> a1 >> a2 >> a3 >> a4 >> osc::EndMessage;
33                
34                 std::cout << "received '/test1' message with arguments: "
35                     << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
36                
37             }else if( strcmp( m.AddressPattern(), "/test2" ) == 0 ){
38                 // example #2 -- argument iterator interface, supports
39                 // reflection for overloaded messages (eg you can call
40                 // (*arg)->IsBool() to check if a bool was passed etc).
41                 osc::ReceivedMessage::const_iterator arg = m.ArgumentsBegin();
42                 bool a1 = (arg++)->AsBool();
43                 int a2 = (arg++)->AsInt32();
44                 float a3 = (arg++)->AsFloat();
45                 const char *a4 = (arg++)->AsString();
46                 if( arg != m.ArgumentsEnd() )
47                     throw osc::ExcessArgumentException();
48                
49                 std::cout << "received '/test2' message with arguments: "
50                     << a1 << " " << a2 << " " << a3 << " " << a4 << "\n";
51             }
52         }catch( osc::Exception& e ){
53             // any parsing errors such as unexpected argument types, or
54             // missing arguments get thrown as exceptions.
55             std::cout << "error while parsing message: "
56                 << m.AddressPattern() << ": " << e.what() << "\n";
57         }
58     }
59 };
60
61 int main(int argc, char* argv[])
62 {
63     ExamplePacketListener listener;
64     UdpListeningReceiveSocket s(
65             IpEndpointName( IpEndpointName::ANY_ADDRESS, PORT ),
66             &listener );
67
68     std::cout << "press ctrl-c to end\n";
69
70     s.RunUntilSigInt();
71
72     return 0;
73 }
74
Note: See TracBrowser for help on using the browser.