frame.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. flagCompress uint8 = 1
  34. headerSize = 8
  35. )
  36. type frame []byte
  37. func (f *frame) writeInt(v int32) {
  38. p := f.grow(4)
  39. (*f)[p] = byte(v >> 24)
  40. (*f)[p+1] = byte(v >> 16)
  41. (*f)[p+2] = byte(v >> 8)
  42. (*f)[p+3] = byte(v)
  43. }
  44. func (f *frame) writeShort(v uint16) {
  45. p := f.grow(2)
  46. (*f)[p] = byte(v >> 8)
  47. (*f)[p+1] = byte(v)
  48. }
  49. func (f *frame) writeString(v string) {
  50. f.writeShort(uint16(len(v)))
  51. p := f.grow(len(v))
  52. copy((*f)[p:], v)
  53. }
  54. func (f *frame) writeLongString(v string) {
  55. f.writeInt(int32(len(v)))
  56. p := f.grow(len(v))
  57. copy((*f)[p:], v)
  58. }
  59. func (f *frame) writeUUID() {
  60. }
  61. func (f *frame) writeStringList(v []string) {
  62. f.writeShort(uint16(len(v)))
  63. for i := range v {
  64. f.writeString(v[i])
  65. }
  66. }
  67. func (f *frame) writeByte(v byte) {
  68. p := f.grow(1)
  69. (*f)[p] = v
  70. }
  71. func (f *frame) writeBytes(v []byte) {
  72. if v == nil {
  73. f.writeInt(-1)
  74. return
  75. }
  76. f.writeInt(int32(len(v)))
  77. p := f.grow(len(v))
  78. copy((*f)[p:], v)
  79. }
  80. func (f *frame) writeShortBytes(v []byte) {
  81. f.writeShort(uint16(len(v)))
  82. p := f.grow(len(v))
  83. copy((*f)[p:], v)
  84. }
  85. func (f *frame) writeInet(ip net.IP, port int) {
  86. p := f.grow(1 + len(ip))
  87. (*f)[p] = byte(len(ip))
  88. copy((*f)[p+1:], ip)
  89. f.writeInt(int32(port))
  90. }
  91. func (f *frame) writeStringMap(v map[string]string) {
  92. f.writeShort(uint16(len(v)))
  93. for key, value := range v {
  94. f.writeString(key)
  95. f.writeString(value)
  96. }
  97. }
  98. func (f *frame) writeStringMultimap(v map[string][]string) {
  99. f.writeShort(uint16(len(v)))
  100. for key, values := range v {
  101. f.writeString(key)
  102. f.writeStringList(values)
  103. }
  104. }
  105. func (f *frame) setHeader(version, flags, stream, opcode uint8) {
  106. (*f)[0] = version
  107. (*f)[1] = flags
  108. (*f)[2] = stream
  109. (*f)[3] = opcode
  110. }
  111. func (f *frame) setLength(length int) {
  112. (*f)[4] = byte(length >> 24)
  113. (*f)[5] = byte(length >> 16)
  114. (*f)[6] = byte(length >> 8)
  115. (*f)[7] = byte(length)
  116. }
  117. func (f *frame) Length() int {
  118. return int((*f)[4])<<24 | int((*f)[5])<<16 | int((*f)[6])<<8 | int((*f)[7])
  119. }
  120. func (f *frame) grow(n int) int {
  121. if len(*f)+n >= cap(*f) {
  122. buf := make(frame, len(*f), len(*f)*2+n)
  123. copy(buf, *f)
  124. *f = buf
  125. }
  126. p := len(*f)
  127. *f = (*f)[:p+n]
  128. return p
  129. }
  130. func (f *frame) skipHeader() {
  131. *f = (*f)[headerSize:]
  132. }
  133. func (f *frame) readInt() int {
  134. if len(*f) < 4 {
  135. panic(ErrProtocol)
  136. }
  137. v := uint32((*f)[0])<<24 | uint32((*f)[1])<<16 | uint32((*f)[2])<<8 | uint32((*f)[3])
  138. *f = (*f)[4:]
  139. return int(int32(v))
  140. }
  141. func (f *frame) readShort() uint16 {
  142. if len(*f) < 2 {
  143. panic(ErrProtocol)
  144. }
  145. v := uint16((*f)[0])<<8 | uint16((*f)[1])
  146. *f = (*f)[2:]
  147. return v
  148. }
  149. func (f *frame) readString() string {
  150. n := int(f.readShort())
  151. if len(*f) < n {
  152. panic(ErrProtocol)
  153. }
  154. v := string((*f)[:n])
  155. *f = (*f)[n:]
  156. return v
  157. }
  158. func (f *frame) readLongString() string {
  159. n := f.readInt()
  160. if len(*f) < n {
  161. panic(ErrProtocol)
  162. }
  163. v := string((*f)[:n])
  164. *f = (*f)[n:]
  165. return v
  166. }
  167. func (f *frame) readBytes() []byte {
  168. n := f.readInt()
  169. if n < 0 {
  170. return nil
  171. }
  172. if len(*f) < n {
  173. panic(ErrProtocol)
  174. }
  175. v := (*f)[:n]
  176. *f = (*f)[n:]
  177. return v
  178. }
  179. func (f *frame) readShortBytes() []byte {
  180. n := int(f.readShort())
  181. if len(*f) < n {
  182. panic(ErrProtocol)
  183. }
  184. v := (*f)[:n]
  185. *f = (*f)[n:]
  186. return v
  187. }
  188. func (f *frame) readTypeInfo() *TypeInfo {
  189. x := f.readShort()
  190. typ := &TypeInfo{Type: Type(x)}
  191. switch typ.Type {
  192. case TypeCustom:
  193. typ.Custom = f.readString()
  194. case TypeMap:
  195. typ.Key = f.readTypeInfo()
  196. fallthrough
  197. case TypeList, TypeSet:
  198. typ.Elem = f.readTypeInfo()
  199. }
  200. return typ
  201. }
  202. func (f *frame) readMetaData() []ColumnInfo {
  203. flags := f.readInt()
  204. numColumns := f.readInt()
  205. globalKeyspace := ""
  206. globalTable := ""
  207. if flags&1 != 0 {
  208. globalKeyspace = f.readString()
  209. globalTable = f.readString()
  210. }
  211. info := make([]ColumnInfo, numColumns)
  212. for i := 0; i < numColumns; i++ {
  213. info[i].Keyspace = globalKeyspace
  214. info[i].Table = globalTable
  215. if flags&1 == 0 {
  216. info[i].Keyspace = f.readString()
  217. info[i].Table = f.readString()
  218. }
  219. info[i].Name = f.readString()
  220. info[i].TypeInfo = f.readTypeInfo()
  221. }
  222. return info
  223. }
  224. func (f *frame) writeConsistency(c Consistency) {
  225. f.writeShort(consistencyCodes[c])
  226. }
  227. var consistencyCodes = []uint16{
  228. Any: 0x0000,
  229. One: 0x0001,
  230. Two: 0x0002,
  231. Three: 0x0003,
  232. Quorum: 0x0004,
  233. All: 0x0005,
  234. LocalQuorum: 0x0006,
  235. EachQuorum: 0x0007,
  236. Serial: 0x0008,
  237. LocalSerial: 0x0009,
  238. }
  239. type readyFrame struct{}
  240. type resultVoidFrame struct{}
  241. type resultRowsFrame struct {
  242. Columns []ColumnInfo
  243. Rows [][][]byte
  244. PagingState []byte
  245. }
  246. type resultKeyspaceFrame struct {
  247. Keyspace string
  248. }
  249. type resultPreparedFrame struct {
  250. PreparedId []byte
  251. Values []ColumnInfo
  252. }
  253. type errorFrame struct {
  254. Code int
  255. Message string
  256. }
  257. func (e errorFrame) Error() string {
  258. return e.Message
  259. }