Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
wangjian | afc76d7d15 |
18
encoder.go
18
encoder.go
|
@ -9,7 +9,7 @@ import (
|
|||
// Encoder will encode object to encoding
|
||||
type encoder struct {
|
||||
seqID byte
|
||||
valbuf []byte
|
||||
valBuf []byte
|
||||
isNode bool
|
||||
isArray bool
|
||||
buf *bytes.Buffer
|
||||
|
@ -21,19 +21,19 @@ type iEncoder interface {
|
|||
}
|
||||
|
||||
func (enc *encoder) GetValBuf() []byte {
|
||||
return enc.valbuf
|
||||
return enc.valBuf
|
||||
}
|
||||
|
||||
func (enc *encoder) IsEmpty() bool {
|
||||
return len(enc.valbuf) == 0
|
||||
return len(enc.valBuf) == 0
|
||||
}
|
||||
|
||||
func (enc *encoder) AddBytes(buf []byte) {
|
||||
enc.valbuf = append(enc.valbuf, buf...)
|
||||
enc.valBuf = append(enc.valBuf, buf...)
|
||||
}
|
||||
|
||||
func (enc *encoder) addRawPacket(en iEncoder) {
|
||||
enc.valbuf = append(enc.valbuf, en.Encode()...)
|
||||
enc.valBuf = append(enc.valBuf, en.Encode()...)
|
||||
}
|
||||
|
||||
// setTag write tag as seqID
|
||||
|
@ -51,11 +51,11 @@ func (enc *encoder) writeTag() {
|
|||
}
|
||||
|
||||
func (enc *encoder) writeLengthBuf() {
|
||||
vallen := len(enc.valbuf)
|
||||
size := encoding.SizeOfPVarInt32(int32(vallen))
|
||||
valLen := len(enc.valBuf)
|
||||
size := encoding.SizeOfPVarInt32(int32(valLen))
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
tmp := make([]byte, size)
|
||||
err := codec.EncodePVarInt32(tmp, int32(vallen))
|
||||
err := codec.EncodePVarInt32(tmp, int32(valLen))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func (enc *encoder) Encode() []byte {
|
|||
// Len
|
||||
enc.writeLengthBuf()
|
||||
// Val
|
||||
enc.buf.Write(enc.valbuf)
|
||||
enc.buf.Write(enc.valBuf)
|
||||
enc.complete = true
|
||||
}
|
||||
return enc.buf.Bytes()
|
||||
|
|
|
@ -60,7 +60,7 @@ func (codec *VarCodec) encodeVarFloat(buffer []byte, bits uint64, width int) err
|
|||
return errors.New("nothing to encode")
|
||||
}
|
||||
|
||||
const unit = 8 // bit width of encoding unit
|
||||
const unit = 8 // the bit width of encoding unit
|
||||
var gap, mask = codec.sizeOfGap(width)
|
||||
|
||||
for (codec.Size & mask) > 0 {
|
||||
|
@ -81,7 +81,7 @@ func (codec *VarCodec) decodeVarFloat(buffer []byte, bits *uint64, width int) er
|
|||
return errors.New("nothing to decode")
|
||||
}
|
||||
|
||||
const unit = 8 // bit width of encoding unit
|
||||
const unit = 8 // the bit width of encoding unit
|
||||
var gap, mask = codec.sizeOfGap(width)
|
||||
|
||||
for (codec.Size & mask) > 0 {
|
||||
|
|
2
go.mod
2
go.mod
|
@ -1,6 +1,6 @@
|
|||
module git.hpds.cc/Component/mq_coder
|
||||
|
||||
go 1.18
|
||||
go 1.19
|
||||
|
||||
require github.com/stretchr/testify v1.8.0
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ func DecodeToNodePacket(buf []byte, pct *NodePacket) (consumedBytes int, err err
|
|||
pct.buf.WriteByte(buf[pos])
|
||||
pos++
|
||||
|
||||
// `Length`: the type is `varint`
|
||||
// `Length`: the type is `varInt`
|
||||
tmpBuf := buf[pos:]
|
||||
var valLen int32
|
||||
codec := encoding.VarCodec{}
|
||||
|
@ -87,7 +87,7 @@ func DecodeToNodePacket(buf []byte, pct *NodePacket) (consumedBytes int, err err
|
|||
if isNode {
|
||||
pct.NodePackets[np.packet.tag.SeqId()] = *np
|
||||
} else {
|
||||
pct.PrimitivePackets[byte(pp.SeqId())] = *pp
|
||||
pct.PrimitivePackets[pp.SeqId()] = *pp
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ func DecodeToPrimitivePacket(buf []byte, p *PrimitivePacket) (*DecodeState, erro
|
|||
pos++
|
||||
decoder.ConsumedBytes = pos
|
||||
|
||||
// read `Varint` from buf for `Length of value`
|
||||
// read `Variant` from buf for `Length of value`
|
||||
tmpBuf := buf[pos:]
|
||||
var bufLen int32
|
||||
codec := encoding.VarCodec{}
|
||||
|
|
|
@ -28,8 +28,8 @@ func NewPrimitivePacketEncoder(sid byte) *PrimitivePacketEncoder {
|
|||
func (enc *PrimitivePacketEncoder) SetInt32Value(v int32) {
|
||||
size := encoding.SizeOfNVarInt32(v)
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodeNVarInt32(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodeNVarInt32(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -39,8 +39,8 @@ func (enc *PrimitivePacketEncoder) SetInt32Value(v int32) {
|
|||
func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) {
|
||||
size := encoding.SizeOfNVarUInt32(v)
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodeNVarUInt32(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodeNVarUInt32(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -50,8 +50,8 @@ func (enc *PrimitivePacketEncoder) SetUInt32Value(v uint32) {
|
|||
func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) {
|
||||
size := encoding.SizeOfNVarInt64(v)
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodeNVarInt64(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodeNVarInt64(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -61,8 +61,8 @@ func (enc *PrimitivePacketEncoder) SetInt64Value(v int64) {
|
|||
func (enc *PrimitivePacketEncoder) SetUInt64Value(v uint64) {
|
||||
size := encoding.SizeOfNVarUInt64(v)
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodeNVarUInt64(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodeNVarUInt64(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -72,8 +72,8 @@ func (enc *PrimitivePacketEncoder) SetUInt64Value(v uint64) {
|
|||
func (enc *PrimitivePacketEncoder) SetFloat32Value(v float32) {
|
||||
var size = encoding.SizeOfVarFloat32(v)
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodeVarFloat32(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodeVarFloat32(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -83,8 +83,8 @@ func (enc *PrimitivePacketEncoder) SetFloat32Value(v float32) {
|
|||
func (enc *PrimitivePacketEncoder) SetFloat64Value(v float64) {
|
||||
var size = encoding.SizeOfVarFloat64(v)
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodeVarFloat64(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodeVarFloat64(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -94,8 +94,8 @@ func (enc *PrimitivePacketEncoder) SetFloat64Value(v float64) {
|
|||
func (enc *PrimitivePacketEncoder) SetBoolValue(v bool) {
|
||||
var size = encoding.SizeOfPVarUInt32(uint32(1))
|
||||
codec := encoding.VarCodec{Size: size}
|
||||
enc.valbuf = make([]byte, size)
|
||||
err := codec.EncodePVarBool(enc.valbuf, v)
|
||||
enc.valBuf = make([]byte, size)
|
||||
err := codec.EncodePVarBool(enc.valBuf, v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -103,10 +103,10 @@ func (enc *PrimitivePacketEncoder) SetBoolValue(v bool) {
|
|||
|
||||
// SetStringValue encode string
|
||||
func (enc *PrimitivePacketEncoder) SetStringValue(v string) {
|
||||
enc.valbuf = []byte(v)
|
||||
enc.valBuf = []byte(v)
|
||||
}
|
||||
|
||||
// SetBytesValue encode []byte
|
||||
func (enc *PrimitivePacketEncoder) SetBytesValue(v []byte) {
|
||||
enc.valbuf = v
|
||||
enc.valBuf = v
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue