scide: GenericCodeEditor - accept drag/drop of text/plain
[supercollider.git] / include / common / scsynthsend.h
blob1ae115005dae9e60078542810d247e307cdb53c1
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef _scpacket_
22 #define _scpacket_
24 #include "SC_Endian.h"
25 #include "SC_Types.h"
26 #include <stdexcept>
27 #include <cstring>
28 #include <string>
30 struct netaddr {
31 int socket;
32 int addr;
33 int port;
35 typedef struct netaddr netaddr;
38 template <int MaxPacketSize = 8192>
39 struct scpacket {
40 static const int kBufSize = MaxPacketSize / sizeof(int32); // round down
41 int32 *wrpos, *endpos, *msgsizepos;
42 char *tagwrpos;
43 int inbundle;
44 int32 buf[kBufSize];
46 void throw_overflow_exception()
48 throw std::runtime_error(std::string("buffer overflow"));
51 scpacket() { reset(); }
52 void reset()
54 wrpos = buf;
55 endpos = buf + kBufSize;
56 inbundle = 0;
58 void addi(int i)
60 if (wrpos >= endpos) throw_overflow_exception();
61 *wrpos++ = htonl(i);
63 void addii(int64 ii)
65 int i;
66 i = (int)(ii >> 32);
67 addi(i);
68 i = (int)ii;
69 addi(i);
71 void addf(float f)
73 if (wrpos >= endpos) throw_overflow_exception();
74 elem32 slot;
75 slot.f = f;
76 *wrpos++ = htonl(slot.i);
78 void addd(double f)
80 if (wrpos >= endpos) throw_overflow_exception();
81 elem64 slot;
82 slot.f = f;
83 *wrpos++ = htonl(slot.i >> 32);
84 *wrpos++ = htonl(slot.i & 0x00000000FFFFFFFF);
86 void adds(const char *src)
88 size_t len = strlen(src);
89 size_t len4 = (len + 4) >> 2;
90 if (wrpos + len4 > endpos) throw_overflow_exception();
91 wrpos[len4 - 1] = 0;
92 memcpy(wrpos, src, len);
93 wrpos += len4;
95 void adds_slpre(const char *src) // prepends a slash
97 size_t len = strlen(src);
98 size_t len4 = (len + 5) >> 2;
99 if (wrpos + len4 > endpos) throw_overflow_exception();
100 wrpos[len4 - 1] = 0;
101 char* wrpos_c = (char*)wrpos;
102 *wrpos_c = '/';
103 memcpy(wrpos_c+1, src, len);
104 wrpos += len4;
106 void adds(const char *src, size_t len)
108 size_t len4 = (len + 4) >> 2;
109 if (wrpos + len4 > endpos) throw_overflow_exception();
110 wrpos[len4 - 1] = 0;
111 memcpy(wrpos, src, len);
112 wrpos += len4;
114 void addb(uint8 *src, size_t len)
116 size_t len4 = (len + 3) >> 2;
117 if (wrpos + (len4 + 1) > endpos) throw_overflow_exception();
118 wrpos[len4 - 1] = 0;
119 int32 swaplen = len;
120 *wrpos++ = htonl(swaplen);
121 memcpy(wrpos, src, len);
122 wrpos += len4;
124 void addtag(char c) { *tagwrpos++ = c; }
125 void skip(int n)
127 if (wrpos + n > endpos) throw_overflow_exception();
128 wrpos += n;
130 void maketags(int n)
132 int size4 = (n + 4) >> 2;
133 tagwrpos = (char*)wrpos;
134 skip(size4);
135 wrpos[-1] = 0;
137 size_t size() { return (char*)wrpos - (char*)buf; }
138 char* data() { return (char*)buf; }
140 void OpenBundle(int64 time)
142 inbundle++;
143 adds("#bundle");
144 addii(time);
146 void CloseBundle()
148 if (inbundle) inbundle--;
151 void BeginMsg()
153 if (inbundle) {
154 msgsizepos = wrpos;
155 addi(0);
158 void EndMsg()
160 if (inbundle) {
161 *msgsizepos = htonl(((wrpos - msgsizepos) - 1) * sizeof(int32));
166 typedef scpacket<> small_scpacket;
167 typedef scpacket<65516> big_scpacket;
170 #endif