2 Copyright (c) Grame 2009
4 This library is free software; you can redistribute it and modify it under
5 the terms of the GNU Library General Public License as published by the
6 Free Software Foundation version 2 of the License, or any later version.
8 This library is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
13 You should have received a copy of the GNU Library General Public License
14 along with this library; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
23 #include "OSCStream.h"
30 OSCStream
* _oscout
= 0; // OSC standard output stream
31 OSCStream
* _oscerr
= 0; // OSC standard input stream
33 static UdpSocket
* _socket
= 0; // a shared transmit socket
36 //--------------------------------------------------------------------------
37 OSCStream::OSCStream ()
38 : fState(kIdle
), fPort(1024), fAddress(kLocalhost
), fOutStream(fBuffer
, kOutBufferSize
), fSocket(_socket
)
40 if (!fSocket
) cerr
<< "warning: incorrect OSCStream, _socket not initialized" << endl
;
43 //--------------------------------------------------------------------------
44 bool OSCStream::start ()
46 _socket
= new UdpSocket
;
47 _oscout
= new OSCStream(_socket
);
48 _oscerr
= new OSCStream(_socket
);
49 return (_socket
&& _oscout
&& _oscerr
);
52 //--------------------------------------------------------------------------
53 void OSCStream::stop ()
58 _oscout
= _oscerr
= 0;
62 //--------------------------------------------------------------------------
63 void OSCStream::setAddress (const string
& address
)
65 IpEndpointName
dst (address
.c_str());
66 setAddress (dst
.address
);
69 //--------------------------------------------------------------------------
70 OSCStream
& OSCStream::start(const char * address
)
73 if (!stream().IsReady()) cerr
<< "OSCStream OutboundPacketStream not ready" << endl
;
74 stream() << osc::BeginMessage( address
) ;
79 //--------------------------------------------------------------------------
80 OSCStream
& OSCStream::end()
82 if (state() == kInProgress
) {
83 stream() << osc::EndMessage
;
85 fSocket
->SendTo (IpEndpointName (fAddress
, fPort
), stream().Data(), stream().Size() );
91 //--------------------------------------------------------------------------
92 OSCStream
& operator <<(OSCStream
& s
, const string
& val
)
94 s
.stream() << val
.c_str();
98 //--------------------------------------------------------------------------
99 OSCStream
& operator <<(OSCStream
& s
, const OSCErr
& val
) { return s
.start(val
.fAddress
); }
100 OSCStream
& operator <<(OSCStream
& s
, const OSCWarn
& val
) { return s
.start(val
.fAddress
); }
101 OSCStream
& operator <<(OSCStream
& s
, const OSCStart
& val
) { return s
.start(val
.fAddress
); }
102 OSCStream
& operator <<(OSCStream
& s
, const OSCEnd val
) { return s
.end(); }