encoder.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2009 The Go 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 gob
  5. import (
  6. "errors"
  7. "io"
  8. "reflect"
  9. "sync"
  10. )
  11. // An Encoder manages the transmission of type and data information to the
  12. // other side of a connection. It is safe for concurrent use by multiple
  13. // goroutines.
  14. type Encoder struct {
  15. mutex sync.Mutex // each item must be sent atomically
  16. w []io.Writer // where to send the data
  17. sent map[reflect.Type]typeId // which types we've already sent
  18. countState *encoderState // stage for writing counts
  19. freeList *encoderState // list of free encoderStates; avoids reallocation
  20. byteBuf encBuffer // buffer for top-level encoderState
  21. err error
  22. }
  23. // Before we encode a message, we reserve space at the head of the
  24. // buffer in which to encode its length. This means we can use the
  25. // buffer to assemble the message without another allocation.
  26. const maxLength = 9 // Maximum size of an encoded length.
  27. var spaceForLength = make([]byte, maxLength)
  28. // NewEncoder returns a new encoder that will transmit on the io.Writer.
  29. func NewEncoder(w io.Writer) *Encoder {
  30. enc := new(Encoder)
  31. enc.w = []io.Writer{w}
  32. enc.sent = make(map[reflect.Type]typeId)
  33. enc.countState = enc.newEncoderState(new(encBuffer))
  34. return enc
  35. }
  36. // writer() returns the innermost writer the encoder is using
  37. func (enc *Encoder) writer() io.Writer {
  38. return enc.w[len(enc.w)-1]
  39. }
  40. // pushWriter adds a writer to the encoder.
  41. func (enc *Encoder) pushWriter(w io.Writer) {
  42. enc.w = append(enc.w, w)
  43. }
  44. // popWriter pops the innermost writer.
  45. func (enc *Encoder) popWriter() {
  46. enc.w = enc.w[0 : len(enc.w)-1]
  47. }
  48. func (enc *Encoder) setError(err error) {
  49. if enc.err == nil { // remember the first.
  50. enc.err = err
  51. }
  52. }
  53. // writeMessage sends the data item preceded by a unsigned count of its length.
  54. func (enc *Encoder) writeMessage(w io.Writer, b *encBuffer) {
  55. // Space has been reserved for the length at the head of the message.
  56. // This is a little dirty: we grab the slice from the bytes.Buffer and massage
  57. // it by hand.
  58. message := b.Bytes()
  59. messageLen := len(message) - maxLength
  60. // Length cannot be bigger than the decoder can handle.
  61. if messageLen >= tooBig {
  62. enc.setError(errors.New("gob: encoder: message too big"))
  63. return
  64. }
  65. // Encode the length.
  66. enc.countState.b.Reset()
  67. enc.countState.encodeUint(uint64(messageLen))
  68. // Copy the length to be a prefix of the message.
  69. offset := maxLength - enc.countState.b.Len()
  70. copy(message[offset:], enc.countState.b.Bytes())
  71. // Write the data.
  72. _, err := w.Write(message[offset:])
  73. // Drain the buffer and restore the space at the front for the count of the next message.
  74. b.Reset()
  75. b.Write(spaceForLength)
  76. if err != nil {
  77. enc.setError(err)
  78. }
  79. }
  80. // sendActualType sends the requested type, without further investigation, unless
  81. // it's been sent before.
  82. func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTypeInfo, actual reflect.Type) (sent bool) {
  83. if _, alreadySent := enc.sent[actual]; alreadySent {
  84. return false
  85. }
  86. info, err := getTypeInfo(ut)
  87. if err != nil {
  88. enc.setError(err)
  89. return
  90. }
  91. // Send the pair (-id, type)
  92. // Id:
  93. state.encodeInt(-int64(info.id))
  94. // Type:
  95. enc.encode(state.b, reflect.ValueOf(info.wire), wireTypeUserInfo)
  96. enc.writeMessage(w, state.b)
  97. if enc.err != nil {
  98. return
  99. }
  100. // Remember we've sent this type, both what the user gave us and the base type.
  101. enc.sent[ut.base] = info.id
  102. if ut.user != ut.base {
  103. enc.sent[ut.user] = info.id
  104. }
  105. // Now send the inner types
  106. switch st := actual; st.Kind() {
  107. case reflect.Struct:
  108. for i := 0; i < st.NumField(); i++ {
  109. if isExported(st.Field(i).Name) {
  110. enc.sendType(w, state, st.Field(i).Type)
  111. }
  112. }
  113. case reflect.Array, reflect.Slice:
  114. enc.sendType(w, state, st.Elem())
  115. case reflect.Map:
  116. enc.sendType(w, state, st.Key())
  117. enc.sendType(w, state, st.Elem())
  118. }
  119. return true
  120. }
  121. // sendType sends the type info to the other side, if necessary.
  122. func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
  123. ut := userType(origt)
  124. if ut.externalEnc != 0 {
  125. // The rules are different: regardless of the underlying type's representation,
  126. // we need to tell the other side that the base type is a GobEncoder.
  127. return enc.sendActualType(w, state, ut, ut.base)
  128. }
  129. // It's a concrete value, so drill down to the base type.
  130. switch rt := ut.base; rt.Kind() {
  131. default:
  132. // Basic types and interfaces do not need to be described.
  133. return
  134. case reflect.Slice:
  135. // If it's []uint8, don't send; it's considered basic.
  136. if rt.Elem().Kind() == reflect.Uint8 {
  137. return
  138. }
  139. // Otherwise we do send.
  140. break
  141. case reflect.Array:
  142. // arrays must be sent so we know their lengths and element types.
  143. break
  144. case reflect.Map:
  145. // maps must be sent so we know their lengths and key/value types.
  146. break
  147. case reflect.Struct:
  148. // structs must be sent so we know their fields.
  149. break
  150. case reflect.Chan, reflect.Func:
  151. // If we get here, it's a field of a struct; ignore it.
  152. return
  153. }
  154. return enc.sendActualType(w, state, ut, ut.base)
  155. }
  156. // Encode transmits the data item represented by the empty interface value,
  157. // guaranteeing that all necessary type information has been transmitted first.
  158. // Passing a nil pointer to Encoder will panic, as they cannot be transmitted by gob.
  159. func (enc *Encoder) Encode(e any) error {
  160. return enc.EncodeValue(reflect.ValueOf(e))
  161. }
  162. // sendTypeDescriptor makes sure the remote side knows about this type.
  163. // It will send a descriptor if this is the first time the type has been
  164. // sent.
  165. func (enc *Encoder) sendTypeDescriptor(w io.Writer, state *encoderState, ut *userTypeInfo) {
  166. // Make sure the type is known to the other side.
  167. // First, have we already sent this type?
  168. rt := ut.base
  169. if ut.externalEnc != 0 {
  170. rt = ut.user
  171. }
  172. if _, alreadySent := enc.sent[rt]; !alreadySent {
  173. // No, so send it.
  174. sent := enc.sendType(w, state, rt)
  175. if enc.err != nil {
  176. return
  177. }
  178. // If the type info has still not been transmitted, it means we have
  179. // a singleton basic type (int, []byte etc.) at top level. We don't
  180. // need to send the type info but we do need to update enc.sent.
  181. if !sent {
  182. info, err := getTypeInfo(ut)
  183. if err != nil {
  184. enc.setError(err)
  185. return
  186. }
  187. enc.sent[rt] = info.id
  188. }
  189. }
  190. }
  191. // sendTypeId sends the id, which must have already been defined.
  192. func (enc *Encoder) sendTypeId(state *encoderState, ut *userTypeInfo) {
  193. // Identify the type of this top-level value.
  194. state.encodeInt(int64(enc.sent[ut.base]))
  195. }
  196. // EncodeValue transmits the data item represented by the reflection value,
  197. // guaranteeing that all necessary type information has been transmitted first.
  198. // Passing a nil pointer to EncodeValue will panic, as they cannot be transmitted by gob.
  199. func (enc *Encoder) EncodeValue(value reflect.Value) error {
  200. if value.Kind() == reflect.Invalid {
  201. return errors.New("gob: cannot encode nil value")
  202. }
  203. if value.Kind() == reflect.Pointer && value.IsNil() {
  204. panic("gob: cannot encode nil pointer of type " + value.Type().String())
  205. }
  206. // Make sure we're single-threaded through here, so multiple
  207. // goroutines can share an encoder.
  208. enc.mutex.Lock()
  209. defer enc.mutex.Unlock()
  210. // Remove any nested writers remaining due to previous errors.
  211. enc.w = enc.w[0:1]
  212. ut, err := validUserType(value.Type())
  213. if err != nil {
  214. return err
  215. }
  216. enc.err = nil
  217. enc.byteBuf.Reset()
  218. enc.byteBuf.Write(spaceForLength)
  219. state := enc.newEncoderState(&enc.byteBuf)
  220. enc.sendTypeDescriptor(enc.writer(), state, ut)
  221. enc.sendTypeId(state, ut)
  222. if enc.err != nil {
  223. return enc.err
  224. }
  225. // Encode the object.
  226. enc.encode(state.b, value, ut)
  227. if enc.err == nil {
  228. enc.writeMessage(enc.writer(), state.b)
  229. }
  230. enc.freeEncoderState(state)
  231. return enc.err
  232. }