func (this *uint32Writer) WriteMsg(msg proto.Message) (err error) { var data []byte if m, ok := msg.(marshaler); ok { n := m.Size() if n >= len(this.buffer) { this.buffer = make([]byte, n) } _, err = m.MarshalTo(this.buffer) if err != nil { return err } data = this.buffer[:n] } else { data, err = proto.Marshal(msg) if err != nil { return err } } length := uint32(len(data)) if err := binary.Write(this.w, this.byteOrder, &length); err != nil { return err } _, err = this.w.Write(data) return err }
func (this *varintWriter) WriteMsg(msg proto.Message) (err error) { var data []byte if m, ok := msg.(marshaler); ok { n := m.Size() if n >= len(this.buffer) { this.buffer = make([]byte, n) } _, err = m.MarshalTo(this.buffer) if err != nil { return err } data = this.buffer[:n] } else { data, err = proto.Marshal(msg) if err != nil { return err } } length := uint64(len(data)) n := binary.PutUvarint(this.lenBuf, length) _, err = this.w.Write(this.lenBuf[:n]) if err != nil { return err } _, err = this.w.Write(data) return err }
func (n *FSNode) GetBytes() ([]byte, error) { pbn := new(pb.Data) pbn.Type = &n.Type pbn.Filesize = proto.Uint64(uint64(len(n.Data)) + n.subtotal) pbn.Blocksizes = n.blocksizes pbn.Data = n.Data return proto.Marshal(pbn) }
func (sk *RsaPrivateKey) Bytes() ([]byte, error) { b := x509.MarshalPKCS1PrivateKey(sk.sk) pbmes := new(pb.PrivateKey) typ := pb.KeyType_RSA pbmes.Type = &typ pbmes.Data = b return proto.Marshal(pbmes) }
// putLocal stores the key value pair in the datastore func (dht *IpfsDHT) putLocal(key key.Key, rec *pb.Record) error { data, err := proto.Marshal(rec) if err != nil { return err } return dht.datastore.Put(key.DsKey(), data) }
// MarshalPrivateKey converts a key object into its protobuf serialized form. func MarshalPrivateKey(k PrivKey) ([]byte, error) { b := MarshalRsaPrivateKey(k.(*RsaPrivateKey)) pmes := new(pb.PrivateKey) typ := pb.KeyType_RSA // for now only type. pmes.Type = &typ pmes.Data = b return proto.Marshal(pmes) }
// MarshalPublicKey converts a public key object into a protobuf serialized // public key func MarshalPublicKey(k PubKey) ([]byte, error) { b, err := MarshalRsaPublicKey(k.(*RsaPublicKey)) if err != nil { return nil, err } pmes := new(pb.PublicKey) typ := pb.KeyType_RSA // for now only type. pmes.Type = &typ pmes.Data = b return proto.Marshal(pmes) }
func writeHdr(n *merkledag.Node, hdr *pb.Set) error { hdrData, err := proto.Marshal(hdr) if err != nil { return err } n.SetData(make([]byte, binary.MaxVarintLen64, binary.MaxVarintLen64+len(hdrData))) written := binary.PutUvarint(n.Data(), uint64(len(hdrData))) n.SetData(n.Data()[:written]) n.SetData(append(n.Data(), hdrData...)) return nil }
func (pk *RsaPublicKey) Bytes() ([]byte, error) { b, err := x509.MarshalPKIXPublicKey(pk.k) if err != nil { return nil, err } pbmes := new(pb.PublicKey) typ := pb.KeyType_RSA pbmes.Type = &typ pbmes.Data = b return proto.Marshal(pbmes) }
func putRoutingRecord(ds datastore.Datastore, k string, value *pb.Record) error { data, err := proto.Marshal(value) if err != nil { return err } dskey := dshelp.NewKeyFromBinary([]byte(k)) // TODO namespace if err := ds.Put(dskey, data); err != nil { return err } return nil }
func putRoutingRecord(ds datastore.Datastore, k key.Key, value *pb.Record) error { data, err := proto.Marshal(value) if err != nil { return err } dskey := k.DsKey() // TODO namespace if err := ds.Put(dskey, data); err != nil { return err } return nil }
func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte) error { rec, err := record.MakePutRecord(c.sk, key, val, false) if err != nil { return err } data, err := proto.Marshal(rec) if err != nil { return err } return c.datastore.Put(dshelp.NewKeyFromBinary(key), data) }
func (c *offlineRouting) PutValue(ctx context.Context, key key.Key, val []byte) error { rec, err := record.MakePutRecord(c.sk, key, val, false) if err != nil { return err } data, err := proto.Marshal(rec) if err != nil { return err } return c.datastore.Put(key.DsKey(), data) }
//FolderPBData returns Bytes that represent a Directory. func FolderPBData() []byte { pbfile := new(pb.Data) typ := pb.Data_Directory pbfile.Type = &typ data, err := proto.Marshal(pbfile) if err != nil { //this really shouldnt happen, i promise panic(err) } return data }
func BytesForMetadata(m *Metadata) ([]byte, error) { pbd := new(pb.Data) pbd.Filesize = proto.Uint64(m.Size) typ := pb.Data_Metadata pbd.Type = &typ mdd, err := m.Bytes() if err != nil { return nil, err } pbd.Data = mdd return proto.Marshal(pbd) }
// FIXME(brian): is this method meant to simulate putting a value into the network? func (c *client) PutValue(ctx context.Context, key key.Key, val []byte) error { log.Debugf("PutValue: %s", key) rec := new(dhtpb.Record) rec.Value = val rec.Key = proto.String(string(key)) rec.TimeReceived = proto.String(u.FormatRFC3339(time.Now())) data, err := proto.Marshal(rec) if err != nil { return err } return c.datastore.Put(key.DsKey(), data) }
//SymlinkData returns a `Data_Symlink` protobuf message for the path you specify. func SymlinkData(path string) ([]byte, error) { pbdata := new(pb.Data) typ := pb.Data_Symlink pbdata.Data = []byte(path) pbdata.Type = &typ out, err := proto.Marshal(pbdata) if err != nil { return nil, err } return out, nil }
//WrapData marshals raw bytes into a `Data_Raw` type protobuf message. func WrapData(b []byte) []byte { pbdata := new(pb.Data) typ := pb.Data_Raw pbdata.Data = b pbdata.Type = &typ pbdata.Filesize = proto.Uint64(uint64(len(b))) out, err := proto.Marshal(pbdata) if err != nil { // This shouldnt happen. seriously. panic(err) } return out }
func TestPBNodeVerboseEqual(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedPBNode(popr, false) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } msg := &PBNode{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("%#v !VerboseEqual %#v, since %v", msg, p, err) } }
func BenchmarkPBLinkProtoMarshal(b *testing.B) { popr := math_rand.New(math_rand.NewSource(616)) total := 0 pops := make([]*PBLink, 10000) for i := 0; i < 10000; i++ { pops[i] = NewPopulatedPBLink(popr, false) } b.ResetTimer() for i := 0; i < b.N; i++ { data, err := github_com_gogo_protobuf_proto.Marshal(pops[i%10000]) if err != nil { panic(err) } total += len(data) } b.SetBytes(int64(total / b.N)) }
func PublishEntry(ctx context.Context, r routing.IpfsRouting, ipnskey key.Key, rec *pb.IpnsEntry) error { timectx, cancel := context.WithTimeout(ctx, PublishPutValTimeout) defer cancel() data, err := proto.Marshal(rec) if err != nil { return err } log.Debugf("Storing ipns entry at: %s", ipnskey) // Store ipns entry at "/ipns/"+b58(h(pubkey)) if err := r.PutValue(timectx, ipnskey, data); err != nil { return err } return nil }
func writeHdr(n *merkledag.ProtoNode, hdr *pb.Set) error { hdrData, err := proto.Marshal(hdr) if err != nil { return err } // make enough space for the length prefix and the marshalled header data data := make([]byte, binary.MaxVarintLen64, binary.MaxVarintLen64+len(hdrData)) // write the uvarint length of the header data uvarlen := binary.PutUvarint(data, uint64(len(hdrData))) // append the actual protobuf data *after* the length value we wrote data = append(data[:uvarlen], hdrData...) n.SetData(data) return nil }
func FilePBData(data []byte, totalsize uint64) []byte { pbfile := new(pb.Data) typ := pb.Data_File pbfile.Type = &typ pbfile.Data = data pbfile.Filesize = proto.Uint64(totalsize) data, err := proto.Marshal(pbfile) if err != nil { // This really shouldnt happen, i promise // The only failure case for marshal is if required fields // are not filled out, and they all are. If the proto object // gets changed and nobody updates this function, the code // should panic due to programmer error panic(err) } return data }
func TestPBNodeSize(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedPBNode(popr, true) size2 := github_com_gogo_protobuf_proto.Size(p) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } size := p.Size() if len(data) != size { t.Fatalf("size %v != marshalled size %v", size, len(data)) } if size2 != size { t.Fatalf("size %v != before marshal proto.Size %v", size, size2) } size3 := github_com_gogo_protobuf_proto.Size(p) if size3 != size { t.Fatalf("size %v != after marshal proto.Size %v", size, size3) } }
func (this *fullWriter) WriteMsg(msg proto.Message) (err error) { var data []byte if m, ok := msg.(marshaler); ok { n := m.Size() if n >= len(this.buffer) { this.buffer = make([]byte, n) } _, err = m.MarshalTo(this.buffer) if err != nil { return err } data = this.buffer[:n] } else { data, err = proto.Marshal(msg) if err != nil { return err } } _, err = this.w.Write(data) return err }
func BenchmarkPBLinkProtoUnmarshal(b *testing.B) { popr := math_rand.New(math_rand.NewSource(616)) total := 0 datas := make([][]byte, 10000) for i := 0; i < 10000; i++ { data, err := github_com_gogo_protobuf_proto.Marshal(NewPopulatedPBLink(popr, false)) if err != nil { panic(err) } datas[i] = data } msg := &PBLink{} b.ResetTimer() for i := 0; i < b.N; i++ { total += len(datas[i%10000]) if err := github_com_gogo_protobuf_proto.Unmarshal(datas[i%10000], msg); err != nil { panic(err) } } b.SetBytes(int64(total / b.N)) }
func TestPBLinkProto(t *testing.T) { popr := math_rand.New(math_rand.NewSource(time.Now().UnixNano())) p := NewPopulatedPBLink(popr, false) data, err := github_com_gogo_protobuf_proto.Marshal(p) if err != nil { panic(err) } msg := &PBLink{} if err := github_com_gogo_protobuf_proto.Unmarshal(data, msg); err != nil { panic(err) } for i := range data { data[i] = byte(popr.Intn(256)) } if err := p.VerboseEqual(msg); err != nil { t.Fatalf("%#v !VerboseProto %#v, since %v", msg, p, err) } if !p.Equal(msg) { t.Fatalf("%#v !Proto %#v", msg, p) } }
func AssertSelected(r *pb.IpnsEntry, from ...*pb.IpnsEntry) error { shuffle(from) var vals [][]byte for _, r := range from { data, err := proto.Marshal(r) if err != nil { return err } vals = append(vals, data) } i, err := selectRecord(from, vals) if err != nil { return err } if from[i] != r { return fmt.Errorf("selected incorrect record %d", i) } return nil }
// Store a value in this peer local storage func (dht *IpfsDHT) handlePutValue(ctx context.Context, p peer.ID, pmes *pb.Message) (*pb.Message, error) { defer log.EventBegin(ctx, "handlePutValue", p).Done() dskey := key.Key(pmes.GetKey()).DsKey() if err := dht.verifyRecordLocally(pmes.GetRecord()); err != nil { log.Warningf("Bad dht record in PUT from: %s. %s", key.Key(pmes.GetRecord().GetAuthor()), err) return nil, err } rec := pmes.GetRecord() // record the time we receive every record rec.TimeReceived = proto.String(u.FormatRFC3339(time.Now())) data, err := proto.Marshal(rec) if err != nil { return nil, err } err = dht.datastore.Put(dskey, data) log.Debugf("%s handlePutValue %v", dht.self, dskey) return pmes, err }
// writeMsgCtx is used by the func writeMsgCtx(ctx context.Context, w msgio.Writer, msg proto.Message) ([]byte, error) { enc, err := proto.Marshal(msg) if err != nil { return nil, err } // write in a goroutine so we can exit when our context is cancelled. done := make(chan error) go func(m []byte) { err := w.WriteMsg(m) select { case done <- err: case <-ctx.Done(): } }(enc) select { case <-ctx.Done(): return nil, ctx.Err() case e := <-done: return enc, e } }