Пример #1
0
func (this *Packet) Encode() ([]byte, error) {
	buf := make([]byte, define.PACKET_HEAD_LEN+len(this.Data))
	buf[0] = byte(this.Number)
	uuid, err := utils.StringToUuid(this.Uuid)
	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}
	copy(buf[1:1+define.UUID_BYTE_LEN], uuid)
	binary.BigEndian.PutUint16(buf[1+define.UUID_BYTE_LEN:3+define.UUID_BYTE_LEN], this.DataLen)
	copy(buf[3+define.UUID_BYTE_LEN:], this.Data)
	return buf, nil
}
Пример #2
0
func (this *Head) Encode() ([]byte, error) {
	//PACKET_HEAD_LEN为19
	buf := make([]byte, define.PACKET_HEAD_LEN)
	buf[0] = byte(this.Number)
	//uuid 16位
	uuid, err := utils.StringToUuid(this.Uuid)
	if err != nil {
		fmt.Println(err.Error())
		return nil, err
	}
	//UUID_BYTE_LEN为16
	copy(buf[1:1+define.UUID_BYTE_LEN], uuid)
	binary.BigEndian.PutUint16(buf[1+define.UUID_BYTE_LEN:3+define.UUID_BYTE_LEN], this.DataLen)
	return buf, nil
}