當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。