github.com/kayoticsully/syncthing@v0.8.9-0.20140724133906-c45a2fdc03f8/protocol/header.go (about)

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