andrew@0
|
1 /*
|
andrew@0
|
2
|
andrew@0
|
3 Copyright (c) 2007-2009, Damian Stewart
|
andrew@0
|
4 All rights reserved.
|
andrew@0
|
5
|
andrew@0
|
6 Redistribution and use in source and binary forms, with or without
|
andrew@0
|
7 modification, are permitted provided that the following conditions are met:
|
andrew@0
|
8 * Redistributions of source code must retain the above copyright
|
andrew@0
|
9 notice, this list of conditions and the following disclaimer.
|
andrew@0
|
10 * Redistributions in binary form must reproduce the above copyright
|
andrew@0
|
11 notice, this list of conditions and the following disclaimer in the
|
andrew@0
|
12 documentation and/or other materials provided with the distribution.
|
andrew@0
|
13 * Neither the name of the developer nor the
|
andrew@0
|
14 names of its contributors may be used to endorse or promote products
|
andrew@0
|
15 derived from this software without specific prior written permission.
|
andrew@0
|
16
|
andrew@0
|
17 THIS SOFTWARE IS PROVIDED BY DAMIAN STEWART ''AS IS'' AND ANY
|
andrew@0
|
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
andrew@0
|
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
andrew@0
|
20 DISCLAIMED. IN NO EVENT SHALL DAMIAN STEWART BE LIABLE FOR ANY
|
andrew@0
|
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
andrew@0
|
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
andrew@0
|
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
andrew@0
|
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
andrew@0
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
andrew@0
|
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
andrew@0
|
27 */
|
andrew@0
|
28
|
andrew@0
|
29
|
andrew@0
|
30 #include "ofxOscSender.h"
|
andrew@0
|
31
|
andrew@0
|
32
|
andrew@0
|
33 #include "UdpSocket.h"
|
andrew@0
|
34
|
andrew@0
|
35 #include <assert.h>
|
andrew@0
|
36
|
andrew@0
|
37 ofxOscSender::ofxOscSender()
|
andrew@0
|
38 {
|
andrew@0
|
39 socket = NULL;
|
andrew@0
|
40 }
|
andrew@0
|
41
|
andrew@0
|
42 ofxOscSender::~ofxOscSender()
|
andrew@0
|
43 {
|
andrew@0
|
44 if ( socket )
|
andrew@0
|
45 shutdown();
|
andrew@0
|
46 }
|
andrew@0
|
47
|
andrew@0
|
48 void ofxOscSender::setup( std::string hostname, int port )
|
andrew@0
|
49 {
|
andrew@0
|
50 if ( socket )
|
andrew@0
|
51 shutdown();
|
andrew@0
|
52
|
andrew@0
|
53 socket = new UdpTransmitSocket( IpEndpointName( hostname.c_str(), port ) );
|
andrew@0
|
54 }
|
andrew@0
|
55
|
andrew@0
|
56 void ofxOscSender::shutdown()
|
andrew@0
|
57 {
|
andrew@0
|
58 if ( socket )
|
andrew@0
|
59 delete socket;
|
andrew@0
|
60 socket = NULL;
|
andrew@0
|
61 }
|
andrew@0
|
62
|
andrew@0
|
63 void ofxOscSender::sendBundle( ofxOscBundle& bundle )
|
andrew@0
|
64 {
|
andrew@0
|
65 static const int OUTPUT_BUFFER_SIZE = 32768;
|
andrew@0
|
66 char buffer[OUTPUT_BUFFER_SIZE];
|
andrew@0
|
67 osc::OutboundPacketStream p(buffer, OUTPUT_BUFFER_SIZE );
|
andrew@0
|
68
|
andrew@0
|
69 // serialise the bundle
|
andrew@0
|
70 appendBundle( bundle, p );
|
andrew@0
|
71
|
andrew@0
|
72 socket->Send( p.Data(), p.Size() );
|
andrew@0
|
73 }
|
andrew@0
|
74
|
andrew@0
|
75 void ofxOscSender::sendMessage( ofxOscMessage& message )
|
andrew@0
|
76 {
|
andrew@0
|
77 static const int OUTPUT_BUFFER_SIZE = 16384;
|
andrew@0
|
78 char buffer[OUTPUT_BUFFER_SIZE];
|
andrew@0
|
79 osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE );
|
andrew@0
|
80
|
andrew@0
|
81 // serialise the message
|
andrew@0
|
82 p << osc::BeginBundleImmediate;
|
andrew@0
|
83 appendMessage( message, p );
|
andrew@0
|
84 p << osc::EndBundle;
|
andrew@0
|
85
|
andrew@0
|
86 socket->Send( p.Data(), p.Size() );
|
andrew@0
|
87 }
|
andrew@0
|
88
|
andrew@0
|
89 void ofxOscSender::appendBundle( ofxOscBundle& bundle, osc::OutboundPacketStream& p )
|
andrew@0
|
90 {
|
andrew@0
|
91 // recursively serialise the bundle
|
andrew@0
|
92 p << osc::BeginBundleImmediate;
|
andrew@0
|
93 for ( int i=0; i<bundle.getBundleCount(); i++ )
|
andrew@0
|
94 {
|
andrew@0
|
95 appendBundle( bundle.getBundleAt( i ), p );
|
andrew@0
|
96 }
|
andrew@0
|
97 for ( int i=0; i<bundle.getMessageCount(); i++ )
|
andrew@0
|
98 {
|
andrew@0
|
99 appendMessage( bundle.getMessageAt( i ), p );
|
andrew@0
|
100 }
|
andrew@0
|
101 p << osc::EndBundle;
|
andrew@0
|
102 }
|
andrew@0
|
103
|
andrew@0
|
104 void ofxOscSender::appendMessage( ofxOscMessage& message, osc::OutboundPacketStream& p )
|
andrew@0
|
105 {
|
andrew@0
|
106 p << osc::BeginMessage( message.getAddress().c_str() );
|
andrew@0
|
107 for ( int i=0; i< message.getNumArgs(); ++i )
|
andrew@0
|
108 {
|
andrew@0
|
109 if ( message.getArgType(i) == OFXOSC_TYPE_INT32 )
|
andrew@0
|
110 p << message.getArgAsInt32( i );
|
andrew@0
|
111 else if ( message.getArgType( i ) == OFXOSC_TYPE_FLOAT )
|
andrew@0
|
112 p << message.getArgAsFloat( i );
|
andrew@0
|
113 else if ( message.getArgType( i ) == OFXOSC_TYPE_STRING )
|
andrew@0
|
114 p << message.getArgAsString( i ).c_str();
|
andrew@0
|
115 else
|
andrew@0
|
116 {
|
andrew@0
|
117 assert( false && "bad argument type" );
|
andrew@0
|
118 }
|
andrew@0
|
119 }
|
andrew@0
|
120 p << osc::EndMessage;
|
andrew@0
|
121 }
|