mirror of
https://github.com/fhmq/hmq.git
synced 2026-04-27 03:58:33 +00:00
110 lines
2.4 KiB
Go
Executable File
110 lines
2.4 KiB
Go
Executable File
// Copyright (c) 2014 The SurgeMQ Authors. All rights reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package message
|
|
|
|
import "fmt"
|
|
|
|
// A PUBACK Packet is the response to a PUBLISH Packet with QoS level 1.
|
|
type PubackMessage struct {
|
|
header
|
|
}
|
|
|
|
var _ Message = (*PubackMessage)(nil)
|
|
|
|
// NewPubackMessage creates a new PUBACK message.
|
|
func NewPubackMessage() *PubackMessage {
|
|
msg := &PubackMessage{}
|
|
msg.SetType(PUBACK)
|
|
|
|
return msg
|
|
}
|
|
|
|
func (this PubackMessage) String() string {
|
|
return fmt.Sprintf("%s, Packet ID=%d", this.header, this.packetId)
|
|
}
|
|
|
|
func (this *PubackMessage) Len() int {
|
|
if !this.dirty {
|
|
return len(this.dbuf)
|
|
}
|
|
|
|
ml := this.msglen()
|
|
|
|
if err := this.SetRemainingLength(int32(ml)); err != nil {
|
|
return 0
|
|
}
|
|
|
|
return this.header.msglen() + ml
|
|
}
|
|
|
|
func (this *PubackMessage) Decode(src []byte) (int, error) {
|
|
total := 0
|
|
|
|
n, err := this.header.decode(src[total:])
|
|
total += n
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
|
|
//this.packetId = binary.BigEndian.Uint16(src[total:])
|
|
this.packetId = src[total : total+2]
|
|
total += 2
|
|
|
|
this.dirty = false
|
|
|
|
return total, nil
|
|
}
|
|
|
|
func (this *PubackMessage) Encode(dst []byte) (int, error) {
|
|
if !this.dirty {
|
|
if len(dst) < len(this.dbuf) {
|
|
return 0, fmt.Errorf("puback/Encode: Insufficient buffer size. Expecting %d, got %d.", len(this.dbuf), len(dst))
|
|
}
|
|
|
|
return copy(dst, this.dbuf), nil
|
|
}
|
|
|
|
hl := this.header.msglen()
|
|
ml := this.msglen()
|
|
|
|
if len(dst) < hl+ml {
|
|
return 0, fmt.Errorf("puback/Encode: Insufficient buffer size. Expecting %d, got %d.", hl+ml, len(dst))
|
|
}
|
|
|
|
if err := this.SetRemainingLength(int32(ml)); err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
total := 0
|
|
|
|
n, err := this.header.encode(dst[total:])
|
|
total += n
|
|
if err != nil {
|
|
return total, err
|
|
}
|
|
|
|
if copy(dst[total:total+2], this.packetId) != 2 {
|
|
dst[total], dst[total+1] = 0, 0
|
|
}
|
|
total += 2
|
|
|
|
return total, nil
|
|
}
|
|
|
|
func (this *PubackMessage) msglen() int {
|
|
// packet ID
|
|
return 2
|
|
}
|