本文整理汇总了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
}
示例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
}
示例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)
}
}
}
示例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)
}
示例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
}
示例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)
}
}
}
示例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(>)
assert.T(t, err == nil, err)
var gt2 GobTest
err = gob.NewDecoder(&buf2).Decode(>2)
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"))
}
示例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)
}
示例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
}
}
}
示例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()
}
示例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
}
示例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
}
示例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)}
}
示例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
}
示例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)
}
}