2009-07-11 15:45:54 -07: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 (
|
2009-07-15 16:10:17 -07:00
|
|
|
"bytes";
|
2009-07-11 15:45:54 -07:00
|
|
|
"gob";
|
|
|
|
|
"io";
|
|
|
|
|
"os";
|
|
|
|
|
"reflect";
|
|
|
|
|
"sync";
|
|
|
|
|
)
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
// A Decoder manages the receipt of type and data information read from the
|
|
|
|
|
// remote side of a connection.
|
2009-07-11 15:45:54 -07:00
|
|
|
type Decoder struct {
|
2009-07-27 11:02:06 -07:00
|
|
|
mutex sync.Mutex; // each item must be received atomically
|
2009-07-15 16:10:17 -07:00
|
|
|
r io.Reader; // source of the data
|
2009-07-27 11:02:06 -07:00
|
|
|
seen map[typeId] *wireType; // which types we've already seen described
|
2009-07-15 16:10:17 -07:00
|
|
|
state *decodeState; // reads data from in-memory buffer
|
|
|
|
|
countState *decodeState; // reads counts from wire
|
2009-07-16 13:05:46 -07:00
|
|
|
buf []byte;
|
2009-07-15 16:10:17 -07:00
|
|
|
oneByte []byte;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
// NewDecoder returns a new decoder that reads from the io.Reader.
|
2009-07-11 15:45:54 -07:00
|
|
|
func NewDecoder(r io.Reader) *Decoder {
|
|
|
|
|
dec := new(Decoder);
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.r = r;
|
2009-07-27 11:02:06 -07:00
|
|
|
dec.seen = make(map[typeId] *wireType);
|
2009-07-28 17:20:19 -07:00
|
|
|
dec.state = newDecodeState(nil); // buffer set in Decode(); rest is unimportant
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.oneByte = make([]byte, 1);
|
2009-07-11 15:45:54 -07:00
|
|
|
|
|
|
|
|
return dec;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
func (dec *Decoder) recvType(id typeId) {
|
2009-07-11 15:45:54 -07:00
|
|
|
// Have we already seen this type? That's an error
|
|
|
|
|
if wt_, alreadySeen := dec.seen[id]; alreadySeen {
|
|
|
|
|
dec.state.err = os.ErrorString("gob: duplicate type received");
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Type:
|
|
|
|
|
wire := new(wireType);
|
2009-07-16 17:55:16 -07:00
|
|
|
decode(dec.state.b, tWireType, wire);
|
2009-07-11 15:45:54 -07:00
|
|
|
// Remember we've seen this type.
|
|
|
|
|
dec.seen[id] = wire;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-27 11:02:06 -07:00
|
|
|
// Decode reads the next value from the connection and stores
|
|
|
|
|
// it in the data represented by the empty interface value.
|
2009-07-11 15:45:54 -07:00
|
|
|
// The value underlying e must be the correct type for the next
|
2009-07-27 11:02:06 -07:00
|
|
|
// data item received.
|
2009-07-11 15:45:54 -07:00
|
|
|
func (dec *Decoder) Decode(e interface{}) os.Error {
|
|
|
|
|
rt, indir := indirect(reflect.Typeof(e));
|
|
|
|
|
|
|
|
|
|
// Make sure we're single-threaded through here.
|
2009-07-27 11:02:06 -07:00
|
|
|
dec.mutex.Lock();
|
|
|
|
|
defer dec.mutex.Unlock();
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
dec.state.err = nil;
|
|
|
|
|
for {
|
|
|
|
|
// Read a count.
|
2009-07-29 15:10:29 -07:00
|
|
|
var nbytes uint64;
|
|
|
|
|
nbytes, dec.state.err = decodeUintReader(dec.r, dec.oneByte);
|
|
|
|
|
if dec.state.err != nil {
|
|
|
|
|
break;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2009-07-16 13:05:46 -07:00
|
|
|
// Allocate the buffer.
|
|
|
|
|
if nbytes > uint64(len(dec.buf)) {
|
|
|
|
|
dec.buf = make([]byte, nbytes + 1000);
|
|
|
|
|
}
|
|
|
|
|
dec.state.b = bytes.NewBuffer(dec.buf[0:nbytes]);
|
|
|
|
|
|
2009-07-15 16:10:17 -07:00
|
|
|
// Read the data
|
|
|
|
|
var n int;
|
2009-07-29 15:10:29 -07:00
|
|
|
n, dec.state.err = io.ReadFull(dec.r, dec.buf[0:nbytes]);
|
|
|
|
|
if dec.state.err != nil {
|
|
|
|
|
break;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
if n < int(nbytes) {
|
2009-07-29 15:10:29 -07:00
|
|
|
dec.state.err = io.ErrUnexpectedEOF;
|
|
|
|
|
break;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-11 15:45:54 -07:00
|
|
|
// Receive a type id.
|
2009-07-27 11:02:06 -07:00
|
|
|
id := typeId(decodeInt(dec.state));
|
2009-07-15 16:10:17 -07:00
|
|
|
if dec.state.err != nil {
|
2009-07-16 23:01:10 -07:00
|
|
|
break;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
2009-07-11 15:45:54 -07:00
|
|
|
|
2009-07-16 17:55:16 -07:00
|
|
|
// Is it a new type?
|
2009-07-15 16:10:17 -07:00
|
|
|
if id < 0 { // 0 is the error state, handled above
|
|
|
|
|
// If the id is negative, we have a type.
|
|
|
|
|
dec.recvType(-id);
|
|
|
|
|
if dec.state.err != nil {
|
2009-07-16 23:01:10 -07:00
|
|
|
break;
|
2009-07-15 16:10:17 -07:00
|
|
|
}
|
|
|
|
|
continue;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
|
|
|
|
|
2009-07-16 13:05:46 -07:00
|
|
|
// No, it's a value.
|
2009-07-16 23:01:10 -07:00
|
|
|
dec.state.err = decode(dec.state.b, id, e);
|
|
|
|
|
break;
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|
2009-07-16 23:01:10 -07:00
|
|
|
return dec.state.err
|
2009-07-11 15:45:54 -07:00
|
|
|
}
|