header.go 866 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import "github.com/calmh/xdr"
  4. type header struct {
  5. version int
  6. msgID int
  7. msgType int
  8. compression bool
  9. }
  10. func (h header) MarshalXDRInto(m *xdr.Marshaller) error {
  11. v := encodeHeader(h)
  12. m.MarshalUint32(v)
  13. return m.Error
  14. }
  15. func (h *header) UnmarshalXDRFrom(u *xdr.Unmarshaller) error {
  16. v := u.UnmarshalUint32()
  17. *h = decodeHeader(v)
  18. return u.Error
  19. }
  20. func encodeHeader(h header) uint32 {
  21. var isComp uint32
  22. if h.compression {
  23. isComp = 1 << 0 // the zeroth bit is the compression bit
  24. }
  25. return uint32(h.version&0xf)<<28 +
  26. uint32(h.msgID&0xfff)<<16 +
  27. uint32(h.msgType&0xff)<<8 +
  28. isComp
  29. }
  30. func decodeHeader(u uint32) header {
  31. return header{
  32. version: int(u>>28) & 0xf,
  33. msgID: int(u>>16) & 0xfff,
  34. msgType: int(u>>8) & 0xff,
  35. compression: u&1 == 1,
  36. }
  37. }