Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaost committed Jul 17, 2024
1 parent a73b0c9 commit 4105b6c
Show file tree
Hide file tree
Showing 7 changed files with 880 additions and 77 deletions.
151 changes: 75 additions & 76 deletions protocol/thrift/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,49 @@ import (
"github.com/cloudwego/gopkg/internal/unsafe"
)

var Binary binaryProtocol
var Binary BinaryProtocol

type binaryProtocol struct{}
type BinaryProtocol struct{}

func (binaryProtocol) WriteMessageBegin(buf []byte, name string, typeID TMessageType, seq int32) int {
func (BinaryProtocol) WriteMessageBegin(buf []byte, name string, typeID TMessageType, seq int32) int {
binary.BigEndian.PutUint32(buf, uint32(msgVersion1)|uint32(typeID&msgTypeMask))
binary.BigEndian.PutUint32(buf[4:], uint32(len(name)))
off := 8 + copy(buf[8:], name)
binary.BigEndian.PutUint32(buf[off:], uint32(seq))
return off + 4
}

func (binaryProtocol) WriteFieldBegin(buf []byte, typeID TType, id int16) int {
func (BinaryProtocol) WriteFieldBegin(buf []byte, typeID TType, id int16) int {
buf[0] = byte(typeID)
binary.BigEndian.PutUint16(buf[1:], uint16(id))
return 3
}

func (binaryProtocol) WriteFieldStop(buf []byte) int {
func (BinaryProtocol) WriteFieldStop(buf []byte) int {
buf[0] = byte(STOP)
return 1
}

func (binaryProtocol) WriteMapBegin(buf []byte, kt, vt TType, size int) int {
func (BinaryProtocol) WriteMapBegin(buf []byte, kt, vt TType, size int) int {
buf[0] = byte(kt)
buf[1] = byte(vt)
binary.BigEndian.PutUint32(buf[2:], uint32(size))
return 6
}

func (binaryProtocol) WriteListBegin(buf []byte, et TType, size int) int {
func (BinaryProtocol) WriteListBegin(buf []byte, et TType, size int) int {
buf[0] = byte(et)
binary.BigEndian.PutUint32(buf[1:], uint32(size))
return 5
}

func (binaryProtocol) WriteSetBegin(buf []byte, et TType, size int) int {
func (BinaryProtocol) WriteSetBegin(buf []byte, et TType, size int) int {
buf[0] = byte(et)
binary.BigEndian.PutUint32(buf[1:], uint32(size))
return 5
}

func (binaryProtocol) WriteBool(buf []byte, v bool) int {
func (BinaryProtocol) WriteBool(buf []byte, v bool) int {
if v {
buf[0] = 1
} else {
Expand All @@ -75,115 +75,115 @@ func (binaryProtocol) WriteBool(buf []byte, v bool) int {
return 1
}

func (binaryProtocol) WriteByte(buf []byte, v int8) int {
func (BinaryProtocol) WriteByte(buf []byte, v int8) int {
buf[0] = byte(v)
return 1
}

func (binaryProtocol) WriteI16(buf []byte, v int16) int {
func (BinaryProtocol) WriteI16(buf []byte, v int16) int {
binary.BigEndian.PutUint16(buf, uint16(v))
return 2
}

func (binaryProtocol) WriteI32(buf []byte, v int32) int {
func (BinaryProtocol) WriteI32(buf []byte, v int32) int {
binary.BigEndian.PutUint32(buf, uint32(v))
return 4
}

func (binaryProtocol) WriteI64(buf []byte, v int64) int {
func (BinaryProtocol) WriteI64(buf []byte, v int64) int {
binary.BigEndian.PutUint64(buf, uint64(v))
return 8
}

func (binaryProtocol) WriteDouble(buf []byte, v float64) int {
func (BinaryProtocol) WriteDouble(buf []byte, v float64) int {
binary.BigEndian.PutUint64(buf, math.Float64bits(v))
return 8
}

func (binaryProtocol) WriteBinary(buf, v []byte) int {
func (BinaryProtocol) WriteBinary(buf, v []byte) int {
binary.BigEndian.PutUint32(buf, uint32(len(v)))
return 4 + copy(buf[4:], v)
}

func (binaryProtocol) WriteBinaryNocopy(buf []byte, w NocopyWriter, v []byte) int {
func (p BinaryProtocol) WriteBinaryNocopy(buf []byte, w NocopyWriter, v []byte) int {
if w == nil || len(buf) < NocopyWriteThreshold {
return Binary.WriteBinary(buf, v)
return p.WriteBinary(buf, v)
}
binary.BigEndian.PutUint32(buf, uint32(len(v)))
_ = w.WriteDirect(v, len(buf[4:])) // always err == nil ?
return 4
}

func (binaryProtocol) WriteString(buf []byte, v string) int {
func (BinaryProtocol) WriteString(buf []byte, v string) int {
binary.BigEndian.PutUint32(buf, uint32(len(v)))
return 4 + copy(buf[4:], v)
}

func (binaryProtocol) WriteStringNocopy(buf []byte, w NocopyWriter, v string) int {
return Binary.WriteBinaryNocopy(buf, w, unsafe.StringToByteSlice(v))
func (p BinaryProtocol) WriteStringNocopy(buf []byte, w NocopyWriter, v string) int {
return p.WriteBinaryNocopy(buf, w, unsafe.StringToByteSlice(v))
}

// Append methods

func (binaryProtocol) AppendMessageBegin(buf []byte, name string, typeID TMessageType, seq int32) []byte {
func (p BinaryProtocol) AppendMessageBegin(buf []byte, name string, typeID TMessageType, seq int32) []byte {
buf = appendUint32(buf, uint32(msgVersion1)|uint32(typeID&msgTypeMask))
buf = Binary.AppendString(buf, name)
return Binary.AppendI32(buf, seq)
buf = p.AppendString(buf, name)
return p.AppendI32(buf, seq)
}

func (binaryProtocol) AppendFieldBegin(buf []byte, typeID TType, id int16) []byte {
func (BinaryProtocol) AppendFieldBegin(buf []byte, typeID TType, id int16) []byte {
return append(buf, byte(typeID), byte(uint16(id>>8)), byte(id))
}

func (binaryProtocol) AppendFieldStop(buf []byte) []byte {
func (BinaryProtocol) AppendFieldStop(buf []byte) []byte {
return append(buf, byte(STOP))
}

func (binaryProtocol) AppendMapBegin(buf []byte, kt, vt TType, size int) []byte {
return Binary.AppendI32(append(buf, byte(kt), byte(vt)), int32(size))
func (p BinaryProtocol) AppendMapBegin(buf []byte, kt, vt TType, size int) []byte {
return p.AppendI32(append(buf, byte(kt), byte(vt)), int32(size))
}

func (binaryProtocol) AppendListBegin(buf []byte, et TType, size int) []byte {
return Binary.AppendI32(append(buf, byte(et)), int32(size))
func (p BinaryProtocol) AppendListBegin(buf []byte, et TType, size int) []byte {
return p.AppendI32(append(buf, byte(et)), int32(size))
}

func (binaryProtocol) AppendSetBegin(buf []byte, et TType, size int) []byte {
return Binary.AppendI32(append(buf, byte(et)), int32(size))
func (p BinaryProtocol) AppendSetBegin(buf []byte, et TType, size int) []byte {
return p.AppendI32(append(buf, byte(et)), int32(size))
}

func (binaryProtocol) AppendBinary(buf, v []byte) []byte {
return append(Binary.AppendI32(buf, int32(len(v))), v...)
func (p BinaryProtocol) AppendBinary(buf, v []byte) []byte {
return append(p.AppendI32(buf, int32(len(v))), v...)
}

func (binaryProtocol) AppendString(buf []byte, v string) []byte {
return append(Binary.AppendI32(buf, int32(len(v))), v...)
func (p BinaryProtocol) AppendString(buf []byte, v string) []byte {
return append(p.AppendI32(buf, int32(len(v))), v...)
}

func (binaryProtocol) AppendBool(buf []byte, v bool) []byte {
func (BinaryProtocol) AppendBool(buf []byte, v bool) []byte {
if v {
return append(buf, 1)
} else {
return append(buf, 0)
}
}

func (binaryProtocol) AppendByte(buf []byte, v int8) []byte {
func (BinaryProtocol) AppendByte(buf []byte, v int8) []byte {
return append(buf, byte(v))
}

func (binaryProtocol) AppendI16(buf []byte, v int16) []byte {
func (BinaryProtocol) AppendI16(buf []byte, v int16) []byte {
return append(buf, byte(uint16(v)>>8), byte(v))
}

func (binaryProtocol) AppendI32(buf []byte, v int32) []byte {
func (BinaryProtocol) AppendI32(buf []byte, v int32) []byte {
return appendUint32(buf, uint32(v))
}

func (binaryProtocol) AppendI64(buf []byte, v int64) []byte {
func (BinaryProtocol) AppendI64(buf []byte, v int64) []byte {
return appendUint64(buf, uint64(v))
}

func (binaryProtocol) AppendDouble(buf []byte, v float64) []byte {
func (BinaryProtocol) AppendDouble(buf []byte, v float64) []byte {
return appendUint64(buf, math.Float64bits(v))
}

Expand All @@ -198,25 +198,25 @@ func appendUint64(buf []byte, v uint64) []byte {

// Length methods

func (binaryProtocol) MessageBeginLength(name string, _ TMessageType, _ int32) int {
func (BinaryProtocol) MessageBeginLength(name string, _ TMessageType, _ int32) int {
return 4 + (4 + len(name)) + 4
}

func (binaryProtocol) FieldBeginLength() int { return 3 }
func (binaryProtocol) FieldStopLength() int { return 1 }
func (binaryProtocol) MapBeginLength() int { return 6 }
func (binaryProtocol) ListBeginLength() int { return 5 }
func (binaryProtocol) SetBeginLength() int { return 5 }
func (binaryProtocol) BoolLength() int { return 1 }
func (binaryProtocol) ByteLength() int { return 1 }
func (binaryProtocol) I16Length() int { return 2 }
func (binaryProtocol) I32Length() int { return 4 }
func (binaryProtocol) I64Length() int { return 8 }
func (binaryProtocol) DoubleLength() int { return 8 }
func (binaryProtocol) StringLength(v string) int { return 4 + len(v) }
func (binaryProtocol) BinaryLength(v []byte) int { return 4 + len(v) }
func (binaryProtocol) StringLengthNocopy(v string) int { return 4 + len(v) }
func (binaryProtocol) BinaryLengthNocopy(v []byte) int { return 4 + len(v) }
func (BinaryProtocol) FieldBeginLength() int { return 3 }
func (BinaryProtocol) FieldStopLength() int { return 1 }
func (BinaryProtocol) MapBeginLength() int { return 6 }
func (BinaryProtocol) ListBeginLength() int { return 5 }
func (BinaryProtocol) SetBeginLength() int { return 5 }
func (BinaryProtocol) BoolLength() int { return 1 }
func (BinaryProtocol) ByteLength() int { return 1 }
func (BinaryProtocol) I16Length() int { return 2 }
func (BinaryProtocol) I32Length() int { return 4 }
func (BinaryProtocol) I64Length() int { return 8 }
func (BinaryProtocol) DoubleLength() int { return 8 }
func (BinaryProtocol) StringLength(v string) int { return 4 + len(v) }
func (BinaryProtocol) BinaryLength(v []byte) int { return 4 + len(v) }
func (BinaryProtocol) StringLengthNocopy(v string) int { return 4 + len(v) }
func (BinaryProtocol) BinaryLengthNocopy(v []byte) int { return 4 + len(v) }

// Read methods

Expand All @@ -225,7 +225,7 @@ var (
errBadVersion = NewProtocolException(BAD_VERSION, "ReadMessageBegin: bad version")
)

func (binaryProtocol) ReadMessageBegin(buf []byte) (name string, typeID TMessageType, seq int32, l int, err error) {
func (p BinaryProtocol) ReadMessageBegin(buf []byte) (name string, typeID TMessageType, seq int32, l int, err error) {
if len(buf) < 4 { // version+type header + name header
return "", 0, 0, 0, errReadMessage
}
Expand All @@ -240,14 +240,14 @@ func (binaryProtocol) ReadMessageBegin(buf []byte) (name string, typeID TMessage
off := 4

// read method name
name, l, err1 := Binary.ReadString(buf[off:])
name, l, err1 := p.ReadString(buf[off:])
if err1 != nil {
return "", 0, 0, 0, errReadMessage
}
off += l

// read seq
seq, l, err2 := Binary.ReadI32(buf[off:])
seq, l, err2 := p.ReadI32(buf[off:])
if err2 != nil {
return "", 0, 0, 0, errReadMessage
}
Expand All @@ -271,7 +271,7 @@ var (
errReadDouble = NewProtocolException(INVALID_DATA, "ReadDouble: len(buf) < 8")
)

func (binaryProtocol) ReadFieldBegin(buf []byte) (typeID TType, id int16, l int, err error) {
func (BinaryProtocol) ReadFieldBegin(buf []byte) (typeID TType, id int16, l int, err error) {
if len(buf) < 1 {
return 0, 0, 0, errReadField
}
Expand All @@ -285,29 +285,29 @@ func (binaryProtocol) ReadFieldBegin(buf []byte) (typeID TType, id int16, l int,
return typeID, int16(binary.BigEndian.Uint16(buf[1:])), 3, nil
}

func (binaryProtocol) ReadMapBegin(buf []byte) (kt, vt TType, size, l int, err error) {
func (BinaryProtocol) ReadMapBegin(buf []byte) (kt, vt TType, size, l int, err error) {
if len(buf) < 6 {
return 0, 0, 0, 0, errReadMap
}
return TType(buf[0]), TType(buf[1]), int(binary.BigEndian.Uint32(buf[2:])), 6, nil
}

func (binaryProtocol) ReadListBegin(buf []byte) (et TType, size, l int, err error) {
func (BinaryProtocol) ReadListBegin(buf []byte) (et TType, size, l int, err error) {
if len(buf) < 5 {
return 0, 0, 0, errReadList
}
return TType(buf[0]), int(binary.BigEndian.Uint32(buf[1:])), 5, nil
}

func (binaryProtocol) ReadSetBegin(buf []byte) (et TType, size, l int, err error) {
func (BinaryProtocol) ReadSetBegin(buf []byte) (et TType, size, l int, err error) {
if len(buf) < 5 {
return 0, 0, 0, errReadSet
}
return TType(buf[0]), int(binary.BigEndian.Uint32(buf[1:])), 5, nil
}

func (binaryProtocol) ReadBinary(buf []byte) (b []byte, l int, err error) {
sz, _, err := Binary.ReadI32(buf)
func (p BinaryProtocol) ReadBinary(buf []byte) (b []byte, l int, err error) {
sz, _, err := p.ReadI32(buf)
if err != nil {
return nil, 0, errReadBin
}
Expand All @@ -319,8 +319,8 @@ func (binaryProtocol) ReadBinary(buf []byte) (b []byte, l int, err error) {
return []byte(string(buf[4:l])), l, nil
}

func (binaryProtocol) ReadString(buf []byte) (s string, l int, err error) {
sz, _, err := Binary.ReadI32(buf)
func (p BinaryProtocol) ReadString(buf []byte) (s string, l int, err error) {
sz, _, err := p.ReadI32(buf)
if err != nil {
return "", 0, errReadStr
}
Expand All @@ -332,7 +332,7 @@ func (binaryProtocol) ReadString(buf []byte) (s string, l int, err error) {
return string(buf[4:l]), l, nil
}

func (binaryProtocol) ReadBool(buf []byte) (v bool, l int, err error) {
func (BinaryProtocol) ReadBool(buf []byte) (v bool, l int, err error) {
if len(buf) < 1 {
return false, 0, errReadBool
}
Expand All @@ -342,35 +342,35 @@ func (binaryProtocol) ReadBool(buf []byte) (v bool, l int, err error) {
return false, 1, nil
}

func (binaryProtocol) ReadByte(buf []byte) (v int8, l int, err error) {
func (BinaryProtocol) ReadByte(buf []byte) (v int8, l int, err error) {
if len(buf) < 1 {
return 0, 0, errReadByte
}
return int8(buf[0]), 1, nil
}

func (binaryProtocol) ReadI16(buf []byte) (v int16, l int, err error) {
func (BinaryProtocol) ReadI16(buf []byte) (v int16, l int, err error) {
if len(buf) < 2 {
return 0, 0, errReadI16
}
return int16(binary.BigEndian.Uint16(buf)), 2, nil
}

func (binaryProtocol) ReadI32(buf []byte) (v int32, l int, err error) {
func (BinaryProtocol) ReadI32(buf []byte) (v int32, l int, err error) {
if len(buf) < 4 {
return 0, 0, errReadI32
}
return int32(binary.BigEndian.Uint32(buf)), 4, nil
}

func (binaryProtocol) ReadI64(buf []byte) (v int64, l int, err error) {
func (BinaryProtocol) ReadI64(buf []byte) (v int64, l int, err error) {
if len(buf) < 8 {
return 0, 0, errReadI64
}
return int64(binary.BigEndian.Uint64(buf)), 8, nil
}

func (binaryProtocol) ReadDouble(buf []byte) (v float64, l int, err error) {
func (BinaryProtocol) ReadDouble(buf []byte) (v float64, l int, err error) {
if len(buf) < 8 {
return 0, 0, errReadDouble
}
Expand All @@ -379,7 +379,6 @@ func (binaryProtocol) ReadDouble(buf []byte) (v float64, l int, err error) {

var (
errDepthLimitExceeded = NewProtocolException(DEPTH_LIMIT, "depth limit exceeded")
errNegativeSize = NewProtocolException(NEGATIVE_SIZE, "negative size")
)

var typeToSize = [256]int8{
Expand All @@ -396,7 +395,7 @@ func skipstr(b []byte) int {
}

// Skip skips over the value for the given type using Go implementation.
func (binaryProtocol) Skip(b []byte, t TType) (int, error) {
func (BinaryProtocol) Skip(b []byte, t TType) (int, error) {
return skipType(b, t, defaultRecursionDepth)
}

Expand Down
Loading

0 comments on commit 4105b6c

Please sign in to comment.