package rpc import "bytes" type Var struct { Key string Value string } func (v *Var) MarshalBinary() ([]byte, error) { var buf bytes.Buffer buf.WriteString(v.Key) buf.WriteByte(byte(0)) buf.Write(v.Len()[0:4]) buf.WriteString(v.Value) buf.WriteByte(byte(0)) return buf.Bytes(), nil } func (v *Var) UnmarshalBinary(data []byte) error { return nil } func (v *Var) Len() (l []byte) { /* Using a slice instead of [4]byte the wasted 2 bytes are better than the more difficult to read read/write calls, and the extra copy operations that would be needed anyway, which waste 6 bytes at a time */ l = make([]byte, 4) l[0] = byte((len(v.Value) / 0x1) % 0x100) l[1] = byte((len(v.Value) / 0x100) % 0x100) l[2] = byte((len(v.Value) / 0x10000) % 0x100) l[3] = byte((len(v.Value) / 0x1000000) % 0x100) return l }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 10684 | Brett Bates |
Minor changes, lost the decoder.... will rewrite and shelve it properly next time... |
||
#2 | 10670 | Brett Bates | Using a map instead of a vars struct - don't think vars needs any information other than k, v | ||
#1 | 10522 | Brett Bates | Converted the gop4 test file encoding part into an encoder loosely based on the golang gob encoder |