github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/gp/packet.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package gp 4 5 import "../protocol" 6 7 const ( 8 // MinPacketLen is minimum packet length of GP packet 9 // 320bit header + 128bit min payload 10 MinPacketLen = 56 11 ) 12 13 type packet []byte 14 15 // CheckPacket will check packet for any bad situation! 16 // Always check packet before use any other packet methods otherwise panic occur! 17 func CheckPacket(p []byte) protocol.Error { 18 if len(p) < MinPacketLen { 19 return ErrPacketTooShort 20 } 21 return nil 22 } 23 24 // GetDestinationAddr returns full destination GP address. 25 func GetDestinationAddr(p []byte) (addr Addr) { 26 copy(addr[:], p[:]) 27 return 28 } 29 30 // GetSourceAddr returns full source GP address. 31 func GetSourceAddr(p []byte) (addr Addr) { 32 copy(addr[:], p[16:]) 33 return 34 } 35 36 // GetPacketNumber returns packet number 37 func GetPacketNumber(p []byte) uint64 { 38 return uint64(p[32]) | uint64(p[33])<<8 | uint64(p[34]<<16) | uint64(p[35])<<24 | uint64(p[36]<<32) | uint64(p[37])<<40 | uint64(p[38]<<48) | uint64(p[39])<<56 39 } 40 41 // GetPayload returns payload that means all data after packetNumber 42 func GetPayload(p []byte) []byte { 43 return p[40:] 44 }