Initial import.
[Faustine.git] / interpretor / faust-0.9.47mr3 / architecture / osclib / faust / src / osc / OSCStream.h
1 /*
2 Copyright (c) Grame 2009
3
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.
7
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
11 for more details.
12
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.
16
17 Grame Research Laboratory, 9, rue du Garet 69001 Lyon - France
18 research@grame.fr
19
20 */
21
22 #ifndef __OSCStream__
23 #define __OSCStream__
24
25 #include <string>
26 #include <vector>
27
28 #include "osc/OscOutboundPacketStream.h"
29 #include "ip/UdpSocket.h"
30
31 namespace oscfaust
32 {
33
34 //--------------------------------------------------------------------------
35 typedef struct OSCStart {
36 const char* fAddress;
37 OSCStart() {}
38 OSCStart(const char* a) : fAddress(a) {}
39 } OSCStart;
40
41 typedef struct OSCErr : public OSCStart {
42 OSCErr() : OSCStart("error:") {}
43 } OSCErr;
44
45 typedef struct OSCWarn : public OSCStart {
46 OSCWarn() : OSCStart("warning:") {}
47 } OSCWarn;
48
49 typedef struct OSCEnd {} OSCEnd;
50
51 #define kLocalhost 0x7f000001
52
53 //--------------------------------------------------------------------------
54 /*!
55 \brief OSC output streams
56 */
57 class OSCStream
58 {
59 enum { kOutBufferSize = 16384 };
60 enum state { kIdle, kInProgress };
61
62 state fState;
63 int fPort; // the destination UDP port
64 unsigned long fAddress; // the destination IP address
65 char fBuffer[kOutBufferSize];
66
67 osc::OutboundPacketStream fOutStream;
68 UdpSocket* fSocket;
69
70 // void initSocket();
71
72 public:
73 static bool start();
74 static void stop();
75
76 OSCStream();
77 OSCStream(UdpSocket* socket)
78 : fState(kIdle), fPort(1024), fAddress(kLocalhost), fOutStream(fBuffer, kOutBufferSize), fSocket(socket) {}
79 virtual ~OSCStream() {}
80
81 osc::OutboundPacketStream& stream() { return fOutStream; }
82 int getPort () const { return fPort; }
83 unsigned long getAddress () const { return fAddress; }
84 UdpSocket* socket() { return fSocket; }
85 int state() const { return fState; }
86
87 OSCStream& start(const char * address);
88 OSCStream& end();
89
90 void setPort (int port) { fPort = port; }
91 void setAddress (unsigned long address) { fAddress = address; }
92 void setAddress (const std::string& address);
93 };
94
95 OSCStream& operator <<(OSCStream& s, OSCEnd val);
96 OSCStream& operator <<(OSCStream& s, const OSCStart& val);
97 OSCStream& operator <<(OSCStream& s, const OSCErr& val);
98 OSCStream& operator <<(OSCStream& s, const OSCWarn& val);
99 OSCStream& operator <<(OSCStream& s, const std::string& val);
100 template <typename T> OSCStream& operator <<(OSCStream& s, T val) { s.stream() << val; return s; }
101 template <typename T> OSCStream& operator <<(OSCStream& s, const std::vector<T>& val)
102 {
103 for (unsigned int i =0; i < val.size(); i++) s << val[i];
104 return s;
105 }
106
107
108 extern OSCStream* _oscout; // OSC standard output stream
109 extern OSCStream* _oscerr; // OSC standard input stream
110
111 #define oscout (*_oscout)
112 #define oscerr (*_oscerr)
113
114 } // end namespace
115
116 #endif