本文整理汇总了Golang中encoding/gob.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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(>)
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"))
}
开发者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
示例16: Send
// Send implements mpi.Interface.Send. Network uses the encoding/gob package to
// serialize data.
func (n *Network) Send(data interface{}, destination, tag int) error {
/*
Implementation comments:
The mpi.Interface.Send specifies that sends between two nodes may happen
concurrently as long as the tags are unique. These concurrent sends may
have different data types, but the program must know the type of data in
order to decode from the communication channel. The solution to this is
to serialize all of the data into a []byte, and to only send the "message"
type over the channel. Gob can decode the message, observe the tag, and
pass along the []byte to receive for further decoding. When Registering
the tag, the tagManager also creates a communication channel to do the
forwarding once the tag is observed.
*/
manager := n.connections[destination].sendtags
// register the tag for this message
manager.Register(tag)
// serialize the data into a []byte
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(data)
if err != nil {
return err
}
// special case if sending locally
if destination == n.myrank {
n.local.Send(tag, buf.Bytes())
return nil
}
// Launch a reader for the reply message from the destination
go func() {
var m message
err := gob.NewDecoder(n.connections[destination].dial).Decode(&m)
if err != nil {
panic(err) // There should never be a send over the connection that isn't a message
}
manager.Channel(m.Tag) <- m.Bytes
}()
// send the data over the connection.
enc := gob.NewEncoder(n.connections[destination].dial)
err = enc.Encode(message{Tag: tag, Bytes: buf.Bytes()})
if err != nil {
return err
}
// Wait for the confirmation message and then delete the tag
<-manager.Channel(tag)
manager.Delete(tag)
return nil
}
开发者ID:pgruenbacher,项目名称:mpi,代码行数:57,代码来源:network.go
示例17: TestPublisherClosesDuplicateConnections
func TestPublisherClosesDuplicateConnections(t *testing.T) {
log.SetOutput(ioutil.Discard)
// Choose a port
port, err := getFreePort()
if err != nil {
t.Fatal(err)
}
// Start a publisher
p, err := xfer.NewTCPPublisher(port)
if err != nil {
t.Fatal(err)
}
defer p.Close()
// Connect a listener
conn, err := net.Dial("tcp4", "127.0.0.1"+port)
if err != nil {
t.Fatal(err)
}
defer conn.Close()
if err := gob.NewEncoder(conn).Encode(xfer.HandshakeRequest{ID: "foo"}); err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond)
// Try to connect the same listener
dupconn, err := net.Dial("tcp4", "127.0.0.1"+port)
if err != nil {
t.Fatal(err)
}
// Send handshake
if err := gob.NewEncoder(dupconn).Encode(xfer.HandshakeRequest{ID: "foo"}); err != nil {
t.Fatal(err)
}
defer dupconn.Close()
// Publish a message
p.Publish(report.Report{})
// The first listener should receive it
var r report.Report
if err := gob.NewDecoder(conn).Decode(&r); err != nil {
t.Fatal(err)
}
// The duplicate listener should have an error
if err := gob.NewDecoder(dupconn).Decode(&r); err == nil {
t.Errorf("expected error, got none")
} else {
t.Logf("dupconn got expected error: %v", err)
}
}
开发者ID:neviim,项目名称:scope,代码行数:54,代码来源:publisher_test.go
示例18: main
func main() {
// generate private key
privatekey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
fmt.Println(err.Error)
os.Exit(1)
}
var publickey *rsa.PublicKey
publickey = &privatekey.PublicKey
// save private and public key separately
privatekeyfile, err := os.Create("private.key")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
privatekeyencoder := gob.NewEncoder(privatekeyfile)
privatekeyencoder.Encode(privatekey)
privatekeyfile.Close()
publickeyfile, err := os.Create("public.key")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
publickeyencoder := gob.NewEncoder(publickeyfile)
publickeyencoder.Encode(publickey)
publickeyfile.Close()
// save PEM file
pemfile, err := os.Create("private.pem")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// 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)
os.Exit(1)
}
pemfile.Close()
}
开发者ID:snowsnail,项目名称:ole_golang_rsa_tryout,代码行数:54,代码来源:rsa_generate_keypair.go
示例19: Init
func (c *Connection) Init() {
c.send_buffer = new(bytes.Buffer)
c.recv_buffer = new(bytes.Buffer)
c.send_buffer_decoder = gob.NewDecoder(c.send_buffer)
c.send_buffer_encoder = gob.NewEncoder(c.send_buffer)
c.recv_buffer_decoder = gob.NewDecoder(c.recv_buffer)
c.recv_buffer_encoder = gob.NewEncoder(c.recv_buffer)
c.Quit = make(chan bool)
c.Outgoing = make(chan interface{}, 1000)
}
开发者ID:KingsEpic,项目名称:client,代码行数:11,代码来源:connection.go
示例20: save
func save(ctx context.Context) {
stats := stats(ctx)
stats.wg.Wait()
stats.Duration = time.Since(stats.Start)
var buf_part, buf_full bytes.Buffer
full := stats_full{
Header: header(ctx),
Stats: stats,
}
if err := gob.NewEncoder(&buf_full).Encode(&full); err != nil {
log.Errorf(ctx, "appstats Save error: %v", err)
return
} else if buf_full.Len() > bufMaxLen {
// first try clearing stack traces
for i := range full.Stats.RPCStats {
full.Stats.RPCStats[i].StackData = ""
}
buf_full.Truncate(0)
gob.NewEncoder(&buf_full).Encode(&full)
}
part := stats_part(*stats)
for i := range part.RPCStats {
part.RPCStats[i].StackData = ""
part.RPCStats[i].In = ""
part.RPCStats[i].Out = ""
}
if err := gob.NewEncoder(&buf_part).Encode(&part); err != nil {
log.Errorf(ctx, "appstats Save error: %v", err)
return
}
item_part := &memcache.Item{
Key: stats.PartKey(),
Value: buf_part.Bytes(),
}
item_full := &memcache.Item{
Key: stats.FullKey(),
Value: buf_full.Bytes(),
}
log.Infof(ctx, "Saved; %s: %s, %s: %s, link: %v",
item_part.Key,
byteSize(len(item_part.Value)),
item_full.Key,
byteSize(len(item_full.Value)),
URL(ctx),
)
nc := storeContext(ctx)
memcache.SetMulti(nc, []*memcache.Item{item_part, item_full})
}
开发者ID:skkoobb,项目名称:appstats,代码行数:53,代码来源:appstats.go
注:本文中的encoding/gob.NewEncoder函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论