Exemple #1
0
func gobDecodeList(b *testing.B, l int) {
	var (
		enc *gob.Encoder
		buf bytes.Buffer
		err error
	)
	s := make([]int, l)
	for i, _ := range s {
		s[i] = rand.Int()
	}

	enc = gob.NewEncoder(&buf)
	err = enc.Encode(s)
	if err != nil {
		panic(err.Error())
	}

	var dest []int
	bs := buf.Bytes()
	b.ResetTimer()
	// decode
	for i := 0; i < b.N; i++ {
		nbuffer := bytes.NewBuffer(bs)
		dec := gob.NewDecoder(nbuffer)
		err := dec.Decode(&dest)
		if err != nil {
			panic(err.Error())
		}
	}
}
Exemple #2
0
func gobDecodeMap(b *testing.B, l int, kl int) {
	var (
		enc *gob.Encoder
		buf bytes.Buffer
		err error
	)
	s := make(map[string]int)
	for i := 0; i < l; i++ {
		key := keygen(i, kl)
		s[key] = rand.Int()
	}

	enc = gob.NewEncoder(&buf)
	err = enc.Encode(s)
	if err != nil {
		panic(err.Error())
	}

	var dest map[string]int

	bs := buf.Bytes()
	b.ResetTimer()
	// decode
	for i := 0; i < b.N; i++ {
		nbuffer := bytes.NewBuffer(bs)
		dec := gob.NewDecoder(nbuffer)
		err := dec.Decode(&dest)
		if err != nil {
			panic(err.Error())
		}
	}
}
Exemple #3
0
func SendPeer(encod *gob.Encoder) {
	err := encod.Encode(*MyPublicPeer)

	if err != nil {
		log.Fatal("Error sending Public Peer: ", err)
	}
}
Exemple #4
0
func sendMakeDirectory(encoder *gob.Encoder, directory image.Directory) error {
	imageUpdate := imageserver.ImageUpdate{
		Directory: &directory,
		Operation: imageserver.OperationMakeDirectory,
	}
	return encoder.Encode(imageUpdate)
}
Exemple #5
0
func netSender(netOutStream <-chan HashedInterval, encoder *gob.Encoder, netOutDoneStream chan<- bool) {
	for r := range netOutStream {
		if verboseServer {
			log.Debug("Server.netSender: sending", r.FileInterval)
		}
		err := encoder.Encode(r)
		if err != nil {
			log.Fatal("Protocol encoder error:", err)
			netOutDoneStream <- false
			return
		}
	}

	rEOF := HashedInterval{FileInterval{SparseIgnore, Interval{}}, make([]byte, 0)}
	if rEOF.Len() != 0 {
		log.Fatal("Server.netSender internal error")
	}
	// err := encoder.Encode(HashedInterval{FileInterval{}, make([]byte, 0)})
	err := encoder.Encode(rEOF)
	if err != nil {
		log.Fatal("Protocol encoder error:", err)
		netOutDoneStream <- false
		return
	}
	if verboseServer {
		log.Debug("Server.netSender: finished sending hashes")
	}
	netOutDoneStream <- true
}
Exemple #6
0
func CreateGobsFile(targetFilePath string, recs *[]interface{}, getRecPtr GobRecPtrMaker, gzipped bool) {
	var file, err = os.Create(targetFilePath)
	var gobber *gob.Encoder
	var gzipper *gzip.Writer
	if file != nil {
		defer file.Close()
	}
	if err != nil {
		panic(err)
	}
	if gzipped {
		if gzipper, err = gzip.NewWriterLevel(file, gzip.BestCompression); gzipper != nil {
			defer gzipper.Close()
			gobber = gob.NewEncoder(gzipper)
		}
		if err != nil {
			panic(err)
		}
	} else {
		gobber = gob.NewEncoder(file)
	}
	for _, rec := range *recs {
		if err = gobber.Encode(coreutil.PtrVal(getRecPtr(rec))); err != nil {
			panic(err)
		}
	}
}
Exemple #7
0
func (conn *LocalConnection) exchangeHandshake(localConnID uint64, usingPassword bool, enc *gob.Encoder, dec *gob.Decoder) (*FieldValidator, *[32]byte, error) {
	handshakeSend := map[string]string{
		"PeerNameFlavour": PeerNameFlavour,
		"Name":            conn.local.Name.String(),
		"NickName":        conn.local.NickName,
		"UID":             fmt.Sprint(conn.local.UID),
		"ConnID":          fmt.Sprint(localConnID)}
	handshakeRecv := map[string]string{}
	var public, private *[32]byte
	var err error
	if usingPassword {
		public, private, err = GenerateKeyPair()
		if err != nil {
			return nil, nil, err
		}
		handshakeSend["PublicKey"] = hex.EncodeToString(public[:])
	}
	enc.Encode(handshakeSend)
	if err = dec.Decode(&handshakeRecv); err != nil {
		return nil, nil, err
	}
	fv := NewFieldValidator(handshakeRecv)
	fv.CheckEqual("PeerNameFlavour", PeerNameFlavour)
	return fv, private, nil
}
Exemple #8
0
func readinput(c net.Conn, enc *gob.Encoder) {
	var buf [1]byte
	for {
		n, err := os.Stdin.Read(buf[:])
		if err != nil {
			log.Print("Failed to read stdin.")
			return
		}
		if n != 1 {
			// short read? why?
			continue
		}

		if buf[0] == 0x01 { // ctrl-a
			ctrla := escape(c, enc)
			if !ctrla {
				continue
			}
		}

		if readwrite {
			req := &connReq{Cmd: Write, Input: buf[:]}
			err = enc.Encode(req)
			if err != nil {
				log.Print("Failed to write on connection.")
				// just keep trying
			}
		}
	}
}
Exemple #9
0
func (console *Console) Save(encoder *gob.Encoder) error {
	encoder.Encode(console.RAM)
	console.CPU.Save(encoder)
	console.APU.Save(encoder)
	console.PPU.Save(encoder)
	console.Cartridge.Save(encoder)
	console.Mapper.Save(encoder)
	return encoder.Encode(true)
}
Exemple #10
0
func (cbv *CachedBackendVersion) encodeCacheToGob(enc *gob.Encoder) error {
	cbv.mu.Lock()
	defer cbv.mu.Unlock()

	if err := enc.Encode(cbv.cache); err != nil {
		return fmt.Errorf("Failed to encode cache map: %v", err)
	}
	return nil
}
Exemple #11
0
func (m *gobMarshallingProvider) safeEncode(enc *gob.Encoder, val interface{}) (err error) {
	defer func() {
		if r := recover(); r != nil {
			err = fmt.Errorf("Panic encoding: %v", r)
		}
	}()
	err = enc.Encode(val)
	return
}
Exemple #12
0
func writeTo(buffers [][]*rfc5424.Message, enc *gob.Encoder) error {
	for _, buf := range buffers {
		for _, msg := range buf {
			if err := enc.Encode(msg); err != nil {
				return err
			}
		}
	}
	return nil
}
Exemple #13
0
func clientEncode(encode *gob.Encoder, buf *bufio.Writer, send <-chan *packet.Packet) {
	for msg := range send {
		if err := encode.Encode(msg); err != nil {
			log.Printf("Error encoding packet: %s", err)
		}
		if err := buf.Flush(); err != nil {
			log.Printf("Error writing buffer: %s", err)
		}
	}
}
Exemple #14
0
func (dn *DirNode) EncodeToGob(enc *gob.Encoder) error {
	if err := serializeCommon(enc, dn.GetType(), dn.INodeCommon); err != nil {
		return err
	}

	if err := enc.Encode(dn.Entries); err != nil {
		return fmt.Errorf("Failed to encode Entries: %v", err)
	}

	return nil
}
Exemple #15
0
func (m *Mapper1) Save(encoder *gob.Encoder) error {
	encoder.Encode(m.shiftRegister)
	encoder.Encode(m.control)
	encoder.Encode(m.prgMode)
	encoder.Encode(m.chrMode)
	encoder.Encode(m.prgBank)
	encoder.Encode(m.chrBank0)
	encoder.Encode(m.chrBank1)
	encoder.Encode(m.prgOffsets)
	encoder.Encode(m.chrOffsets)
	return nil
}
Exemple #16
0
func negotiateVersion(e *gob.Encoder, d *gob.Decoder) {
	var c Command
	c.Command = CmdVersion
	c.Params = []string{protocolVersion}
	err := e.Encode(c)
	panicOn(err)
	err = d.Decode(&c)
	if c.Params[0] != protocolVersion {
		panic(fmt.Errorf("Mismatched protocol version %s != %s", c.Params[0], protocolVersion))
	}
	panicOn(err)
}
Exemple #17
0
// interfaceEncode encodes the interface value into the encoder.
func interfaceEncode(enc *gob.Encoder, p Pythagoras) {
	// The encode will fail unless the concrete type has been
	// registered. We registered it in the calling function.

	// Pass pointer to interface so Encode sees (and hence sends) a value of
	// interface type. If we passed p directly it would see the concrete type instead.
	// See the blog post, "The Laws of Reflection" for background.
	err := enc.Encode(&p)
	if err != nil {
		log.Fatal("encode:", err)
	}
}
Exemple #18
0
func gobEncode(data interface{}) {
	var (
		enc *gob.Encoder
		buf bytes.Buffer
		err error
	)
	enc = gob.NewEncoder(&buf)
	err = enc.Encode(data)
	if err != nil {
		panic(err.Error())
	}
}
Exemple #19
0
func (p *IndexDb) SaveConfig(path string, config *IndexDbConfig) error {
	var buf bytes.Buffer
	var enc *gob.Encoder = gob.NewEncoder(&buf)

	var encodeErr error = enc.Encode(config)
	if encodeErr != nil {
		log.Fatal("encode error:", encodeErr)
	}
	file, err := os.Create(path)
	file.Write(buf.Bytes())
	file.Close()
	return err
}
Exemple #20
0
func sendSyncRequest(encoder *gob.Encoder, decoder *gob.Decoder, path string, size int64, salt []byte) bool {
	err := encoder.Encode(requestHeader{requestMagic, syncRequestCode})
	if err != nil {
		log.Fatal("Client protocol encoder error:", err)
		return false
	}
	err = encoder.Encode(path)
	if err != nil {
		log.Fatal("Client protocol encoder error:", err)
		return false
	}
	err = encoder.Encode(size)
	if err != nil {
		log.Fatal("Client protocol encoder error:", err)
		return false
	}
	err = encoder.Encode(salt)
	if err != nil {
		log.Fatal("Client protocol encoder error:", err)
		return false
	}

	var ack bool
	err = decoder.Decode(&ack)
	if err != nil {
		log.Fatal("Client protocol decoder error:", err)
		return false
	}

	return ack
}
Exemple #21
0
func (b *Bin1D) gobEncode(enc *gob.Encoder) error {
	var err error

	err = enc.Encode(b.entries)
	if err != nil {
		return err
	}

	err = enc.Encode(b.sw)
	if err != nil {
		return err
	}

	err = enc.Encode(b.swc)
	if err != nil {
		return err
	}

	err = enc.Encode(b.sw2)
	if err != nil {
		return err
	}

	return err
}
Exemple #22
0
func (s *Server) clientWriteRoutine(enc *gob.Encoder) {
	defer s.StackCatcher()
	for event := range s.Events {
		var data wireData
		data.Event = event
		err := enc.Encode(data)
		if err != nil {
			if s.Logger != nil {
				s.Logger.Printf("CGF error writing to client: %v", err)
			}
			return
		}
	}
}
Exemple #23
0
func (fn *FileNode) EncodeToGob(enc *gob.Encoder) error {
	if err := serializeCommon(enc, fn.GetType(), fn.INodeCommon); err != nil {
		return err
	}

	if err := enc.Encode(fn.Size); err != nil {
		return fmt.Errorf("Failed to encode Size: %v", err)
	}

	if err := enc.Encode(fn.Chunks); err != nil {
		return fmt.Errorf("Failed to encode Chunks: %v", err)
	}

	return nil
}
Exemple #24
0
func (peer *Peer) Encode(enc *gob.Encoder) {
	checkFatal(enc.Encode(peer.PeerSummary))

	connSummaries := []ConnectionSummary{}
	for _, conn := range peer.connections {
		connSummaries = append(connSummaries, ConnectionSummary{
			conn.Remote().NameByte,
			conn.RemoteTCPAddr(),
			conn.Outbound(),
			conn.Established(),
		})
	}

	checkFatal(enc.Encode(connSummaries))
}
Exemple #25
0
func makeSend(fname string) (chan<- Request, <-chan error, Stopper) {
	var enc *gob.Encoder

	open := func(fname string) (*os.File, error) {
		file, err := os.OpenFile(fname, os.O_WRONLY, 0666)
		if err == nil {
			enc = gob.NewEncoder(file)
		}

		return file, err
	}

	tx := func(v interface{}) error {
		return enc.Encode(v)
	}

	return makeTx(fname, open, tx)
}
Exemple #26
0
func (peer *Peer) encode(enc *gob.Encoder) {
	if err := enc.Encode(peer.peerSummary); err != nil {
		log.Fatal(err)
	}

	connSummaries := []connectionSummary{}
	for _, conn := range peer.connections {
		connSummaries = append(connSummaries, connectionSummary{
			conn.Remote().NameByte,
			conn.remoteTCPAddress(),
			conn.isOutbound(),
			conn.isEstablished(),
		})
	}

	if err := enc.Encode(connSummaries); err != nil {
		log.Fatal(err)
	}
}
Exemple #27
0
func (apu *APU) Save(encoder *gob.Encoder) error {
	encoder.Encode(apu.cycle)
	encoder.Encode(apu.framePeriod)
	encoder.Encode(apu.frameValue)
	encoder.Encode(apu.frameIRQ)
	apu.pulse1.Save(encoder)
	apu.pulse2.Save(encoder)
	apu.triangle.Save(encoder)
	apu.noise.Save(encoder)
	apu.dmc.Save(encoder)
	return nil
}
Exemple #28
0
func gobEncodeMapIntNi(b *testing.B, l int) {
	s := make(map[int]int)
	for idx := 0; idx < l; idx++ {
		s[idx] = rand.Int()
	}
	var (
		enc *gob.Encoder
		buf bytes.Buffer
		err error
	)
	b.ResetTimer()
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		enc = gob.NewEncoder(&buf)
		err = enc.Encode(s)
		if err != nil {
			panic(err.Error())
		}
		//gobEncode(s)
	}
}
Exemple #29
0
func gobEncodeListNi(b *testing.B, l int) {
	s := make([]int, l)
	for idx, _ := range s {
		s[idx] = rand.Int()
	}

	var (
		enc *gob.Encoder
		buf bytes.Buffer
		err error
	)

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		enc = gob.NewEncoder(&buf)
		err = enc.Encode(s)
		if err != nil {
			panic(err.Error())
		}
	}
}
Exemple #30
0
func serverEncode(id string, bufclient *bufio.Writer, encode *gob.Encoder, client net.Conn, send <-chan *packet.Packet) {
	tick := time.NewTicker(time.Second / 20)
	defer tick.Stop()

	for {
		select {
		case msg, ok := <-send:
			if !ok {
				return
			}
			if err := encode.Encode(msg); err != nil {
				log.Printf("[client:%s] encoding packet: %s", id, err)
			}

		case <-tick.C:
			client.SetWriteDeadline(time.Now().Add(30 * time.Second))
			if err := bufclient.Flush(); err != nil {
				log.Printf("[client:%s] sending buffer: %s", id, err)
			}
		}
	}
}