… | |
… | |
266 | |
266 | |
267 | /* Contains the base information we use to make up a packet we want to send. */ |
267 | /* Contains the base information we use to make up a packet we want to send. */ |
268 | struct packet |
268 | struct packet |
269 | { |
269 | { |
270 | enum { bufsize = MAXSOCKBUF }; |
270 | enum { bufsize = MAXSOCKBUF }; |
|
|
271 | enum { hdrlen = 2 }; // keep this many bytes free at the front |
271 | |
272 | |
272 | // strategy: only check on variable-length adds |
273 | // strategy: only check on variable-length adds |
273 | // and allow 256 bytes for any headers. |
274 | // and allow 256 bytes for any headers. |
274 | uint8 *cur; |
275 | uint8 *cur; |
275 | uint8 buf [bufsize + 256]; |
276 | uint8 buf_ [hdrlen + bufsize + 256]; |
276 | |
277 | |
277 | packet () { reset (); } |
278 | packet () { reset (); } |
278 | |
279 | |
|
|
280 | uint8 operator [](unsigned int i) const { return buf_ [i + hdrlen]; } |
|
|
281 | uint8 &operator [](unsigned int i) { return buf_ [i + hdrlen]; } |
|
|
282 | |
|
|
283 | uint8 *buf () { return buf_ + hdrlen; } |
|
|
284 | const uint8 *buf () const { return buf_ + hdrlen; } |
|
|
285 | |
279 | void reset (int offset = 0) { cur = buf + offset; } |
286 | void reset (int offset = 0) { cur = buf () + offset; } |
280 | int length () const { return cur - buf; } |
287 | int length () const { return cur - buf (); } |
281 | int room () { return bufsize - (cur - buf); } |
288 | int room () { return bufsize - (cur - buf ()); } |
282 | |
289 | |
283 | // unchecked |
290 | // unchecked |
284 | packet &operator <<(uint8 v) { *cur++ = v; return *this; } |
291 | packet &operator <<(uint8 v) { *cur++ = v; return *this; } |
285 | |
292 | |
286 | packet &operator <<(uint16 v) { return *this << uint8 (v >> 8) << uint8 (v); } |
293 | packet &operator <<(uint16 v) { return *this << uint8 (v >> 8) << uint8 (v); } |