当前位置: 首页>>代码示例>>Golang>>正文


Golang gob.NewEncoder函数代码示例

本文整理汇总了Golang中encoding/gob.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewEncoder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: encryptDispatch

func encryptDispatch(conn net.Conn, m Message) error {
	// We want to sent encrypted data.
	// In order to encrypt, we need to first serialize the message.
	// In order to sent/receive hassle free, we need to serialize the encrypted message
	// So: msg -> [GOB] -> [ENCRYPT] -> [GOB] -> (dispatch)

	// Create encrypter
	var encMsg bytes.Buffer
	encrypter, err := saltsecret.NewWriter(&encMsg, conf.Key, saltsecret.ENCRYPT, true)
	if err != nil {
		return err
	}

	// Serialize message
	enc := gob.NewEncoder(encrypter)
	if err = enc.Encode(m); err != nil {
		return err
	}

	// Flush encrypter to actuall encrypt the message
	if err = encrypter.Flush(); err != nil {
		return err
	}

	// Serialize encrypted message and dispatch it
	dispatch := gob.NewEncoder(conn)
	if err = dispatch.Encode(encMsg.Bytes()); err != nil {
		return err
	}

	return nil
}
开发者ID:andmarios,项目名称:bashistdb,代码行数:32,代码来源:encryptcomm.go

示例2: GobEncode

// When checkpointing the system, encode the non-public interface into the Val,
// followed by a normal encoding of the struct
func (m *MutState) GobEncode() ([]byte, error) {

	if !m.v.IsValid() {
		return nil, fmt.Errorf("Trying to checkpoint a mutable state with a pointer from a previous ceckpoint")
	}

	var b bytes.Buffer
	enc := gob.NewEncoder(&b)
	if err := enc.EncodeValue(m.v.Elem()); err != nil {
		return nil, err
	}
	m.Val = b.Bytes()

	var buff bytes.Buffer

	enc = gob.NewEncoder(&buff)
	if err := enc.Encode(m.KT); err != nil {
		return nil, err
	}
	if err := enc.Encode(m.Val); err != nil {
		return nil, err
	}

	return buff.Bytes(), nil
}
开发者ID:paddie,项目名称:statedb,代码行数:27,代码来源:mutable.go

示例3: sendWork

func sendWork(c net.Conn, workchan chan Work, clientId int) {
	for {
		// assign works until workchan close
		select {
		case work, ok := <-workchan:
			if ok {
				fmt.Printf("[INFO]Assigned work to [%d-%d] to client %d\n", work.StartVal, work.EndVal, clientId)
				encoder := gob.NewEncoder(c)
				err := encoder.Encode(work)
				if err != nil {
					fmt.Printf("[ERROR]Client %d disconnected, lost Job [%d-%d]\n", clientId, work.StartVal, work.EndVal)
					return
				}
			} else {
				// if workchan is closed, send complete message to worker
				encoder := gob.NewEncoder(c)
				err := encoder.Encode(Work{-1, 0, "", ""})
				if err != nil {
					fmt.Printf("[ERROR]Send complete message to client %d failed\n", clientId)
				}
				return
			}
		default:
			time.Sleep(1 * time.Second)
		}
	}
}
开发者ID:RayZ-O,项目名称:go-distributed-simulator,代码行数:27,代码来源:server.go

示例4: Store

// Store stores a fuzz.FuzzReport and the binaryFuzzNames associated with it to the underlying
// fuzz.FuzzReportCache. It creates a bucket with the
// name of the given revision and stores the report as a []byte under a simple key.
func (b *FuzzReportCache) Store(report fuzz.FuzzReportTree, binaryFuzzNames []string, revision string) error {
	storeFunc := func(tx *bolt.Tx) error {
		bkt, err := tx.CreateBucketIfNotExists([]byte(revision))
		if err != nil {
			return fmt.Errorf("Could not make cache/bucket for %s", revision)
		}
		var buffReport bytes.Buffer
		enc := gob.NewEncoder(&buffReport)
		if err := enc.Encode(report); err != nil {
			return fmt.Errorf("Problem encoding report: %s", err)
		}
		if err := bkt.Put(REPORT_KEY, buffReport.Bytes()); err != nil {
			return fmt.Errorf("Problem storing %d bytes of report: %s", buffReport.Len(), err)
		}
		var buffNames bytes.Buffer
		enc = gob.NewEncoder(&buffNames)
		if err := enc.Encode(binaryFuzzNames); err != nil {
			return fmt.Errorf("Problem encoding fuzz names: %s", err)
		}
		if err := bkt.Put(BINARY_FUZZES_KEY, buffNames.Bytes()); err != nil {
			return fmt.Errorf("Problem storing %d bytes of binaryFuzzNames: %s", buffNames.Len(), err)
		}
		return nil
	}
	return b.DB.Update(storeFunc)
}
开发者ID:saltmueller,项目名称:skia-buildbot,代码行数:29,代码来源:fuzzcache.go

示例5: PreWrite

func (zip GobMeddler) PreWrite(field interface{}) (saveValue interface{}, err error) {
	buffer := new(bytes.Buffer)

	if zip {
		// gob encode and gzip
		gzipWriter := gzip.NewWriter(buffer)
		defer gzipWriter.Close()
		gobEncoder := gob.NewEncoder(gzipWriter)
		if err := gobEncoder.Encode(field); err != nil {
			return nil, fmt.Errorf("Gob encoding/gzip error: %v", err)
		}
		if err := gzipWriter.Close(); err != nil {
			return nil, fmt.Errorf("Closing gzip writer: %v", err)
		}

		return buffer.Bytes(), nil
	}

	// gob encode
	gobEncoder := gob.NewEncoder(buffer)
	if err := gobEncoder.Encode(field); err != nil {
		return nil, fmt.Errorf("Gob encoding error: %v", err)
	}
	return buffer.Bytes(), nil
}
开发者ID:RidleyLarsen,项目名称:codegrinder,代码行数:25,代码来源:meddler.go

示例6: CreateGobsFile

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)
		}
	}
}
开发者ID:hyl87,项目名称:2011_Go_Geo_Gfx,代码行数:27,代码来源:gobutil.go

示例7: TestJsonHelperGobEncoding

func TestJsonHelperGobEncoding(t *testing.T) {
	raw := `{"testing": 123,"name":"bob & more"}`
	reader := strings.NewReader(raw)
	jh, err := NewJsonHelperReader(reader)
	assert.Tf(t, err == nil, "Unexpected error decoding gob: %s", err)
	assert.Tf(t, jh.Int("testing") == 123, "Unexpected value in gob: %d", jh.Int("testing"))
	var buf bytes.Buffer
	err = gob.NewEncoder(&buf).Encode(&jh)
	assert.T(t, err == nil, err)

	var jhNew JsonHelper
	err = gob.NewDecoder(&buf).Decode(&jhNew)
	assert.T(t, err == nil, err)
	assert.Tf(t, jhNew.Int("testing") == 123, "Unexpected value in gob: %d", jhNew.Int("testing"))
	assert.Tf(t, jhNew.String("name") == "bob & more", "Unexpected value in gob: %d", jhNew.String("name"))

	buf2 := bytes.Buffer{}
	gt := GobTest{"Hello", jh}
	err = gob.NewEncoder(&buf2).Encode(&gt)
	assert.T(t, err == nil, err)

	var gt2 GobTest
	err = gob.NewDecoder(&buf2).Decode(&gt2)
	assert.T(t, err == nil, err)
	assert.Tf(t, gt2.Name == "Hello", "Unexpected value in gob: %d", gt2.Name)
	assert.Tf(t, gt2.Data.Int("testing") == 123, "Unexpected value in gob: %d", gt2.Data.Int("testing"))
	assert.Tf(t, gt2.Data.String("name") == "bob & more", "Unexpected value in gob: %d", gt2.Data.String("name"))
}
开发者ID:admpub,项目名称:gou,代码行数:28,代码来源:jsonhelper_test.go

示例8: clientSocket

/* Connection to this node's supernode(s) or fellow local nodes*/
func (m *Messenger) clientSocket(name string, address string, conn_type int) {
	conn, err := net.Dial("tcp", fmt.Sprintf("%v", address))
	defer conn.Close()
	if err != nil {
		fmt.Printf("ERROR: Failed to connect to %v on address %v\n", name, address)
		return
	}
	fmt.Printf("Connected to %v on address %v\n", name, address)

	dec := gob.NewDecoder(conn)

	//Add new connection to either the global or local connections/encoders
	if conn_type == mylib.GLOBAL_INTRODUCTION {
		m.global_conns = append(m.global_conns, &conn)
		m.global_encoders[name] = gob.NewEncoder(conn)
		m.global_encoders[name].Encode(&mylib.Message{"", m.name, m.name, name, m.Is_supernode, conn_type, m.v_clock.CurTime()})
		defer delete(m.global_encoders, name)
	} else {
		m.local_conns = append(m.local_conns, &conn)
		m.local_encoders[name] = gob.NewEncoder(conn)
		m.local_encoders[name].Encode(&mylib.Message{"", m.name, m.name, name, m.Is_supernode, conn_type, m.v_clock.CurTime()})
		defer delete(m.local_encoders, name)
	}

	//Start message listening loop
	m.receive_messages(name, dec)
}
开发者ID:DaemonGG,项目名称:Pass_The_Queen,代码行数:28,代码来源:messenger.go

示例9: handleClient

/*********************************************************************
	Logic for Server
*********************************************************************/
func handleClient(conn net.Conn) {

	for {
		var err error
		var request string

		err = gob.NewDecoder(conn).Decode(&request)

		if err != nil {
			conn.Write([]byte("Error in 'reading' data at server."))
			return
		}

		// -------------------
		// Logic at Server
		// -------------------
		comm := strings.Split(request, " ")
		response := data_pkt{}

		if comm[0] == "set" {
			kvs[comm[1]] = comm[2]
			response.Status = true
			response.Msg = kvs[comm[1]] + " got added successfully."
			err = gob.NewEncoder(conn).Encode(response)
		} else if comm[0] == "get" {
			value, status := kvs[comm[1]]
			if status == true {
				response.Status = true
				response.Msg = value
				err = gob.NewEncoder(conn).Encode(response)
			} else {
				response.Status = false
				response.Msg = "Error!!! \nNo key exists."
				err = gob.NewEncoder(conn).Encode(response)
			}
		} else if comm[0] == "delete" {
			temp, ok := kvs[comm[1]]

			if temp == "" && ok == false {
				response.Status = false
				response.Msg = comm[1] + " does not exists in K.V.Store ."
				err = gob.NewEncoder(conn).Encode(response)
			} else {
				delete(kvs, comm[1])
				response.Status = true
				response.Msg = comm[1] + " got deleted."
				err = gob.NewEncoder(conn).Encode(response)
			}
		}

		checkError(err)

		if err != nil {
			conn.Write([]byte("Error Occurred in Server somewhere."))
			return
		}

	}

}
开发者ID:RaviKumarYadav,项目名称:kvstore,代码行数:63,代码来源:kvs_test.go

示例10: sendConnection

/*
 * send connections to nodes with greater Names
 * and stores connections into connections map
 * @param	latterNodes
 *			map that contains all nodes with greater or equal Node names
 *
 * @param	LocalNode
 **/
func sendConnection(latterNodes map[string]Node) {
	defer wg.Done()
	for _, node := range latterNodes {
		conn, err := net.Dial("tcp", node.IP+":"+strconv.Itoa(node.Port))
		for err != nil {
			fmt.Print(".")
			time.Sleep(time.Second * 1)
			conn, err = net.Dial("tcp", node.IP+":"+strconv.Itoa(node.Port))
		}
		if node.Name == LocalNode.Name {
			localConn = conn
			localEncoder = gob.NewEncoder(conn)
		} else {
			addConnection(node.Name, conn)
		}

		/* send an initial ping message to other side of the connection */
		timestampMutex.Lock()
		msg := Message{LocalNode.Name, node.Name, "ping", "ping", 0, vectorTimeStamp}
		timestampMutex.Unlock()
		encoder := gob.NewEncoder(conn)
		encoder.Encode(msg)
	}
	fmt.Println()
}
开发者ID:arminm,项目名称:multegula,代码行数:33,代码来源:MessagePasser.go

示例11: encode

func (f *File) encode(o interface{}) (bs []byte, err error) {
	buf := new(bytes.Buffer)
	if f.compressMethod == _COMPRESS_SNAPPY {
		w := snappy.NewWriter(buf)
		if f.codec == _CODEC_GOB {
			err = gob.NewEncoder(w).Encode(o)
		} else if f.codec == _CODEC_MSGPACK {
			err = msgpack.NewEncoder(w).Encode(o)
		}
		if err != nil {
			return nil, err
		}
		err = w.Close()
		if err != nil {
			return nil, err
		}
	} else {
		if f.codec == _CODEC_GOB {
			err = gob.NewEncoder(buf).Encode(o)
		} else if f.codec == _CODEC_MSGPACK {
			err = msgpack.NewEncoder(buf).Encode(o)
		}
		if err != nil {
			return nil, err
		}
	}
	return buf.Bytes(), nil
}
开发者ID:reusee,项目名称:rcf,代码行数:28,代码来源:rcf.go

示例12: generateKeyPair

func generateKeyPair() (genResult bool) {

	// Generate private key
	privatekey, err := rsa.GenerateKey(rand.Reader, 1024) // Maybe increase size?

	if err != nil {
		fmt.Println(err.Error)
		os.Exit(1)
	}

	var publickey *rsa.PublicKey
	publickey = &privatekey.PublicKey

	// Save private and public key
	privatekeyfile, err := os.Create(PVT_KEY_PATH)
	if err != nil {
		fmt.Println(err)
		genResult = false
	}
	privatekeyencoder := gob.NewEncoder(privatekeyfile)
	privatekeyencoder.Encode(privatekey)
	privatekeyfile.Close()

	publickeyfile, err := os.Create(PUB_KEY_PATH)
	if err != nil {
		fmt.Println(err)
		genResult = false
	}
	publickeyencoder := gob.NewEncoder(publickeyfile)
	publickeyencoder.Encode(publickey)
	publickeyfile.Close()

	// Save PEM file
	pemfile, err := os.Create(PVT_PEM_PATH)

	if err != nil {
		fmt.Println(err)
		genResult = false
	}

	// http://golang.org/pkg/encoding/pem/#Block
	var pemkey = &pem.Block{
		Type:  "RSA PRIVATE KEY",
		Bytes: x509.MarshalPKCS1PrivateKey(privatekey)}

	err = pem.Encode(pemfile, pemkey)

	if err != nil {
		fmt.Println(err)
		genResult = false
	}

	pemfile.Close()

	genResult = true

	return

}
开发者ID:ksred,项目名称:go-ebics-client,代码行数:59,代码来源:crypto.go

示例13: NewGobConnection

func NewGobConnection(conn net.Conn) GobConnection {
	if gcn, ok := gobPool.Get().(*gobConnection); ok {
		gcn.rwc = conn
		gcn.enc = gob.NewEncoder(conn)
		gcn.dec = gob.NewDecoder(conn)
		return gcn
	}
	return &gobConnection{rwc: conn, enc: gob.NewEncoder(conn), dec: gob.NewDecoder(conn)}
}
开发者ID:dongjun111111,项目名称:notes,代码行数:9,代码来源:小工具_一个基于Gob的TCP通讯包.go

示例14: NewGobCodecs

func NewGobCodecs(conn io.ReadWriteCloser) (*gobServerCodec, *gobClientCodec) {
	sbuf := bufio.NewWriter(conn)
	srv := &gobServerCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(sbuf), sbuf}

	cbuf := bufio.NewWriter(conn)
	cli := &gobClientCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(cbuf), cbuf}

	return srv, cli
}
开发者ID:vgp,项目名称:gogp,代码行数:9,代码来源:codec.go

示例15: main

func main() {
	flag.Parse()

	conn, ch := qutils.GetChannel(url)
	defer conn.Close()
	defer ch.Close()

	dataQueue := qutils.GetQueue(*name, ch, false)

	publishQueueName(ch)

	discoveryQueue := qutils.GetQueue("", ch, true)

	ch.QueueBind(
		discoveryQueue.Name,
		"",
		qutils.SensorDiscoveryExchange,
		false,
		nil)

	go listenForDiscoveryRequests(discoveryQueue.Name, ch)

	dur, _ := time.ParseDuration(strconv.Itoa(1000/int(*freq)) + "ms")

	signal := time.Tick(dur)

	buf := new(bytes.Buffer)
	enc := gob.NewEncoder(buf)

	for range signal {
		calcValue()

		reading := dto.SensorMessage{
			Name:      *name,
			Value:     value,
			Timestamp: time.Now(),
		}

		buf.Reset()
		enc = gob.NewEncoder(buf)
		enc.Encode(reading)

		msg := amqp.Publishing{
			Body: buf.Bytes(),
		}

		ch.Publish(
			"",             // exchange name
			dataQueue.Name, // key string
			false,          // mandatory bool
			false,          // immediate bool
			msg)            // msg amqp.Publish

		log.Printf("Reading sent. Value: %v\n", value)
	}
}
开发者ID:horaceheaven,项目名称:go-distributed,代码行数:56,代码来源:sensor.go


注:本文中的encoding/gob.NewEncoder函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。