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
24 #include "SC_Endian.h"
35 typedef struct netaddr netaddr
;
38 template <int MaxPacketSize
= 8192>
40 static const int kBufSize
= MaxPacketSize
/ sizeof(int32
); // round down
41 int32
*wrpos
, *endpos
, *msgsizepos
;
46 void throw_overflow_exception()
48 throw std::runtime_error(std::string("buffer overflow"));
51 scpacket() { reset(); }
55 endpos
= buf
+ kBufSize
;
60 if (wrpos
>= endpos
) throw_overflow_exception();
73 if (wrpos
>= endpos
) throw_overflow_exception();
76 *wrpos
++ = htonl(slot
.i
);
80 if (wrpos
>= endpos
) throw_overflow_exception();
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();
92 memcpy(wrpos
, src
, len
);
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();
101 char* wrpos_c
= (char*)wrpos
;
103 memcpy(wrpos_c
+1, src
, len
);
106 void adds(const char *src
, size_t len
)
108 size_t len4
= (len
+ 4) >> 2;
109 if (wrpos
+ len4
> endpos
) throw_overflow_exception();
111 memcpy(wrpos
, src
, len
);
114 void addb(uint8
*src
, size_t len
)
116 size_t len4
= (len
+ 3) >> 2;
117 if (wrpos
+ (len4
+ 1) > endpos
) throw_overflow_exception();
120 *wrpos
++ = htonl(swaplen
);
121 memcpy(wrpos
, src
, len
);
124 void addtag(char c
) { *tagwrpos
++ = c
; }
127 if (wrpos
+ n
> endpos
) throw_overflow_exception();
132 int size4
= (n
+ 4) >> 2;
133 tagwrpos
= (char*)wrpos
;
137 int size() { return (char*)wrpos
- (char*)buf
; }
138 char* data() { return (char*)buf
; }
140 void OpenBundle(int64 time
)
148 if (inbundle
) inbundle
--;
161 *msgsizepos
= htonl(((wrpos
- msgsizepos
) - 1) * sizeof(int32
));
166 typedef scpacket
<> small_scpacket
;
167 typedef scpacket
<65516> big_scpacket
;