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")) }
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 }
// 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 }
/* * 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() }
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) } } }
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 }
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) } } }
// 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) }
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 }
/********************************************************************* 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 } } }
/* 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) }
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 }
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 }
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)} }
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) } }
// 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 }
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) } }
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() }
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) }
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}) }
// 参考资料: http://blog.golang.org/gobs-of-data // gob 似乎是一个Go语言的序列化(反序列化)工具;gob是面向go的,是个非其它语言友好的编码格式,类似于PHP的serialize func main() { data := map[int16]string{1: "first", 2: "second"} en := gob.NewEncoder(os.Stdout) en.Encode(data) fmt.Println() var buf bytes.Buffer en2 := gob.NewEncoder(&buf) en2.Encode(data) fmt.Print(buf) }
// Save freezes and saves the data store to disk. func (ds *DataStore) Save(dsFile string) error { if !ds.updated { return nil } logger.Infof("Data has changed, saving data store to disk") if dsFile == "" { err := fmt.Errorf("Yikes! Cannot save data store to disk because no file was specified.") return err } fp, err := ioutil.TempFile(path.Dir(dsFile), "ds-store") if err != nil { return err } zfp := zlib.NewWriter(fp) fstore := new(dsFileStore) dscache := new(bytes.Buffer) objList := new(bytes.Buffer) ds.m.RLock() defer ds.m.RUnlock() ds.updated = false err = ds.dsc.Save(dscache) if err != nil { fp.Close() return err } enc := gob.NewEncoder(objList) defer func() { if x := recover(); x != nil { err = fmt.Errorf("Something went wrong encoding the data store with Gob") } }() err = enc.Encode(ds.objList) if err != nil { fp.Close() return err } fstore.Cache = dscache.Bytes() fstore.ObjList = objList.Bytes() enc = gob.NewEncoder(zfp) err = enc.Encode(fstore) zfp.Close() if err != nil { fp.Close() return err } err = fp.Close() if err != nil { return err } return os.Rename(fp.Name(), dsFile) }
func (d *FileTreeCache) AddEntry(entry protocol.FileInfo, peer protocol.DeviceID) { d.db.Update(func(tx *bolt.Tx) error { eb := tx.Bucket(d.folderBucketKey).Bucket(entriesBucket) /* save entry */ var buf bytes.Buffer enc := gob.NewEncoder(&buf) enc.Encode(entry) eb.Put([]byte(entry.Name), buf.Bytes()) // TODO handle error? /* add peer */ edb := tx.Bucket(d.folderBucketKey).Bucket(entryDevicesBucket) v := edb.Get([]byte(entry.Name)) var devices map[string]bool if v == nil { devices = make(map[string]bool) } else { rbuf := bytes.NewBuffer(v) dec := gob.NewDecoder(rbuf) dec.Decode(&devices) } devices[peer.String()] = true var dbuf bytes.Buffer enc = gob.NewEncoder(&dbuf) enc.Encode(devices) edb.Put([]byte(entry.Name), dbuf.Bytes()) /* add child lookup */ dir := path.Dir(entry.Name) clb := tx.Bucket(d.folderBucketKey).Bucket(childLookupBucket) v = clb.Get([]byte(dir)) if debug { l.Debugln("Adding child", entry.Name, "for dir", dir) } var children map[string]bool if v == nil { children = make(map[string]bool) } else { rbuf := bytes.NewBuffer(v) dec := gob.NewDecoder(rbuf) dec.Decode(&children) } children[entry.Name] = true var cbuf bytes.Buffer enc = gob.NewEncoder(&cbuf) enc.Encode(children) clb.Put([]byte(dir), cbuf.Bytes()) return nil }) }
func main() { var stream bytes.Buffer gob.NewEncoder(&stream).Encode(Test{"one", 1}) var result Test dec := gob.NewDecoder(&stream) err := dec.Decode(&result) log.Print(result, err) stream.Reset() var result2 Test gob.NewEncoder(&stream).Encode(Test{"two", 2}) err = dec.Decode(&result2) log.Print(result2, err) }
// Go should have the same behaviour as net/rpc/Go(). func (r Rrpc) Go(serviceMethod string, args, reply interface{}, done chan *Call) *Call { r.mutex.Lock() r.nextCallID++ thisID := r.nextCallID r.mutex.Unlock() if done == nil { done = make(chan *Call, 1) } else { // Comment from: http://golang.org/src/net/rpc/client.go // If caller passes done != nil, it must arrange that // done has enough buffer for the number of simultaneous // RPCs that will be using that channel. If the channel // is totally unbuffered, it's best not to run at all. if cap(done) == 0 { log.Panic("rpc: done channel is unbuffered") } } call := &Call{ Valid: true, ID: thisID, ServiceMethod: serviceMethod, replyIface: reply, Done: done, } ca := bytes.NewBuffer(call.Args) enc := gob.NewEncoder(ca) err := enc.Encode(args) if err != nil { call.ErrorMsg = err.Error() call.Error = err done <- call return call } call.Args = ca.Bytes() cr := bytes.NewBuffer(call.Reply) enc2 := gob.NewEncoder(cr) err = enc2.Encode(reply) if err != nil { call.ErrorMsg = err.Error() call.Error = err done <- call return call } call.Reply = cr.Bytes() //fmt.Printf("DEBUG c.waiting <- call Rrpc=%#v Call=%#v\n", r, call) r.waiting <- call return call }
func (l *commandHolder) getHolderChannel(holderName string) chan command { l.Lock() defer l.Unlock() res, ok := l.channels[holderName] if !ok { res = commandHolderChannel{cmdChan: make(chan command, 10000), exitChan: make(chan bool)} l.channels[holderName] = res l.wg.Add(1) go func(holderName string, c commandHolderChannel) { defer l.wg.Done() dir, _ := filepath.Split(holderName) os.MkdirAll(dir, os.ModeDir) f, err := os.OpenFile(holderName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { panic(err) } defer f.Close() // Сохранение метки начала enc := gob.NewEncoder(f) startDt := time.Now() err = enc.Encode(&startDt) if err != nil { fmt.Println(err) } for { select { case cmd := <-c.cmdChan: { func() { enc := gob.NewEncoder(f) err = enc.Encode(&cmd) if err != nil { fmt.Println(err) } }() l.wgSend.Done() } case <-c.exitChan: { return } } } }(holderName, res) } return res.cmdChan }
func (c *Client) encode(typ msgType, val interface{}, dst *net.UDPAddr) error { log.ProtoLevel().Tag("client", "discover").Printf("Send request (%v) to %v from %v.", typ, dst, c.conn.LocalAddr()) reqBuf := bytes.NewBuffer([]byte{}) buf := make([]byte, binary.MaxVarintLen16) binary.PutUvarint(buf, uint64(typ)) n, err := reqBuf.Write(buf) if err != nil { return e.Push(err, "error enconding message type") } if n != len(buf) { return e.Push(err, "error enconding message type") } enc := gob.NewEncoder(reqBuf) err = enc.Encode(val) if err != nil { return e.Push(err, e.New("error encoding")) } msg, err := NewMsg(c.Name, c.ServerName, c.PrivateKey, c.ServerKey, reqBuf.Bytes()) if err != nil { return e.Push(err, "erro cryptographing the value") } reqBuf = bytes.NewBuffer([]byte{}) enc = gob.NewEncoder(reqBuf) err = enc.Encode(msg) if err != nil { return e.Push(err, e.New("error encoding")) } if reqBuf.Len() > c.BufSize { return e.New("value to encode is too big %v", reqBuf.Len()) } err = c.conn.SetDeadline(time.Now().Add(c.Deadline)) if err != nil { return e.New(err) } _, _, err = c.conn.WriteMsgUDP(reqBuf.Bytes(), nil, dst) if err != nil { return e.New(err) } err = c.conn.SetDeadline(time.Time{}) if err != nil { return e.New(err) } return nil }
func (txn *Txn) PutGo(dbi DBI, key, val interface{}, flags uint) error { var bkey bytes.Buffer encoder := gob.NewEncoder(&bkey) err := encoder.Encode(key) if err != nil { return err } var bval bytes.Buffer encoder = gob.NewEncoder(&bval) err = encoder.Encode(val) if err != nil { return err } return txn.Put(dbi, bkey.Bytes(), bval.Bytes(), flags) }
func (cursor *Cursor) PutGo(key, val interface{}, flags uint) error { var bkey bytes.Buffer encoder := gob.NewEncoder(&bkey) err := encoder.Encode(key) if err != nil { return err } var bval bytes.Buffer encoder = gob.NewEncoder(&bval) err = encoder.Encode(val) if err != nil { return err } return cursor.Put(bkey.Bytes(), bval.Bytes(), flags) }
// Put adds the package documentation to the database. func (db *Database) Put(pdoc *doc.Package, nextCrawl time.Time) error { c := db.Pool.Get() defer c.Close() score := documentScore(pdoc) terms := documentTerms(pdoc, score) var gobBuf bytes.Buffer if err := gob.NewEncoder(&gobBuf).Encode(pdoc); err != nil { return err } // Truncate large documents. if gobBuf.Len() > 700000 { pdocNew := *pdoc pdoc = &pdocNew pdoc.Truncated = true pdoc.Vars = nil pdoc.Funcs = nil pdoc.Types = nil pdoc.Consts = nil pdoc.Examples = nil gobBuf.Reset() if err := gob.NewEncoder(&gobBuf).Encode(pdoc); err != nil { return err } } gobBytes, err := snappy.Encode(nil, gobBuf.Bytes()) if err != nil { return err } kind := "p" switch { case pdoc.Name == "": kind = "d" case pdoc.IsCmd: kind = "c" } t := int64(0) if !nextCrawl.IsZero() { t = nextCrawl.Unix() } _, err = putScript.Do(c, pdoc.ImportPath, pdoc.Synopsis, score, gobBytes, strings.Join(terms, " "), pdoc.Etag, kind, t) return err }