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"
34 typedef struct netaddr netaddr
;
37 template <int MaxPacketSize
= 8192>
39 static const int kBufSize
= MaxPacketSize
/ sizeof(int32
); // round down
40 int32
*wrpos
, *endpos
, *msgsizepos
;
45 void throw_overflow_exception()
47 throw std::runtime_error("buffer overflow");
50 scpacket() { reset(); }
54 endpos
= buf
+ kBufSize
;
59 if (wrpos
>= endpos
) throw_overflow_exception();
72 if (wrpos
>= endpos
) throw_overflow_exception();
75 *wrpos
++ = htonl(slot
.i
);
79 if (wrpos
>= endpos
) throw_overflow_exception();
82 *wrpos
++ = htonl(slot
.i
>> 32);
83 *wrpos
++ = htonl(slot
.i
& 0x00000000FFFFFFFF);
85 void adds(const char *src
)
87 size_t len
= strlen(src
);
88 size_t len4
= (len
+ 4) >> 2;
89 if (wrpos
+ len4
> endpos
) throw_overflow_exception();
91 memcpy(wrpos
, src
, len
);
94 void adds_slpre(const char *src
) // prepends a slash
96 size_t len
= strlen(src
);
97 size_t len4
= (len
+ 5) >> 2;
98 if (wrpos
+ len4
> endpos
) throw_overflow_exception();
100 char* wrpos_c
= (char*)wrpos
;
102 memcpy(wrpos_c
+1, src
, len
);
105 void adds(const char *src
, size_t len
)
107 size_t len4
= (len
+ 4) >> 2;
108 if (wrpos
+ len4
> endpos
) throw_overflow_exception();
110 memcpy(wrpos
, src
, len
);
113 void addb(uint8
*src
, size_t len
)
115 size_t len4
= (len
+ 3) >> 2;
116 if (wrpos
+ (len4
+ 1) > endpos
) throw_overflow_exception();
119 *wrpos
++ = htonl(swaplen
);
120 memcpy(wrpos
, src
, len
);
123 void addtag(char c
) { *tagwrpos
++ = c
; }
126 if (wrpos
+ n
> endpos
) throw_overflow_exception();
131 int size4
= (n
+ 4) >> 2;
132 tagwrpos
= (char*)wrpos
;
136 int size() { return (char*)wrpos
- (char*)buf
; }
137 char* data() { return (char*)buf
; }
139 void OpenBundle(int64 time
)
147 if (inbundle
) inbundle
--;
160 *msgsizepos
= htonl(((wrpos
- msgsizepos
) - 1) * sizeof(int32
));
165 typedef scpacket
<> small_scpacket
;
166 typedef scpacket
<65516> big_scpacket
;