2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gob
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// An Encoder manages the transmission of type and data information to the
|
|
|
|
// other side of a connection.
|
|
|
|
type Encoder struct {
|
|
|
|
mutex sync.Mutex // each item must be sent atomically
|
2011-03-17 00:05:44 +01:00
|
|
|
w []io.Writer // where to send the data
|
2010-12-03 05:34:57 +01:00
|
|
|
sent map[reflect.Type]typeId // which types we've already sent
|
|
|
|
countState *encoderState // stage for writing counts
|
2011-03-25 00:46:17 +01:00
|
|
|
freeList *encoderState // list of free encoderStates; avoids reallocation
|
2010-12-03 05:34:57 +01:00
|
|
|
buf []byte // for collecting the output.
|
2011-03-25 00:46:17 +01:00
|
|
|
byteBuf bytes.Buffer // buffer for top-level encoderState
|
2010-12-03 05:34:57 +01:00
|
|
|
err os.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEncoder returns a new encoder that will transmit on the io.Writer.
|
|
|
|
func NewEncoder(w io.Writer) *Encoder {
|
|
|
|
enc := new(Encoder)
|
2011-03-17 00:05:44 +01:00
|
|
|
enc.w = []io.Writer{w}
|
2010-12-03 05:34:57 +01:00
|
|
|
enc.sent = make(map[reflect.Type]typeId)
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.countState = enc.newEncoderState(new(bytes.Buffer))
|
2010-12-03 05:34:57 +01:00
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// writer() returns the innermost writer the encoder is using
|
|
|
|
func (enc *Encoder) writer() io.Writer {
|
|
|
|
return enc.w[len(enc.w)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// pushWriter adds a writer to the encoder.
|
|
|
|
func (enc *Encoder) pushWriter(w io.Writer) {
|
|
|
|
enc.w = append(enc.w, w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// popWriter pops the innermost writer.
|
|
|
|
func (enc *Encoder) popWriter() {
|
|
|
|
enc.w = enc.w[0 : len(enc.w)-1]
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
func (enc *Encoder) badType(rt reflect.Type) {
|
2011-09-16 17:47:21 +02:00
|
|
|
enc.setError(os.NewError("gob: can't encode type " + rt.String()))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (enc *Encoder) setError(err os.Error) {
|
|
|
|
if enc.err == nil { // remember the first.
|
|
|
|
enc.err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// writeMessage sends the data item preceded by a unsigned count of its length.
|
|
|
|
func (enc *Encoder) writeMessage(w io.Writer, b *bytes.Buffer) {
|
|
|
|
enc.countState.encodeUint(uint64(b.Len()))
|
2010-12-03 05:34:57 +01:00
|
|
|
// Build the buffer.
|
|
|
|
countLen := enc.countState.b.Len()
|
2011-03-17 00:05:44 +01:00
|
|
|
total := countLen + b.Len()
|
2010-12-03 05:34:57 +01:00
|
|
|
if total > len(enc.buf) {
|
|
|
|
enc.buf = make([]byte, total+1000) // extra for growth
|
|
|
|
}
|
|
|
|
// Place the length before the data.
|
|
|
|
// TODO(r): avoid the extra copy here.
|
|
|
|
enc.countState.b.Read(enc.buf[0:countLen])
|
|
|
|
// Now the data.
|
2011-03-17 00:05:44 +01:00
|
|
|
b.Read(enc.buf[countLen:total])
|
2010-12-03 05:34:57 +01:00
|
|
|
// Write the data.
|
2011-03-17 00:05:44 +01:00
|
|
|
_, err := w.Write(enc.buf[0:total])
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
enc.setError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// sendActualType sends the requested type, without further investigation, unless
|
|
|
|
// it's been sent before.
|
|
|
|
func (enc *Encoder) sendActualType(w io.Writer, state *encoderState, ut *userTypeInfo, actual reflect.Type) (sent bool) {
|
|
|
|
if _, alreadySent := enc.sent[actual]; alreadySent {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
typeLock.Lock()
|
|
|
|
info, err := getTypeInfo(ut)
|
|
|
|
typeLock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
enc.setError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Send the pair (-id, type)
|
|
|
|
// Id:
|
|
|
|
state.encodeInt(-int64(info.id))
|
|
|
|
// Type:
|
2011-05-20 02:18:15 +02:00
|
|
|
enc.encode(state.b, reflect.ValueOf(info.wire), wireTypeUserInfo)
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.writeMessage(w, state.b)
|
|
|
|
if enc.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember we've sent this type, both what the user gave us and the base type.
|
|
|
|
enc.sent[ut.base] = info.id
|
|
|
|
if ut.user != ut.base {
|
|
|
|
enc.sent[ut.user] = info.id
|
|
|
|
}
|
|
|
|
// Now send the inner types
|
2011-05-20 02:18:15 +02:00
|
|
|
switch st := actual; st.Kind() {
|
|
|
|
case reflect.Struct:
|
2011-03-25 00:46:17 +01:00
|
|
|
for i := 0; i < st.NumField(); i++ {
|
|
|
|
enc.sendType(w, state, st.Field(i).Type)
|
|
|
|
}
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
enc.sendType(w, state, st.Elem())
|
|
|
|
case reflect.Map:
|
|
|
|
enc.sendType(w, state, st.Key())
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.sendType(w, state, st.Elem())
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendType sends the type info to the other side, if necessary.
|
2011-03-17 00:05:44 +01:00
|
|
|
func (enc *Encoder) sendType(w io.Writer, state *encoderState, origt reflect.Type) (sent bool) {
|
|
|
|
ut := userType(origt)
|
2011-03-25 00:46:17 +01:00
|
|
|
if ut.isGobEncoder {
|
|
|
|
// The rules are different: regardless of the underlying type's representation,
|
|
|
|
// we need to tell the other side that this exact type is a GobEncoder.
|
|
|
|
return enc.sendActualType(w, state, ut, ut.user)
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
// It's a concrete value, so drill down to the base type.
|
2011-05-20 02:18:15 +02:00
|
|
|
switch rt := ut.base; rt.Kind() {
|
2010-12-03 05:34:57 +01:00
|
|
|
default:
|
|
|
|
// Basic types and interfaces do not need to be described.
|
|
|
|
return
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Slice:
|
2010-12-03 05:34:57 +01:00
|
|
|
// If it's []uint8, don't send; it's considered basic.
|
|
|
|
if rt.Elem().Kind() == reflect.Uint8 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Otherwise we do send.
|
|
|
|
break
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Array:
|
2010-12-03 05:34:57 +01:00
|
|
|
// arrays must be sent so we know their lengths and element types.
|
|
|
|
break
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Map:
|
2010-12-03 05:34:57 +01:00
|
|
|
// maps must be sent so we know their lengths and key/value types.
|
|
|
|
break
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Struct:
|
2010-12-03 05:34:57 +01:00
|
|
|
// structs must be sent so we know their fields.
|
|
|
|
break
|
2011-05-20 02:18:15 +02:00
|
|
|
case reflect.Chan, reflect.Func:
|
2010-12-03 05:34:57 +01:00
|
|
|
// Probably a bad field in a struct.
|
|
|
|
enc.badType(rt)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
return enc.sendActualType(w, state, ut, ut.base)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode transmits the data item represented by the empty interface value,
|
|
|
|
// guaranteeing that all necessary type information has been transmitted first.
|
|
|
|
func (enc *Encoder) Encode(e interface{}) os.Error {
|
2011-05-20 02:18:15 +02:00
|
|
|
return enc.EncodeValue(reflect.ValueOf(e))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// sendTypeDescriptor makes sure the remote side knows about this type.
|
2010-12-03 05:34:57 +01:00
|
|
|
// It will send a descriptor if this is the first time the type has been
|
2011-03-17 00:05:44 +01:00
|
|
|
// sent.
|
|
|
|
func (enc *Encoder) sendTypeDescriptor(w io.Writer, state *encoderState, ut *userTypeInfo) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Make sure the type is known to the other side.
|
2011-03-25 00:46:17 +01:00
|
|
|
// First, have we already sent this type?
|
|
|
|
rt := ut.base
|
|
|
|
if ut.isGobEncoder {
|
|
|
|
rt = ut.user
|
|
|
|
}
|
|
|
|
if _, alreadySent := enc.sent[rt]; !alreadySent {
|
2010-12-03 05:34:57 +01:00
|
|
|
// No, so send it.
|
2011-03-25 00:46:17 +01:00
|
|
|
sent := enc.sendType(w, state, rt)
|
2010-12-03 05:34:57 +01:00
|
|
|
if enc.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// If the type info has still not been transmitted, it means we have
|
|
|
|
// a singleton basic type (int, []byte etc.) at top level. We don't
|
|
|
|
// need to send the type info but we do need to update enc.sent.
|
|
|
|
if !sent {
|
|
|
|
typeLock.Lock()
|
2011-03-25 00:46:17 +01:00
|
|
|
info, err := getTypeInfo(ut)
|
2010-12-03 05:34:57 +01:00
|
|
|
typeLock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
enc.setError(err)
|
|
|
|
return
|
|
|
|
}
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.sent[rt] = info.id
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-17 00:05:44 +01:00
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// sendTypeId sends the id, which must have already been defined.
|
|
|
|
func (enc *Encoder) sendTypeId(state *encoderState, ut *userTypeInfo) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// Identify the type of this top-level value.
|
2011-03-17 00:05:44 +01:00
|
|
|
state.encodeInt(int64(enc.sent[ut.base]))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeValue transmits the data item represented by the reflection value,
|
|
|
|
// guaranteeing that all necessary type information has been transmitted first.
|
|
|
|
func (enc *Encoder) EncodeValue(value reflect.Value) os.Error {
|
|
|
|
// Make sure we're single-threaded through here, so multiple
|
|
|
|
// goroutines can share an encoder.
|
|
|
|
enc.mutex.Lock()
|
|
|
|
defer enc.mutex.Unlock()
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
// Remove any nested writers remaining due to previous errors.
|
|
|
|
enc.w = enc.w[0:1]
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
ut, err := validUserType(value.Type())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-17 00:05:44 +01:00
|
|
|
enc.err = nil
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.byteBuf.Reset()
|
|
|
|
state := enc.newEncoderState(&enc.byteBuf)
|
2011-03-17 00:05:44 +01:00
|
|
|
|
|
|
|
enc.sendTypeDescriptor(enc.writer(), state, ut)
|
|
|
|
enc.sendTypeId(state, ut)
|
2010-12-03 05:34:57 +01:00
|
|
|
if enc.err != nil {
|
|
|
|
return enc.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode the object.
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.encode(state.b, value, ut)
|
|
|
|
if enc.err == nil {
|
2011-03-17 00:05:44 +01:00
|
|
|
enc.writeMessage(enc.writer(), state.b)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-03-25 00:46:17 +01:00
|
|
|
enc.freeEncoderState(state)
|
2010-12-03 05:34:57 +01:00
|
|
|
return enc.err
|
|
|
|
}
|