andrew@0: /* andrew@0: andrew@0: Copyright (c) 2007-2009, Damian Stewart andrew@0: All rights reserved. andrew@0: andrew@0: Redistribution and use in source and binary forms, with or without andrew@0: modification, are permitted provided that the following conditions are met: andrew@0: * Redistributions of source code must retain the above copyright andrew@0: notice, this list of conditions and the following disclaimer. andrew@0: * Redistributions in binary form must reproduce the above copyright andrew@0: notice, this list of conditions and the following disclaimer in the andrew@0: documentation and/or other materials provided with the distribution. andrew@0: * Neither the name of the developer nor the andrew@0: names of its contributors may be used to endorse or promote products andrew@0: derived from this software without specific prior written permission. andrew@0: andrew@0: THIS SOFTWARE IS PROVIDED BY DAMIAN STEWART ''AS IS'' AND ANY andrew@0: EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED andrew@0: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE andrew@0: DISCLAIMED. IN NO EVENT SHALL DAMIAN STEWART BE LIABLE FOR ANY andrew@0: DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES andrew@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; andrew@0: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND andrew@0: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT andrew@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS andrew@0: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. andrew@0: */ andrew@0: andrew@0: andrew@0: #include "ofxOscSender.h" andrew@0: andrew@0: andrew@0: #include "UdpSocket.h" andrew@0: andrew@0: #include andrew@0: andrew@0: ofxOscSender::ofxOscSender() andrew@0: { andrew@0: socket = NULL; andrew@0: } andrew@0: andrew@0: ofxOscSender::~ofxOscSender() andrew@0: { andrew@0: if ( socket ) andrew@0: shutdown(); andrew@0: } andrew@0: andrew@0: void ofxOscSender::setup( std::string hostname, int port ) andrew@0: { andrew@0: if ( socket ) andrew@0: shutdown(); andrew@0: andrew@0: socket = new UdpTransmitSocket( IpEndpointName( hostname.c_str(), port ) ); andrew@0: } andrew@0: andrew@0: void ofxOscSender::shutdown() andrew@0: { andrew@0: if ( socket ) andrew@0: delete socket; andrew@0: socket = NULL; andrew@0: } andrew@0: andrew@0: void ofxOscSender::sendBundle( ofxOscBundle& bundle ) andrew@0: { andrew@0: static const int OUTPUT_BUFFER_SIZE = 32768; andrew@0: char buffer[OUTPUT_BUFFER_SIZE]; andrew@0: osc::OutboundPacketStream p(buffer, OUTPUT_BUFFER_SIZE ); andrew@0: andrew@0: // serialise the bundle andrew@0: appendBundle( bundle, p ); andrew@0: andrew@0: socket->Send( p.Data(), p.Size() ); andrew@0: } andrew@0: andrew@0: void ofxOscSender::sendMessage( ofxOscMessage& message ) andrew@0: { andrew@0: static const int OUTPUT_BUFFER_SIZE = 16384; andrew@0: char buffer[OUTPUT_BUFFER_SIZE]; andrew@0: osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE ); andrew@0: andrew@0: // serialise the message andrew@0: p << osc::BeginBundleImmediate; andrew@0: appendMessage( message, p ); andrew@0: p << osc::EndBundle; andrew@0: andrew@0: socket->Send( p.Data(), p.Size() ); andrew@0: } andrew@0: andrew@0: void ofxOscSender::appendBundle( ofxOscBundle& bundle, osc::OutboundPacketStream& p ) andrew@0: { andrew@0: // recursively serialise the bundle andrew@0: p << osc::BeginBundleImmediate; andrew@0: for ( int i=0; i