/ dnet / handler.go
handler.go
 1  package dnet
 2  
 3  import "encoding/binary"
 4  
 5  const BindMessageSize = 4 + 4 + 32
 6  
 7  type BindMessage struct {
 8  	Version uint32
 9  	Chan    Tag4CC
10  	PubKey  [32]byte
11  }
12  
13  func (msg BindMessage) Encode() []byte {
14  	buf := make([]byte, BindMessageSize)
15  	binary.LittleEndian.PutUint32(buf[0:4], msg.Version)
16  	binary.BigEndian.PutUint32(buf[4:8], uint32(msg.Chan))
17  	copy(buf[8:40], msg.PubKey[:])
18  	return buf
19  }
20  
21  func DecodeBindMessage(payload []byte) (msg BindMessage, ok bool) {
22  	if len(payload) == BindMessageSize {
23  		msg.Version = binary.LittleEndian.Uint32(payload[0:4])
24  		msg.Chan = Tag4CC(binary.BigEndian.Uint32(payload[4:8]))
25  		copy(msg.PubKey[:], payload[8:40])
26  		return msg, true
27  	}
28  	return BindMessage{}, false
29  }