binary_protocol.go 10 KB

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