vector_xdr.go 941 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (C) 2015 The Protocol Authors.
  2. package protocol
  3. // This stuff is hacked up manually because genxdr doesn't support 'type
  4. // Vector []Counter' declarations and it was tricky when I tried to add it...
  5. type xdrWriter interface {
  6. WriteUint32(uint32) (int, error)
  7. WriteUint64(uint64) (int, error)
  8. }
  9. type xdrReader interface {
  10. ReadUint32() uint32
  11. ReadUint64() uint64
  12. }
  13. // EncodeXDRInto encodes the vector as an XDR object into the given XDR
  14. // encoder.
  15. func (v Vector) EncodeXDRInto(w xdrWriter) (int, error) {
  16. w.WriteUint32(uint32(len(v)))
  17. for i := range v {
  18. w.WriteUint64(v[i].ID)
  19. w.WriteUint64(v[i].Value)
  20. }
  21. return 4 + 16*len(v), nil
  22. }
  23. // DecodeXDRFrom decodes the XDR objects from the given reader into itself.
  24. func (v *Vector) DecodeXDRFrom(r xdrReader) error {
  25. l := int(r.ReadUint32())
  26. n := make(Vector, l)
  27. for i := range n {
  28. n[i].ID = r.ReadUint64()
  29. n[i].Value = r.ReadUint64()
  30. }
  31. *v = n
  32. return nil
  33. }