frame.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "net"
  7. )
  8. const (
  9. protoRequest byte = 0x02
  10. protoResponse byte = 0x82
  11. opError byte = 0x00
  12. opStartup byte = 0x01
  13. opReady byte = 0x02
  14. opAuthenticate byte = 0x03
  15. opOptions byte = 0x05
  16. opSupported byte = 0x06
  17. opQuery byte = 0x07
  18. opResult byte = 0x08
  19. opPrepare byte = 0x09
  20. opExecute byte = 0x0A
  21. opRegister byte = 0x0B
  22. opEvent byte = 0x0C
  23. opBatch byte = 0x0D
  24. opAuthChallenge byte = 0x0E
  25. opAuthResponse byte = 0x0F
  26. opAuthSuccess byte = 0x10
  27. resultKindVoid = 1
  28. resultKindRows = 2
  29. resultKindKeyspace = 3
  30. resultKindPrepared = 4
  31. resultKindSchemaChanged = 5
  32. flagQueryValues uint8 = 1
  33. headerSize = 8
  34. )
  35. type frame []byte
  36. func (f *frame) writeInt(v int32) {
  37. p := f.grow(4)
  38. (*f)[p] = byte(v >> 24)
  39. (*f)[p+1] = byte(v >> 16)
  40. (*f)[p+2] = byte(v >> 8)
  41. (*f)[p+3] = byte(v)
  42. }
  43. func (f *frame) writeShort(v uint16) {
  44. p := f.grow(2)
  45. (*f)[p] = byte(v >> 8)
  46. (*f)[p+1] = byte(v)
  47. }
  48. func (f *frame) writeString(v string) {
  49. f.writeShort(uint16(len(v)))
  50. p := f.grow(len(v))
  51. copy((*f)[p:], v)
  52. }
  53. func (f *frame) writeLongString(v string) {
  54. f.writeInt(int32(len(v)))
  55. p := f.grow(len(v))
  56. copy((*f)[p:], v)
  57. }
  58. func (f *frame) writeUUID() {
  59. }
  60. func (f *frame) writeStringList(v []string) {
  61. f.writeShort(uint16(len(v)))
  62. for i := range v {
  63. f.writeString(v[i])
  64. }
  65. }
  66. func (f *frame) writeByte(v byte) {
  67. p := f.grow(1)
  68. (*f)[p] = v
  69. }
  70. func (f *frame) writeBytes(v []byte) {
  71. if v == nil {
  72. f.writeInt(-1)
  73. return
  74. }
  75. f.writeInt(int32(len(v)))
  76. p := f.grow(len(v))
  77. copy((*f)[p:], v)
  78. }
  79. func (f *frame) writeShortBytes(v []byte) {
  80. f.writeShort(uint16(len(v)))
  81. p := f.grow(len(v))
  82. copy((*f)[p:], v)
  83. }
  84. func (f *frame) writeInet(ip net.IP, port int) {
  85. p := f.grow(1 + len(ip))
  86. (*f)[p] = byte(len(ip))
  87. copy((*f)[p+1:], ip)
  88. f.writeInt(int32(port))
  89. }
  90. func (f *frame) writeStringMap(v map[string]string) {
  91. f.writeShort(uint16(len(v)))
  92. for key, value := range v {
  93. f.writeString(key)
  94. f.writeString(value)
  95. }
  96. }
  97. func (f *frame) writeStringMultimap(v map[string][]string) {
  98. f.writeShort(uint16(len(v)))
  99. for key, values := range v {
  100. f.writeString(key)
  101. f.writeStringList(values)
  102. }
  103. }
  104. func (f *frame) setHeader(version, flags, stream, opcode uint8) {
  105. (*f)[0] = version
  106. (*f)[1] = flags
  107. (*f)[2] = stream
  108. (*f)[3] = opcode
  109. }
  110. func (f *frame) setLength(length int) {
  111. (*f)[4] = byte(length >> 24)
  112. (*f)[5] = byte(length >> 16)
  113. (*f)[6] = byte(length >> 8)
  114. (*f)[7] = byte(length)
  115. }
  116. func (f *frame) Length() int {
  117. return int((*f)[4])<<24 | int((*f)[5])<<16 | int((*f)[6])<<8 | int((*f)[7])
  118. }
  119. func (f *frame) grow(n int) int {
  120. if len(*f)+n >= cap(*f) {
  121. buf := make(frame, len(*f), len(*f)*2+n)
  122. copy(buf, *f)
  123. *f = buf
  124. }
  125. p := len(*f)
  126. *f = (*f)[:p+n]
  127. return p
  128. }
  129. func (f *frame) skipHeader() {
  130. *f = (*f)[headerSize:]
  131. }
  132. func (f *frame) readInt() int {
  133. if len(*f) < 4 {
  134. panic(ErrProtocol)
  135. }
  136. v := uint32((*f)[0])<<24 | uint32((*f)[1])<<16 | uint32((*f)[2])<<8 | uint32((*f)[3])
  137. *f = (*f)[4:]
  138. return int(int32(v))
  139. }
  140. func (f *frame) readShort() uint16 {
  141. if len(*f) < 2 {
  142. panic(ErrProtocol)
  143. }
  144. v := uint16((*f)[0])<<8 | uint16((*f)[1])
  145. *f = (*f)[2:]
  146. return v
  147. }
  148. func (f *frame) readString() string {
  149. n := int(f.readShort())
  150. if len(*f) < n {
  151. panic(ErrProtocol)
  152. }
  153. v := string((*f)[:n])
  154. *f = (*f)[n:]
  155. return v
  156. }
  157. func (f *frame) readLongString() string {
  158. n := f.readInt()
  159. if len(*f) < n {
  160. panic(ErrProtocol)
  161. }
  162. v := string((*f)[:n])
  163. *f = (*f)[n:]
  164. return v
  165. }
  166. func (f *frame) readBytes() []byte {
  167. n := f.readInt()
  168. if n < 0 {
  169. return nil
  170. }
  171. if len(*f) < n {
  172. panic(ErrProtocol)
  173. }
  174. v := (*f)[:n]
  175. *f = (*f)[n:]
  176. return v
  177. }
  178. func (f *frame) readShortBytes() []byte {
  179. n := int(f.readShort())
  180. if len(*f) < n {
  181. panic(ErrProtocol)
  182. }
  183. v := (*f)[:n]
  184. *f = (*f)[n:]
  185. return v
  186. }
  187. func (f *frame) readTypeInfo() *TypeInfo {
  188. x := f.readShort()
  189. typ := &TypeInfo{Type: Type(x)}
  190. switch typ.Type {
  191. case TypeCustom:
  192. typ.Custom = f.readString()
  193. case TypeMap:
  194. typ.Key = f.readTypeInfo()
  195. fallthrough
  196. case TypeList, TypeSet:
  197. typ.Elem = f.readTypeInfo()
  198. }
  199. return typ
  200. }
  201. func (f *frame) readMetaData() []ColumnInfo {
  202. flags := f.readInt()
  203. numColumns := f.readInt()
  204. globalKeyspace := ""
  205. globalTable := ""
  206. if flags&1 != 0 {
  207. globalKeyspace = f.readString()
  208. globalTable = f.readString()
  209. }
  210. info := make([]ColumnInfo, numColumns)
  211. for i := 0; i < numColumns; i++ {
  212. info[i].Keyspace = globalKeyspace
  213. info[i].Table = globalTable
  214. if flags&1 == 0 {
  215. info[i].Keyspace = f.readString()
  216. info[i].Table = f.readString()
  217. }
  218. info[i].Name = f.readString()
  219. info[i].TypeInfo = f.readTypeInfo()
  220. }
  221. return info
  222. }
  223. func (f *frame) writeConsistency(c Consistency) {
  224. f.writeShort(consistencyCodes[c])
  225. }
  226. var consistencyCodes = []uint16{
  227. Any: 0x0000,
  228. One: 0x0001,
  229. Two: 0x0002,
  230. Three: 0x0003,
  231. Quorum: 0x0004,
  232. All: 0x0005,
  233. LocalQuorum: 0x0006,
  234. EachQuorum: 0x0007,
  235. Serial: 0x0008,
  236. LocalSerial: 0x0009,
  237. }
  238. func decodeFrame(f frame) (rval interface{}, err error) {
  239. defer func() {
  240. if r := recover(); r != nil {
  241. if e, ok := r.(error); ok && e == ErrProtocol {
  242. err = e
  243. return
  244. }
  245. panic(r)
  246. }
  247. }()
  248. if len(f) < headerSize || (f[0] != 1|flagResponse && f[0] != 2|flagResponse) {
  249. return nil, ErrProtocol
  250. }
  251. switch f[3] {
  252. case opReady:
  253. return readyFrame{}, nil
  254. case opResult:
  255. f.skipHeader()
  256. switch kind := f.readInt(); kind {
  257. case resultKindVoid:
  258. return resultVoidFrame{}, nil
  259. case resultKindRows:
  260. columns := f.readMetaData()
  261. numRows := f.readInt()
  262. values := make([][]byte, numRows*len(columns))
  263. for i := 0; i < len(values); i++ {
  264. values[i] = f.readBytes()
  265. }
  266. rows := make([][][]byte, numRows)
  267. for i := 0; i < len(values); i += len(columns) {
  268. rows[i] = values[i : i+len(columns)]
  269. }
  270. return resultRowsFrame{columns, rows, nil}, nil
  271. case resultKindKeyspace:
  272. keyspace := f.readString()
  273. return resultKeyspaceFrame{keyspace}, nil
  274. case resultKindPrepared:
  275. id := f.readShortBytes()
  276. values := f.readMetaData()
  277. return resultPreparedFrame{id, values}, nil
  278. case resultKindSchemaChanged:
  279. return resultVoidFrame{}, nil
  280. default:
  281. return nil, ErrProtocol
  282. }
  283. case opError:
  284. f.skipHeader()
  285. code := f.readInt()
  286. msg := f.readString()
  287. return errorFrame{code, msg}, nil
  288. default:
  289. return nil, ErrProtocol
  290. }
  291. }
  292. type readyFrame struct{}
  293. type resultVoidFrame struct{}
  294. type resultRowsFrame struct {
  295. Columns []ColumnInfo
  296. Rows [][][]byte
  297. PagingState []byte
  298. }
  299. type resultKeyspaceFrame struct {
  300. Keyspace string
  301. }
  302. type resultPreparedFrame struct {
  303. PreparedId []byte
  304. Values []ColumnInfo
  305. }
  306. type errorFrame struct {
  307. Code int
  308. Message string
  309. }
  310. func (e errorFrame) Error() string {
  311. return e.Message
  312. }