Skip to content

Commit

Permalink
newKeepalive/asKeepalive (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelquigley committed Aug 20, 2021
1 parent 8c9d49f commit a68f95e
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ func (wm *WireMessage) encodeHeader(dataSize uint16) (*WireMessage, error) {
return wm, nil
}

func newKeepalive(rxPortalSize int, pool *Pool) (wm *WireMessage, err error) {
wm = &WireMessage{
Seq: -1,
Mt: KEEPALIVE,
buf: pool.Get(),
}
util.WriteInt32(wm.buf.Data[dataStart:], int32(rxPortalSize))
return wm.encodeHeader(4)
}

func (wm *WireMessage) asKeepalive() (rxPortalSize int, err error) {
if wm.messageType() != KEEPALIVE {
return 0, errors.Errorf("unexpected message type [%d], expected KEEPALIVE", wm.messageType())
}
if wm.buf.Used < dataStart+4 {
return 0, errors.Errorf("short buffer for keepalive decode [%d < %d]", wm.buf.Used, dataStart+4)
}
rxPortalSize = int(util.ReadInt32(wm.buf.Data[dataStart:]))
return rxPortalSize, nil
}

func decodeHeader(buf *Buffer) (*WireMessage, error) {
size := util.ReadUint16(buf.Data[5:dataStart])
if uint32(dataStart+size) > buf.Used {
Expand Down

0 comments on commit a68f95e

Please sign in to comment.