1 // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
2 // Use of this source code is governed by a MIT license found in the LICENSE file.
7 Msgpack-c implementation powers the c, c++, python, ruby, etc libraries.
8 We need to maintain compatibility with it and how it encodes integer values
9 without caring about the type.
11 For compatibility with behaviour of msgpack-c reference implementation:
12 - Go intX (>0) and uintX
14 msgpack +ve fixnum, unsigned
17 msgpack -ve fixnum, signed
32 mpPosFixNumMin byte = 0x00
82 var mpTimeExtTag int8 = -1
83 var mpTimeExtTagU = uint8(mpTimeExtTag)
85 // var mpdesc = map[byte]string{
86 // mpPosFixNumMin: "PosFixNumMin",
87 // mpPosFixNumMax: "PosFixNumMax",
88 // mpFixMapMin: "FixMapMin",
89 // mpFixMapMax: "FixMapMax",
90 // mpFixArrayMin: "FixArrayMin",
91 // mpFixArrayMax: "FixArrayMax",
92 // mpFixStrMin: "FixStrMin",
93 // mpFixStrMax: "FixStrMax",
98 // mpDouble: "Double",
100 // mpUint16: "Uint16",
101 // mpUint32: "Uint32",
102 // mpUint64: "Uint64",
113 // mpFixExt1: "FixExt1",
114 // mpFixExt2: "FixExt2",
115 // mpFixExt4: "FixExt4",
116 // mpFixExt8: "FixExt8",
117 // mpFixExt16: "FixExt16",
121 // mpArray16: "Array16",
122 // mpArray32: "Array32",
125 // mpNegFixNumMin: "NegFixNumMin",
126 // mpNegFixNumMax: "NegFixNumMax",
129 func mpdesc(bd byte) string {
137 case mpFloat, mpDouble:
139 case mpUint8, mpUint16, mpUint32, mpUint64:
141 case mpInt8, mpInt16, mpInt32, mpInt64:
145 case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
147 case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
149 case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
150 return "string|bytes"
151 case bd == mpBin8, bd == mpBin16, bd == mpBin32:
153 case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
155 case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
157 case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
165 // MsgpackSpecRpcMultiArgs is a special type which signifies to the MsgpackSpecRpcCodec
166 // that the backend RPC service takes multiple arguments, which have been arranged
167 // in sequence in the slice.
169 // The Codec then passes it AS-IS to the rpc service (without wrapping it in an
170 // array of 1 element).
171 type MsgpackSpecRpcMultiArgs []interface{}
173 // A MsgpackContainer type specifies the different types of msgpackContainers.
174 type msgpackContainerType struct {
176 bFixMin, b8, b16, b32 byte
177 hasFixMin, has8, has8Always bool
181 msgpackContainerStr = msgpackContainerType{
182 32, mpFixStrMin, mpStr8, mpStr16, mpStr32, true, true, false,
184 msgpackContainerBin = msgpackContainerType{
185 0, 0, mpBin8, mpBin16, mpBin32, false, true, true,
187 msgpackContainerList = msgpackContainerType{
188 16, mpFixArrayMin, 0, mpArray16, mpArray32, true, false, false,
190 msgpackContainerMap = msgpackContainerType{
191 16, mpFixMapMin, 0, mpMap16, mpMap32, true, false, false,
195 //---------------------------------------------
197 type msgpackEncDriver struct {
199 encDriverNoopContainerWriter
205 // _ [3]uint64 // padding
208 func (e *msgpackEncDriver) EncodeNil() {
212 func (e *msgpackEncDriver) EncodeInt(i int64) {
213 if e.h.PositiveIntUnsigned && i >= 0 {
214 e.EncodeUint(uint64(i))
215 } else if i > math.MaxInt8 {
216 if i <= math.MaxInt16 {
218 bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
219 } else if i <= math.MaxInt32 {
221 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
224 bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
228 e.w.writen2(mpInt8, byte(i))
232 } else if i >= math.MinInt8 {
233 e.w.writen2(mpInt8, byte(i))
234 } else if i >= math.MinInt16 {
236 bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
237 } else if i >= math.MinInt32 {
239 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
242 bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
246 func (e *msgpackEncDriver) EncodeUint(i uint64) {
247 if i <= math.MaxInt8 {
249 e.w.writen2(mpUint8, byte(i))
253 } else if i <= math.MaxUint8 {
254 e.w.writen2(mpUint8, byte(i))
255 } else if i <= math.MaxUint16 {
256 e.w.writen1(mpUint16)
257 bigenHelper{e.x[:2], e.w}.writeUint16(uint16(i))
258 } else if i <= math.MaxUint32 {
259 e.w.writen1(mpUint32)
260 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(i))
262 e.w.writen1(mpUint64)
263 bigenHelper{e.x[:8], e.w}.writeUint64(uint64(i))
267 func (e *msgpackEncDriver) EncodeBool(b bool) {
275 func (e *msgpackEncDriver) EncodeFloat32(f float32) {
277 bigenHelper{e.x[:4], e.w}.writeUint32(math.Float32bits(f))
280 func (e *msgpackEncDriver) EncodeFloat64(f float64) {
281 e.w.writen1(mpDouble)
282 bigenHelper{e.x[:8], e.w}.writeUint64(math.Float64bits(f))
285 func (e *msgpackEncDriver) EncodeTime(t time.Time) {
291 sec, nsec := t.Unix(), uint64(t.Nanosecond())
294 if sec >= 0 && sec>>34 == 0 {
295 data64 = (nsec << 34) | uint64(sec)
296 if data64&0xffffffff00000000 != 0 {
303 e.encodeExtPreamble(mpTimeExtTagU, l)
305 e.writeContainerLen(msgpackContainerStr, l)
309 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(data64))
311 bigenHelper{e.x[:8], e.w}.writeUint64(data64)
313 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(nsec))
314 bigenHelper{e.x[:8], e.w}.writeUint64(uint64(sec))
318 func (e *msgpackEncDriver) EncodeExt(v interface{}, xtag uint64, ext Ext, _ *Encoder) {
319 bs := ext.WriteExt(v)
325 e.encodeExtPreamble(uint8(xtag), len(bs))
328 e.EncodeStringBytes(cRAW, bs)
332 func (e *msgpackEncDriver) EncodeRawExt(re *RawExt, _ *Encoder) {
333 e.encodeExtPreamble(uint8(re.Tag), len(re.Data))
337 func (e *msgpackEncDriver) encodeExtPreamble(xtag byte, l int) {
339 e.w.writen2(mpFixExt1, xtag)
341 e.w.writen2(mpFixExt2, xtag)
343 e.w.writen2(mpFixExt4, xtag)
345 e.w.writen2(mpFixExt8, xtag)
347 e.w.writen2(mpFixExt16, xtag)
349 e.w.writen2(mpExt8, byte(l))
351 } else if l < 65536 {
353 bigenHelper{e.x[:2], e.w}.writeUint16(uint16(l))
357 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(l))
362 func (e *msgpackEncDriver) WriteArrayStart(length int) {
363 e.writeContainerLen(msgpackContainerList, length)
366 func (e *msgpackEncDriver) WriteMapStart(length int) {
367 e.writeContainerLen(msgpackContainerMap, length)
370 func (e *msgpackEncDriver) EncodeString(c charEncoding, s string) {
372 if c == cRAW && e.h.WriteExt {
373 e.writeContainerLen(msgpackContainerBin, slen)
375 e.writeContainerLen(msgpackContainerStr, slen)
382 func (e *msgpackEncDriver) EncodeStringBytes(c charEncoding, bs []byte) {
388 if c == cRAW && e.h.WriteExt {
389 e.writeContainerLen(msgpackContainerBin, slen)
391 e.writeContainerLen(msgpackContainerStr, slen)
398 func (e *msgpackEncDriver) writeContainerLen(ct msgpackContainerType, l int) {
399 if ct.hasFixMin && l < ct.fixCutoff {
400 e.w.writen1(ct.bFixMin | byte(l))
401 } else if ct.has8 && l < 256 && (ct.has8Always || e.h.WriteExt) {
402 e.w.writen2(ct.b8, uint8(l))
403 } else if l < 65536 {
405 bigenHelper{e.x[:2], e.w}.writeUint16(uint16(l))
408 bigenHelper{e.x[:4], e.w}.writeUint32(uint32(l))
412 //---------------------------------------------
414 type msgpackDecDriver struct {
418 // b [scratchByteArrayLen]byte
421 br bool // bytes reader
425 decDriverNoopContainerReader
426 // _ [3]uint64 // padding
429 // Note: This returns either a primitive (int, bool, etc) for non-containers,
430 // or a containerType, or a specific type denoting nil or extension.
431 // It is called when a nil interface{} is passed, leaving it up to the DecDriver
432 // to introspect the stream and decide how best to decode.
433 // It deciphers the value by looking at the stream first.
434 func (d *msgpackDecDriver) DecodeNaked() {
440 var decodeFurther bool
455 n.f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
458 n.f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
462 n.u = uint64(d.r.readn1())
465 n.u = uint64(bigen.Uint16(d.r.readx(2)))
468 n.u = uint64(bigen.Uint32(d.r.readx(4)))
471 n.u = uint64(bigen.Uint64(d.r.readx(8)))
475 n.i = int64(int8(d.r.readn1()))
478 n.i = int64(int16(bigen.Uint16(d.r.readx(2))))
481 n.i = int64(int32(bigen.Uint32(d.r.readx(4))))
484 n.i = int64(int64(bigen.Uint64(d.r.readx(8))))
488 case bd >= mpPosFixNumMin && bd <= mpPosFixNumMax:
489 // positive fixnum (always signed)
491 n.i = int64(int8(bd))
492 case bd >= mpNegFixNumMin && bd <= mpNegFixNumMax:
495 n.i = int64(int8(bd))
496 case bd == mpStr8, bd == mpStr16, bd == mpStr32, bd >= mpFixStrMin && bd <= mpFixStrMax:
498 n.v = valueTypeString
499 n.s = d.DecodeString()
502 n.l = d.DecodeBytes(nil, false)
504 case bd == mpBin8, bd == mpBin16, bd == mpBin32:
506 n.l = d.DecodeBytes(nil, false)
507 case bd == mpArray16, bd == mpArray32, bd >= mpFixArrayMin && bd <= mpFixArrayMax:
510 case bd == mpMap16, bd == mpMap32, bd >= mpFixMapMin && bd <= mpFixMapMax:
513 case bd >= mpFixExt1 && bd <= mpFixExt16, bd >= mpExt8 && bd <= mpExt32:
515 clen := d.readExtLen()
516 n.u = uint64(d.r.readn1())
517 if n.u == uint64(mpTimeExtTagU) {
519 n.t = d.decodeTime(clen)
521 n.l = d.r.readx(clen)
523 n.l = decByteSlice(d.r, clen, d.d.h.MaxInitLen, d.d.b[:])
526 d.d.errorf("cannot infer value: %s: Ox%x/%d/%s", msgBadDesc, bd, bd, mpdesc(bd))
532 if n.v == valueTypeUint && d.h.SignedInteger {
539 // int can be decoded from msgpack type: intXXX or uintXXX
540 func (d *msgpackDecDriver) DecodeInt64() (i int64) {
546 i = int64(uint64(d.r.readn1()))
548 i = int64(uint64(bigen.Uint16(d.r.readx(2))))
550 i = int64(uint64(bigen.Uint32(d.r.readx(4))))
552 i = int64(bigen.Uint64(d.r.readx(8)))
554 i = int64(int8(d.r.readn1()))
556 i = int64(int16(bigen.Uint16(d.r.readx(2))))
558 i = int64(int32(bigen.Uint32(d.r.readx(4))))
560 i = int64(bigen.Uint64(d.r.readx(8)))
563 case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
564 i = int64(int8(d.bd))
565 case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
566 i = int64(int8(d.bd))
568 d.d.errorf("cannot decode signed integer: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
576 // uint can be decoded from msgpack type: intXXX or uintXXX
577 func (d *msgpackDecDriver) DecodeUint64() (ui uint64) {
583 ui = uint64(d.r.readn1())
585 ui = uint64(bigen.Uint16(d.r.readx(2)))
587 ui = uint64(bigen.Uint32(d.r.readx(4)))
589 ui = bigen.Uint64(d.r.readx(8))
591 if i := int64(int8(d.r.readn1())); i >= 0 {
594 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
598 if i := int64(int16(bigen.Uint16(d.r.readx(2)))); i >= 0 {
601 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
605 if i := int64(int32(bigen.Uint32(d.r.readx(4)))); i >= 0 {
608 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
612 if i := int64(bigen.Uint64(d.r.readx(8))); i >= 0 {
615 d.d.errorf("assigning negative signed value: %v, to unsigned type", i)
620 case d.bd >= mpPosFixNumMin && d.bd <= mpPosFixNumMax:
622 case d.bd >= mpNegFixNumMin && d.bd <= mpNegFixNumMax:
623 d.d.errorf("assigning negative signed value: %v, to unsigned type", int(d.bd))
626 d.d.errorf("cannot decode unsigned integer: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
634 // float can either be decoded from msgpack type: float, double or intX
635 func (d *msgpackDecDriver) DecodeFloat64() (f float64) {
640 f = float64(math.Float32frombits(bigen.Uint32(d.r.readx(4))))
641 } else if d.bd == mpDouble {
642 f = math.Float64frombits(bigen.Uint64(d.r.readx(8)))
644 f = float64(d.DecodeInt64())
650 // bool can be decoded from bool, fixnum 0 or 1.
651 func (d *msgpackDecDriver) DecodeBool() (b bool) {
655 if d.bd == mpFalse || d.bd == 0 {
657 } else if d.bd == mpTrue || d.bd == 1 {
660 d.d.errorf("cannot decode bool: %s: %x/%s", msgBadDesc, d.bd, mpdesc(d.bd))
667 func (d *msgpackDecDriver) DecodeBytes(bs []byte, zerocopy bool) (bsOut []byte) {
672 // check if an "array" of uint8's (see ContainerType for how to infer if an array)
674 // DecodeBytes could be from: bin str fixstr fixarray array ...
676 vt := d.ContainerType()
679 // valueTypeBytes may be a mpBin or an mpStr container
680 if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 {
681 clen = d.readContainerLen(msgpackContainerBin)
683 clen = d.readContainerLen(msgpackContainerStr)
685 case valueTypeString:
686 clen = d.readContainerLen(msgpackContainerStr)
688 if zerocopy && len(bs) == 0 {
691 bsOut, _ = fastpathTV.DecSliceUint8V(bs, true, d.d)
694 d.d.errorf("invalid container type: expecting bin|str|array, got: 0x%x", uint8(vt))
698 // these are (bin|str)(8|16|32)
700 // bytes may be nil, so handle it. if nil, clen=-1.
706 return d.r.readx(clen)
707 } else if len(bs) == 0 {
711 return decByteSlice(d.r, clen, d.h.MaxInitLen, bs)
714 func (d *msgpackDecDriver) DecodeString() (s string) {
715 return string(d.DecodeBytes(d.d.b[:], true))
718 func (d *msgpackDecDriver) DecodeStringAsBytes() (s []byte) {
719 return d.DecodeBytes(d.d.b[:], true)
722 func (d *msgpackDecDriver) readNextBd() {
727 func (d *msgpackDecDriver) uncacheRead() {
734 func (d *msgpackDecDriver) ContainerType() (vt valueType) {
741 } else if bd == mpBin8 || bd == mpBin16 || bd == mpBin32 ||
743 (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax))) {
744 return valueTypeBytes
745 } else if d.h.RawToString &&
746 (bd == mpStr8 || bd == mpStr16 || bd == mpStr32 || (bd >= mpFixStrMin && bd <= mpFixStrMax)) {
747 return valueTypeString
748 } else if bd == mpArray16 || bd == mpArray32 || (bd >= mpFixArrayMin && bd <= mpFixArrayMax) {
749 return valueTypeArray
750 } else if bd == mpMap16 || bd == mpMap32 || (bd >= mpFixMapMin && bd <= mpFixMapMax) {
754 // d.d.errorf("isContainerType: unsupported parameter: %v", vt)
756 return valueTypeUnset
759 func (d *msgpackDecDriver) TryDecodeAsNil() (v bool) {
770 func (d *msgpackDecDriver) readContainerLen(ct msgpackContainerType) (clen int) {
773 clen = -1 // to represent nil
774 } else if bd == ct.b8 {
775 clen = int(d.r.readn1())
776 } else if bd == ct.b16 {
777 clen = int(bigen.Uint16(d.r.readx(2)))
778 } else if bd == ct.b32 {
779 clen = int(bigen.Uint32(d.r.readx(4)))
780 } else if (ct.bFixMin & bd) == ct.bFixMin {
781 clen = int(ct.bFixMin ^ bd)
783 d.d.errorf("cannot read container length: %s: hex: %x, decimal: %d", msgBadDesc, bd, bd)
790 func (d *msgpackDecDriver) ReadMapStart() int {
794 return d.readContainerLen(msgpackContainerMap)
797 func (d *msgpackDecDriver) ReadArrayStart() int {
801 return d.readContainerLen(msgpackContainerList)
804 func (d *msgpackDecDriver) readExtLen() (clen int) {
807 clen = -1 // to represent nil
819 clen = int(d.r.readn1())
821 clen = int(bigen.Uint16(d.r.readx(2)))
823 clen = int(bigen.Uint32(d.r.readx(4)))
825 d.d.errorf("decoding ext bytes: found unexpected byte: %x", d.bd)
831 func (d *msgpackDecDriver) DecodeTime() (t time.Time) {
832 // decode time from string bytes or ext
841 switch d.ContainerType() {
842 case valueTypeBytes, valueTypeString:
843 clen = d.readContainerLen(msgpackContainerStr)
845 // expect to see mpFixExt4,-1 OR mpFixExt8,-1 OR mpExt8,12,-1
848 if d.bd == mpFixExt4 && b2 == mpTimeExtTagU {
850 } else if d.bd == mpFixExt8 && b2 == mpTimeExtTagU {
852 } else if d.bd == mpExt8 && b2 == 12 && d.r.readn1() == mpTimeExtTagU {
855 d.d.errorf("invalid bytes for decoding time as extension: got 0x%x, 0x%x", d.bd, b2)
859 return d.decodeTime(clen)
862 func (d *msgpackDecDriver) decodeTime(clen int) (t time.Time) {
863 // bs = d.r.readx(clen)
867 t = time.Unix(int64(bigen.Uint32(d.r.readx(4))), 0).UTC()
869 tv := bigen.Uint64(d.r.readx(8))
870 t = time.Unix(int64(tv&0x00000003ffffffff), int64(tv>>34)).UTC()
872 nsec := bigen.Uint32(d.r.readx(4))
873 sec := bigen.Uint64(d.r.readx(8))
874 t = time.Unix(int64(sec), int64(nsec)).UTC()
876 d.d.errorf("invalid length of bytes for decoding time - expecting 4 or 8 or 12, got %d", clen)
882 func (d *msgpackDecDriver) DecodeExt(rv interface{}, xtag uint64, ext Ext) (realxtag uint64) {
884 d.d.errorf("ext: tag must be <= 0xff; got: %v", xtag)
887 realxtag1, xbs := d.decodeExtV(ext != nil, uint8(xtag))
888 realxtag = uint64(realxtag1)
892 re.Data = detachZeroCopyBytes(d.br, re.Data, xbs)
899 func (d *msgpackDecDriver) decodeExtV(verifyTag bool, tag byte) (xtag byte, xbs []byte) {
904 if xbd == mpBin8 || xbd == mpBin16 || xbd == mpBin32 {
905 xbs = d.DecodeBytes(nil, true)
906 } else if xbd == mpStr8 || xbd == mpStr16 || xbd == mpStr32 ||
907 (xbd >= mpFixStrMin && xbd <= mpFixStrMax) {
908 xbs = d.DecodeStringAsBytes()
910 clen := d.readExtLen()
912 if verifyTag && xtag != tag {
913 d.d.errorf("wrong extension tag - got %b, expecting %v", xtag, tag)
917 xbs = d.r.readx(clen)
919 xbs = decByteSlice(d.r, clen, d.d.h.MaxInitLen, d.d.b[:])
926 //--------------------------------------------------
928 //MsgpackHandle is a Handle for the Msgpack Schema-Free Encoding Format.
929 type MsgpackHandle struct {
932 // RawToString controls how raw bytes are decoded into a nil interface{}.
935 // NoFixedNum says to output all signed integers as 2-bytes, never as 1-byte fixednum.
938 // WriteExt flag supports encoding configured extensions with extension tags.
939 // It also controls whether other elements of the new spec are encoded (ie Str8).
941 // With WriteExt=false, configured extensions are serialized as raw bytes
942 // and Str8 is not encoded.
944 // A stream can still be decoded into a typed value, provided an appropriate value
945 // is provided, but the type cannot be inferred from the stream. If no appropriate
946 // type is provided (e.g. decoding into a nil interface{}), you get back
947 // a []byte or string based on the setting of RawToString.
950 // PositiveIntUnsigned says to encode positive integers as unsigned.
951 PositiveIntUnsigned bool
956 // _ [1]uint64 // padding
959 // Name returns the name of the handle: msgpack
960 func (h *MsgpackHandle) Name() string { return "msgpack" }
962 // SetBytesExt sets an extension
963 func (h *MsgpackHandle) SetBytesExt(rt reflect.Type, tag uint64, ext BytesExt) (err error) {
964 return h.SetExt(rt, tag, &extWrapper{ext, interfaceExtFailer{}})
967 func (h *MsgpackHandle) newEncDriver(e *Encoder) encDriver {
968 return &msgpackEncDriver{e: e, w: e.w, h: h}
971 func (h *MsgpackHandle) newDecDriver(d *Decoder) decDriver {
972 return &msgpackDecDriver{d: d, h: h, r: d.r, br: d.bytes}
975 func (e *msgpackEncDriver) reset() {
979 func (d *msgpackDecDriver) reset() {
980 d.r, d.br = d.d.r, d.d.bytes
981 d.bd, d.bdRead = 0, false
984 //--------------------------------------------------
986 type msgpackSpecRpcCodec struct {
990 // /////////////// Spec RPC Codec ///////////////////
991 func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error {
992 // WriteRequest can write to both a Go service, and other services that do
993 // not abide by the 1 argument rule of a Go service.
994 // We discriminate based on if the body is a MsgpackSpecRpcMultiArgs
995 var bodyArr []interface{}
996 if m, ok := body.(MsgpackSpecRpcMultiArgs); ok {
997 bodyArr = ([]interface{})(m)
999 bodyArr = []interface{}{body}
1001 r2 := []interface{}{0, uint32(r.Seq), r.ServiceMethod, bodyArr}
1002 return c.write(r2, nil, false)
1005 func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error {
1010 if moe != nil && body != nil {
1013 r2 := []interface{}{1, uint32(r.Seq), moe, body}
1014 return c.write(r2, nil, false)
1017 func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error {
1018 return c.parseCustomHeader(1, &r.Seq, &r.Error)
1021 func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error {
1022 return c.parseCustomHeader(0, &r.Seq, &r.ServiceMethod)
1025 func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error {
1026 if body == nil { // read and discard
1029 bodyArr := []interface{}{body}
1030 return c.read(&bodyArr)
1033 func (c *msgpackSpecRpcCodec) parseCustomHeader(expectTypeByte byte, msgid *uint64, methodOrError *string) (err error) {
1034 if cls := c.cls.load(); cls.closed {
1038 // We read the response header by hand
1039 // so that the body can be decoded on its own from the stream at a later time.
1041 const fia byte = 0x94 //four item array descriptor value
1042 // Not sure why the panic of EOF is swallowed above.
1043 // if bs1 := c.dec.r.readn1(); bs1 != fia {
1044 // err = fmt.Errorf("Unexpected value for array descriptor: Expecting %v. Received %v", fia, bs1)
1050 n, err = c.r.Read(ba[:])
1061 err = fmt.Errorf("not array - %s %x/%s", msgBadDesc, b, mpdesc(b))
1065 if b != expectTypeByte {
1066 err = fmt.Errorf("%s - expecting %v but got %x/%s",
1067 msgBadDesc, expectTypeByte, b, mpdesc(b))
1071 err = c.read(methodOrError)
1079 //--------------------------------------------------
1081 // msgpackSpecRpc is the implementation of Rpc that uses custom communication protocol
1082 // as defined in the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md
1083 type msgpackSpecRpc struct{}
1085 // MsgpackSpecRpc implements Rpc using the communication protocol defined in
1086 // the msgpack spec at https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md .
1088 // See GoRpc documentation, for information on buffering for better performance.
1089 var MsgpackSpecRpc msgpackSpecRpc
1091 func (x msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) rpc.ServerCodec {
1092 return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
1095 func (x msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) rpc.ClientCodec {
1096 return &msgpackSpecRpcCodec{newRPCCodec(conn, h)}
1099 var _ decDriver = (*msgpackDecDriver)(nil)
1100 var _ encDriver = (*msgpackEncDriver)(nil)