- memcpy(ring.buf + ring.wptr, buf, head);
- memcpy(ring.buf, buf + head, tail);
- ring.wptr = tail;
+ if ((int)header.length() <= head) {
+ memcpy(ring.buf + ring.wptr, header.data(), header.length());
+ head -= header.length();
+ if (head > 0) {
+ memcpy(ring.buf + ring.wptr + header.length(), payload.data(), head);
+ }
+ memcpy(ring.buf, payload.data() + head, tail);
+ } else {
+ memcpy(ring.buf + ring.wptr, header.data(), head);
+ int part = header.length() - head;
+ memcpy(ring.buf, header.data() + head, part);
+ if (tail > part) {
+ memcpy(ring.buf + part, payload.data(), tail - part);
+ }
+ }
+ *(ring.buf + tail) = '\0';
+ ring.wptr = tail + 1;