Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Akegarasu committed Oct 23, 2023
1 parent 3c026c8 commit 77e5a27
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packet/heartbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package packet

// NewHeartBeatPacket 构造心跳包
func NewHeartBeatPacket() []byte {
pkt := NewPacket(Plain, HeartBeat, nil)
pkt := NewPacket(1, HeartBeat, nil)
return pkt.Build()
}
15 changes: 13 additions & 2 deletions packet/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const (
)

const (
_ = iota
_
HandShake = iota
HandShakeResponse
HeartBeat
HeartBeatResponse
_
Expand Down Expand Up @@ -94,10 +94,21 @@ func (p *Packet) Unmarshal(v interface{}) error {
}

func (p *Packet) Build() []byte {
// default packet with some constants
// raw header length: buf[5] = 16
// seq: buf[16] = 1
rawBuf := []byte{0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}

// protocol version
binary.BigEndian.PutUint16(rawBuf[6:], p.ProtocolVersion)

// operation code
binary.BigEndian.PutUint32(rawBuf[8:], p.Operation)

// append payload
rawBuf = append(rawBuf, p.Body...)

// payload length
binary.BigEndian.PutUint32(rawBuf, uint32(len(rawBuf)))
return rawBuf
}
Expand Down

0 comments on commit 77e5a27

Please sign in to comment.