| 52 | } |
| 53 | |
| 54 | func (f *Frame) AppendMarshal(dst []byte) ([]byte, error) { |
| 55 | if len(f.Target) > maxTargetLen { |
| 56 | return nil, fmt.Errorf("target too long: %d > %d", len(f.Target), maxTargetLen) |
| 57 | } |
| 58 | if len(f.Payload) > maxPayloadSize { |
| 59 | return nil, fmt.Errorf("payload too large: %d", len(f.Payload)) |
| 60 | } |
| 61 | size := f.EncodedLen() |
| 62 | base := len(dst) |
| 63 | if cap(dst)-base < size { |
| 64 | next := make([]byte, base, base+size) |
| 65 | copy(next, dst) |
| 66 | dst = next |
| 67 | } |
| 68 | dst = dst[:base+size] |
| 69 | out := dst[base:] |
| 70 | off := 0 |
| 71 | copy(out[off:off+SessionIDLen], f.SessionID[:]) |
| 72 | off += SessionIDLen |
| 73 | binary.BigEndian.PutUint64(out[off:off+8], f.Seq) |
| 74 | off += 8 |
| 75 | out[off] = f.Flags |
| 76 | off++ |
| 77 | out[off] = uint8(len(f.Target)) |
| 78 | off++ |
| 79 | copy(out[off:off+len(f.Target)], f.Target) |
| 80 | off += len(f.Target) |
| 81 | binary.BigEndian.PutUint32(out[off:off+4], uint32(len(f.Payload))) |
| 82 | off += 4 |
| 83 | copy(out[off:off+len(f.Payload)], f.Payload) |
| 84 | return dst, nil |
| 85 | } |
| 86 | |
| 87 | // Unmarshal parses a frame produced by Marshal. Returns the number of bytes |
| 88 | // consumed (which equals len(data) for a well-formed single frame). |