binary_protocol.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package thrift
  20. import (
  21. "bytes"
  22. "encoding/binary"
  23. "errors"
  24. "fmt"
  25. "io"
  26. "math"
  27. )
  28. type TBinaryProtocol struct {
  29. trans TRichTransport
  30. origTransport TTransport
  31. reader io.Reader
  32. writer io.Writer
  33. strictRead bool
  34. strictWrite bool
  35. buffer [64]byte
  36. }
  37. type TBinaryProtocolFactory struct {
  38. strictRead bool
  39. strictWrite bool
  40. }
  41. func NewTBinaryProtocolTransport(t TTransport) *TBinaryProtocol {
  42. return NewTBinaryProtocol(t, false, true)
  43. }
  44. func NewTBinaryProtocol(t TTransport, strictRead, strictWrite bool) *TBinaryProtocol {
  45. p := &TBinaryProtocol{origTransport: t, strictRead: strictRead, strictWrite: strictWrite}
  46. if et, ok := t.(TRichTransport); ok {
  47. p.trans = et
  48. } else {
  49. p.trans = NewTRichTransport(t)
  50. }
  51. p.reader = p.trans
  52. p.writer = p.trans
  53. return p
  54. }
  55. func NewTBinaryProtocolFactoryDefault() *TBinaryProtocolFactory {
  56. return NewTBinaryProtocolFactory(false, true)
  57. }
  58. func NewTBinaryProtocolFactory(strictRead, strictWrite bool) *TBinaryProtocolFactory {
  59. return &TBinaryProtocolFactory{strictRead: strictRead, strictWrite: strictWrite}
  60. }
  61. func (p *TBinaryProtocolFactory) GetProtocol(t TTransport) TProtocol {
  62. return NewTBinaryProtocol(t, p.strictRead, p.strictWrite)
  63. }
  64. /**
  65. * Writing Methods
  66. */
  67. func (p *TBinaryProtocol) WriteMessageBegin(name string, typeId TMessageType, seqId int32) error {
  68. if p.strictWrite {
  69. version := uint32(VERSION_1) | uint32(typeId)
  70. e := p.WriteI32(int32(version))
  71. if e != nil {
  72. return e
  73. }
  74. e = p.WriteString(name)
  75. if e != nil {
  76. return e
  77. }
  78. e = p.WriteI32(seqId)
  79. return e
  80. } else {
  81. e := p.WriteString(name)
  82. if e != nil {
  83. return e
  84. }
  85. e = p.WriteByte(int8(typeId))
  86. if e != nil {
  87. return e
  88. }
  89. e = p.WriteI32(seqId)
  90. return e
  91. }
  92. return nil
  93. }
  94. func (p *TBinaryProtocol) WriteMessageEnd() error {
  95. return nil
  96. }
  97. func (p *TBinaryProtocol) WriteStructBegin(name string) error {
  98. return nil
  99. }
  100. func (p *TBinaryProtocol) WriteStructEnd() error {
  101. return nil
  102. }
  103. func (p *TBinaryProtocol) WriteFieldBegin(name string, typeId TType, id int16) error {
  104. e := p.WriteByte(int8(typeId))
  105. if e != nil {
  106. return e
  107. }
  108. e = p.WriteI16(id)
  109. return e
  110. }
  111. func (p *TBinaryProtocol) WriteFieldEnd() error {
  112. return nil
  113. }
  114. func (p *TBinaryProtocol) WriteFieldStop() error {
  115. e := p.WriteByte(STOP)
  116. return e
  117. }
  118. func (p *TBinaryProtocol) WriteMapBegin(keyType TType, valueType TType, size int) error {
  119. e := p.WriteByte(int8(keyType))
  120. if e != nil {
  121. return e
  122. }
  123. e = p.WriteByte(int8(valueType))
  124. if e != nil {
  125. return e
  126. }
  127. e = p.WriteI32(int32(size))
  128. return e
  129. }
  130. func (p *TBinaryProtocol) WriteMapEnd() error {
  131. return nil
  132. }
  133. func (p *TBinaryProtocol) WriteListBegin(elemType TType, size int) error {
  134. e := p.WriteByte(int8(elemType))
  135. if e != nil {
  136. return e
  137. }
  138. e = p.WriteI32(int32(size))
  139. return e
  140. }
  141. func (p *TBinaryProtocol) WriteListEnd() error {
  142. return nil
  143. }
  144. func (p *TBinaryProtocol) WriteSetBegin(elemType TType, size int) error {
  145. e := p.WriteByte(int8(elemType))
  146. if e != nil {
  147. return e
  148. }
  149. e = p.WriteI32(int32(size))
  150. return e
  151. }
  152. func (p *TBinaryProtocol) WriteSetEnd() error {
  153. return nil
  154. }
  155. func (p *TBinaryProtocol) WriteBool(value bool) error {
  156. if value {
  157. return p.WriteByte(1)
  158. }
  159. return p.WriteByte(0)
  160. }
  161. func (p *TBinaryProtocol) WriteByte(value int8) error {
  162. e := p.trans.WriteByte(byte(value))
  163. return NewTProtocolException(e)
  164. }
  165. func (p *TBinaryProtocol) WriteI16(value int16) error {
  166. v := p.buffer[0:2]
  167. binary.BigEndian.PutUint16(v, uint16(value))
  168. _, e := p.writer.Write(v)
  169. return NewTProtocolException(e)
  170. }
  171. func (p *TBinaryProtocol) WriteI32(value int32) error {
  172. v := p.buffer[0:4]
  173. binary.BigEndian.PutUint32(v, uint32(value))
  174. _, e := p.writer.Write(v)
  175. return NewTProtocolException(e)
  176. }
  177. func (p *TBinaryProtocol) WriteI64(value int64) error {
  178. v := p.buffer[0:8]
  179. binary.BigEndian.PutUint64(v, uint64(value))
  180. _, err := p.writer.Write(v)
  181. return NewTProtocolException(err)
  182. }
  183. func (p *TBinaryProtocol) WriteDouble(value float64) error {
  184. return p.WriteI64(int64(math.Float64bits(value)))
  185. }
  186. func (p *TBinaryProtocol) WriteString(value string) error {
  187. e := p.WriteI32(int32(len(value)))
  188. if e != nil {
  189. return e
  190. }
  191. _, err := p.trans.WriteString(value)
  192. return NewTProtocolException(err)
  193. }
  194. func (p *TBinaryProtocol) WriteBinary(value []byte) error {
  195. e := p.WriteI32(int32(len(value)))
  196. if e != nil {
  197. return e
  198. }
  199. _, err := p.writer.Write(value)
  200. return NewTProtocolException(err)
  201. }
  202. /**
  203. * Reading methods
  204. */
  205. func (p *TBinaryProtocol) ReadMessageBegin() (name string, typeId TMessageType, seqId int32, err error) {
  206. size, e := p.ReadI32()
  207. if e != nil {
  208. return "", typeId, 0, NewTProtocolException(e)
  209. }
  210. if size < 0 {
  211. typeId = TMessageType(size & 0x0ff)
  212. version := int64(int64(size) & VERSION_MASK)
  213. if version != VERSION_1 {
  214. return name, typeId, seqId, NewTProtocolExceptionWithType(BAD_VERSION, fmt.Errorf("Bad version in ReadMessageBegin"))
  215. }
  216. name, e = p.ReadString()
  217. if e != nil {
  218. return name, typeId, seqId, NewTProtocolException(e)
  219. }
  220. seqId, e = p.ReadI32()
  221. if e != nil {
  222. return name, typeId, seqId, NewTProtocolException(e)
  223. }
  224. return name, typeId, seqId, nil
  225. }
  226. if p.strictRead {
  227. return name, typeId, seqId, NewTProtocolExceptionWithType(BAD_VERSION, fmt.Errorf("Missing version in ReadMessageBegin"))
  228. }
  229. name, e2 := p.readStringBody(size)
  230. if e2 != nil {
  231. return name, typeId, seqId, e2
  232. }
  233. b, e3 := p.ReadByte()
  234. if e3 != nil {
  235. return name, typeId, seqId, e3
  236. }
  237. typeId = TMessageType(b)
  238. seqId, e4 := p.ReadI32()
  239. if e4 != nil {
  240. return name, typeId, seqId, e4
  241. }
  242. return name, typeId, seqId, nil
  243. }
  244. func (p *TBinaryProtocol) ReadMessageEnd() error {
  245. return nil
  246. }
  247. func (p *TBinaryProtocol) ReadStructBegin() (name string, err error) {
  248. return
  249. }
  250. func (p *TBinaryProtocol) ReadStructEnd() error {
  251. return nil
  252. }
  253. func (p *TBinaryProtocol) ReadFieldBegin() (name string, typeId TType, seqId int16, err error) {
  254. t, err := p.ReadByte()
  255. typeId = TType(t)
  256. if err != nil {
  257. return name, typeId, seqId, err
  258. }
  259. if t != STOP {
  260. seqId, err = p.ReadI16()
  261. }
  262. return name, typeId, seqId, err
  263. }
  264. func (p *TBinaryProtocol) ReadFieldEnd() error {
  265. return nil
  266. }
  267. var invalidDataLength = NewTProtocolExceptionWithType(INVALID_DATA, errors.New("Invalid data length"))
  268. func (p *TBinaryProtocol) ReadMapBegin() (kType, vType TType, size int, err error) {
  269. k, e := p.ReadByte()
  270. if e != nil {
  271. err = NewTProtocolException(e)
  272. return
  273. }
  274. kType = TType(k)
  275. v, e := p.ReadByte()
  276. if e != nil {
  277. err = NewTProtocolException(e)
  278. return
  279. }
  280. vType = TType(v)
  281. size32, e := p.ReadI32()
  282. if e != nil {
  283. err = NewTProtocolException(e)
  284. return
  285. }
  286. if size32 < 0 {
  287. err = invalidDataLength
  288. return
  289. }
  290. size = int(size32)
  291. return kType, vType, size, nil
  292. }
  293. func (p *TBinaryProtocol) ReadMapEnd() error {
  294. return nil
  295. }
  296. func (p *TBinaryProtocol) ReadListBegin() (elemType TType, size int, err error) {
  297. b, e := p.ReadByte()
  298. if e != nil {
  299. err = NewTProtocolException(e)
  300. return
  301. }
  302. elemType = TType(b)
  303. size32, e := p.ReadI32()
  304. if e != nil {
  305. err = NewTProtocolException(e)
  306. return
  307. }
  308. if size32 < 0 {
  309. err = invalidDataLength
  310. return
  311. }
  312. size = int(size32)
  313. return
  314. }
  315. func (p *TBinaryProtocol) ReadListEnd() error {
  316. return nil
  317. }
  318. func (p *TBinaryProtocol) ReadSetBegin() (elemType TType, size int, err error) {
  319. b, e := p.ReadByte()
  320. if e != nil {
  321. err = NewTProtocolException(e)
  322. return
  323. }
  324. elemType = TType(b)
  325. size32, e := p.ReadI32()
  326. if e != nil {
  327. err = NewTProtocolException(e)
  328. return
  329. }
  330. if size32 < 0 {
  331. err = invalidDataLength
  332. return
  333. }
  334. size = int(size32)
  335. return elemType, size, nil
  336. }
  337. func (p *TBinaryProtocol) ReadSetEnd() error {
  338. return nil
  339. }
  340. func (p *TBinaryProtocol) ReadBool() (bool, error) {
  341. b, e := p.ReadByte()
  342. v := true
  343. if b != 1 {
  344. v = false
  345. }
  346. return v, e
  347. }
  348. func (p *TBinaryProtocol) ReadByte() (int8, error) {
  349. v, err := p.trans.ReadByte()
  350. return int8(v), err
  351. }
  352. func (p *TBinaryProtocol) ReadI16() (value int16, err error) {
  353. buf := p.buffer[0:2]
  354. err = p.readAll(buf)
  355. value = int16(binary.BigEndian.Uint16(buf))
  356. return value, err
  357. }
  358. func (p *TBinaryProtocol) ReadI32() (value int32, err error) {
  359. buf := p.buffer[0:4]
  360. err = p.readAll(buf)
  361. value = int32(binary.BigEndian.Uint32(buf))
  362. return value, err
  363. }
  364. func (p *TBinaryProtocol) ReadI64() (value int64, err error) {
  365. buf := p.buffer[0:8]
  366. err = p.readAll(buf)
  367. value = int64(binary.BigEndian.Uint64(buf))
  368. return value, err
  369. }
  370. func (p *TBinaryProtocol) ReadDouble() (value float64, err error) {
  371. buf := p.buffer[0:8]
  372. err = p.readAll(buf)
  373. value = math.Float64frombits(binary.BigEndian.Uint64(buf))
  374. return value, err
  375. }
  376. func (p *TBinaryProtocol) ReadString() (value string, err error) {
  377. size, e := p.ReadI32()
  378. if e != nil {
  379. return "", e
  380. }
  381. if size < 0 {
  382. err = invalidDataLength
  383. return
  384. }
  385. return p.readStringBody(size)
  386. }
  387. func (p *TBinaryProtocol) ReadBinary() ([]byte, error) {
  388. size, e := p.ReadI32()
  389. if e != nil {
  390. return nil, e
  391. }
  392. if size < 0 {
  393. return nil, invalidDataLength
  394. }
  395. if uint64(size) > p.trans.RemainingBytes() {
  396. return nil, invalidDataLength
  397. }
  398. isize := int(size)
  399. buf := make([]byte, isize)
  400. _, err := io.ReadFull(p.trans, buf)
  401. return buf, NewTProtocolException(err)
  402. }
  403. func (p *TBinaryProtocol) Flush() (err error) {
  404. return NewTProtocolException(p.trans.Flush())
  405. }
  406. func (p *TBinaryProtocol) Skip(fieldType TType) (err error) {
  407. return SkipDefaultDepth(p, fieldType)
  408. }
  409. func (p *TBinaryProtocol) Transport() TTransport {
  410. return p.origTransport
  411. }
  412. func (p *TBinaryProtocol) readAll(buf []byte) error {
  413. _, err := io.ReadFull(p.reader, buf)
  414. return NewTProtocolException(err)
  415. }
  416. const readLimit = 32768
  417. func (p *TBinaryProtocol) readStringBody(size int32) (value string, err error) {
  418. if size < 0 {
  419. return "", nil
  420. }
  421. if uint64(size) > p.trans.RemainingBytes() {
  422. return "", invalidDataLength
  423. }
  424. var (
  425. buf bytes.Buffer
  426. e error
  427. b []byte
  428. )
  429. switch {
  430. case int(size) <= len(p.buffer):
  431. b = p.buffer[:size] // avoids allocation for small reads
  432. case int(size) < readLimit:
  433. b = make([]byte, size)
  434. default:
  435. b = make([]byte, readLimit)
  436. }
  437. for size > 0 {
  438. _, e = io.ReadFull(p.trans, b)
  439. buf.Write(b)
  440. if e != nil {
  441. break
  442. }
  443. size -= readLimit
  444. if size < readLimit && size > 0 {
  445. b = b[:size]
  446. }
  447. }
  448. return buf.String(), NewTProtocolException(e)
  449. }