header.go 812 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. package protocol
  5. import "github.com/calmh/syncthing/xdr"
  6. type header struct {
  7. version int
  8. msgID int
  9. msgType int
  10. }
  11. func (h header) encodeXDR(xw *xdr.Writer) (int, error) {
  12. u := encodeHeader(h)
  13. return xw.WriteUint32(u)
  14. }
  15. func (h *header) decodeXDR(xr *xdr.Reader) error {
  16. u := xr.ReadUint32()
  17. *h = decodeHeader(u)
  18. return xr.Error()
  19. }
  20. func encodeHeader(h header) uint32 {
  21. return uint32(h.version&0xf)<<28 +
  22. uint32(h.msgID&0xfff)<<16 +
  23. uint32(h.msgType&0xff)<<8
  24. }
  25. func decodeHeader(u uint32) header {
  26. return header{
  27. version: int(u>>28) & 0xf,
  28. msgID: int(u>>16) & 0xfff,
  29. msgType: int(u>>8) & 0xff,
  30. }
  31. }