• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang proto.NewBuffer函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中code/google/com/p/goprotobuf/proto.NewBuffer函数的典型用法代码示例。如果您正苦于以下问题:Golang NewBuffer函数的具体用法?Golang NewBuffer怎么用?Golang NewBuffer使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewBuffer函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: newLog

// Creates a new log.
func newLog() *Log {
	return &Log{
		entries:   make([]*LogEntry, 0),
		pBuffer:   proto.NewBuffer(nil),
		pLogEntry: &protobuf.ProtoLogEntry{},
	}
}
开发者ID:josephyzhou,项目名称:raft,代码行数:8,代码来源:log.go


示例2: struct_pack

func struct_pack(L *lua.LState) int {

	format := byte(L.CheckInt(2))
	l_value := L.CheckNumber(3)
	b := proto.NewBuffer(nil)
	b.Reset()
	L.SetTop(1)
	switch format {
	case 'i':
		b.EncodeFixed32(uint64(int32(l_value)))
		L.Push(lua.LString(string(b.Bytes())))
	case 'q':
		b.EncodeFixed64(uint64(int64(l_value)))
		L.Push(lua.LString(string(b.Bytes())))
	case 'f':
		b.EncodeFixed32(uint64(float32(l_value)))
		L.Push(lua.LString(string(b.Bytes())))
	case 'd':
		b.EncodeFixed64(uint64(float64(l_value)))
		L.Push(lua.LString(string(b.Bytes())))
	case 'I':
		b.EncodeFixed32(uint64(uint32(l_value)))
		L.Push(lua.LString(string(b.Bytes())))
	case 'Q':
		b.EncodeFixed64(uint64(l_value))
		L.Push(lua.LString(string(b.Bytes())))
	default:
		L.RaiseError("Unknown, format")
	}
	L.Call(1, 0)

	return 0
}
开发者ID:bodogbo,项目名称:OSG-Server-Go,代码行数:33,代码来源:lua_pb_auto.go


示例3: receiveMsg

func receiveMsg(conn net.Conn) {
	buf := make([]byte, 128)
	n, err := conn.Read(buf)
	if checkerr(err) {
		return
	}
	fmt.Printf("\nread (%d) byte from %v :\n%v\n", n, conn.RemoteAddr(), buf[:n])

	protobuf := proto.NewBuffer(buf[:n])
	var msg OSMsg.OSMsg
	pumerr := protobuf.Unmarshal(&msg)
	if checkerr(pumerr) {
		return
	}
	fmt.Printf("[Message]----->%v\n\n", msg.String())

	//
	// umerr := proto.Unmarshal(buf[:n], &msg)
	// if checkerr(umerr) {
	// 	return
	// }
	// fmt.Printf("[MyMessage] %v\n", msg)

	// readStr := string(buf[:n])
	// fmt.Printf("read string (%d):\n%s\n", n, readStr)
	// umterr := proto.UnmarshalText(readStr, &msg)
	// if checkerr(umterr) {
	// 	return
	// }
	// fmt.Printf("[MyMessage] %v\n", msg)
}
开发者ID:shaalx,项目名称:protobuf,代码行数:31,代码来源:main_c.go


示例4: NewServerCodec

func NewServerCodec(conn zmq.Socket) rpc.ServerCodec {
	req := proto.NewBuffer(nil)
	packetIds := make(idMap)
	clientIds := make(map[uint64]uint64)

	return &serverCodec{conn, req, packetIds, clientIds}
}
开发者ID:yanatan16,项目名称:protorpc,代码行数:7,代码来源:server.go


示例5: Get

func (l *bufferList) Get() (*proto.Buffer, bool) {
	if v, ok := l.l.Get(); ok {
		return v.(*proto.Buffer), ok
	}

	return proto.NewBuffer(make([]byte, 0, 4096)), false
}
开发者ID:pjjw,项目名称:prometheus,代码行数:7,代码来源:freelist.go


示例6: BenchmarkUnmarshal

func BenchmarkUnmarshal(b *testing.B) {
	b.StopTimer()

	// BenchmarkUnmarshal is called multiple times.
	once.Do(func() {
		go http.ListenAndServe("localhost:9090", nil)
	})

	raw := prepareBuf(b).Bytes()
	buf := proto.NewBuffer(make([]byte, 0, 4096))

	for i := 0; i < b.N; i++ {
		buf.SetBuf(raw)

		b.StartTimer()
		v := &SampleValueSeries{Value: make([]*SampleValueSeries_Value, 0, numSamples)}
		if err := buf.Unmarshal(v); err != nil {
			b.Fatal(err)
		}
		b.StopTimer()

		if len(v.Value) != numSamples {
			b.Fatal(len(v.Value))
		}
	}
}
开发者ID:juliusv,项目名称:bench_sample_formats,代码行数:26,代码来源:proto_test.go


示例7: BuildJsonFromProto

func BuildJsonFromProto(cmdname string, cmddata []byte) string {
	recvbuf := proto.NewBuffer(cmddata)
	recv := &AccountTokenVerifyLoginUserPmd_CS{} //难点,这里这个结构是不确定的,只能动态描述
	recvbuf.Unmarshal(recv)
	recv_json, _ := json.Marshal(recv)
	return string(recv_json)
}
开发者ID:navy1125,项目名称:gostudy,代码行数:7,代码来源:json2prototest.go


示例8: struct_unpack

func struct_unpack(L *lua.LState) int {

	format := byte(L.CheckInt(1))
	l_value := L.CheckString(2)
	pos := L.CheckInt(3)

	b_value := []byte(l_value)

	b := proto.NewBuffer(b_value[pos:])

	switch format {
	case 'i':
		value, _ := b.DecodeFixed32()
		L.Push(lua.LNumber(int32(value)))
	case 'q':
		value, _ := b.DecodeFixed64()
		L.Push(lua.LNumber(int64(value)))
	case 'f':
		value, _ := b.DecodeFixed32()
		L.Push(lua.LNumber(float32(value)))
	case 'd':
		value, _ := b.DecodeFixed64()
		L.Push(lua.LNumber(float64(value)))
	case 'I':
		value, _ := b.DecodeFixed32()
		L.Push(lua.LNumber(uint32(value)))
	case 'Q':
		value, _ := b.DecodeFixed64()
		L.Push(lua.LNumber(uint64(value)))
	default:
		L.RaiseError("Unknown, format")
	}

	return 1
}
开发者ID:bodogbo,项目名称:OSG-Server-Go,代码行数:35,代码来源:lua_pb_auto.go


示例9: NewBuffer

func NewBuffer(rw io.ReadWriter) *Buffer {
	return &Buffer{
		rw:          rw,
		protoBuffer: proto.NewBuffer(nil),
		index:       0,
	}
}
开发者ID:lijie,项目名称:siriusdb,代码行数:7,代码来源:net.go


示例10: BuildProtoFromJson

func BuildProtoFromJson(typ reflect.Type, cmdjson string) []byte {
	proto_cmd := reflect.New(typ).Interface().(proto.Message)
	rawdata := []byte(cmdjson)
	json.Unmarshal(rawdata, proto_cmd)
	sendbuf := proto.NewBuffer(nil)
	sendbuf.Marshal(proto_cmd)
	return sendbuf.Bytes()
}
开发者ID:navy1125,项目名称:gostudy,代码行数:8,代码来源:json2prototest.go


示例11: Write

func (self *LevelDbShard) Write(database string, series []*protocol.Series) error {
	wb := levigo.NewWriteBatch()
	defer wb.Close()

	for _, s := range series {
		if len(s.Points) == 0 {
			return errors.New("Unable to write no data. Series was nil or had no points.")
		}

		count := 0
		for fieldIndex, field := range s.Fields {
			temp := field
			id, err := self.createIdForDbSeriesColumn(&database, s.Name, &temp)
			if err != nil {
				return err
			}
			keyBuffer := bytes.NewBuffer(make([]byte, 0, 24))
			dataBuffer := proto.NewBuffer(nil)
			for _, point := range s.Points {
				keyBuffer.Reset()
				dataBuffer.Reset()

				keyBuffer.Write(id)
				timestamp := self.convertTimestampToUint(point.GetTimestampInMicroseconds())
				// pass the uint64 by reference so binary.Write() doesn't create a new buffer
				// see the source code for intDataSize() in binary.go
				binary.Write(keyBuffer, binary.BigEndian, &timestamp)
				binary.Write(keyBuffer, binary.BigEndian, point.SequenceNumber)
				pointKey := keyBuffer.Bytes()

				if point.Values[fieldIndex].GetIsNull() {
					wb.Delete(pointKey)
					goto check
				}

				err = dataBuffer.Marshal(point.Values[fieldIndex])
				if err != nil {
					return err
				}
				wb.Put(pointKey, dataBuffer.Bytes())
			check:
				count++
				if count >= self.writeBatchSize {
					err = self.db.Write(self.writeOptions, wb)
					if err != nil {
						return err
					}
					count = 0
					wb.Clear()
				}
			}
		}
	}

	return self.db.Write(self.writeOptions, wb)
}
开发者ID:j0ni,项目名称:influxdb,代码行数:56,代码来源:leveldb_shard.go


示例12: Write

func (self *Shard) Write(database string, series []*protocol.Series) error {
	wb := make([]storage.Write, 0)

	for _, s := range series {
		if len(s.Points) == 0 {
			return errors.New("Unable to write no data. Series was nil or had no points.")
		}
		if len(s.FieldIds) == 0 {
			return errors.New("Unable to write points without fields")
		}

		count := 0
		for fieldIndex, id := range s.FieldIds {
			for _, point := range s.Points {
				// keyBuffer and dataBuffer have to be recreated since we are
				// batching the writes, otherwise new writes will override the
				// old writes that are still in memory
				keyBuffer := bytes.NewBuffer(make([]byte, 0, 24))
				dataBuffer := proto.NewBuffer(nil)
				var err error

				binary.Write(keyBuffer, binary.BigEndian, &id)
				timestamp := self.convertTimestampToUint(point.GetTimestampInMicroseconds())
				// pass the uint64 by reference so binary.Write() doesn't create a new buffer
				// see the source code for intDataSize() in binary.go
				binary.Write(keyBuffer, binary.BigEndian, &timestamp)
				binary.Write(keyBuffer, binary.BigEndian, point.SequenceNumber)
				pointKey := keyBuffer.Bytes()

				if point.Values[fieldIndex].GetIsNull() {
					wb = append(wb, storage.Write{Key: pointKey, Value: nil})
					goto check
				}

				err = dataBuffer.Marshal(point.Values[fieldIndex])
				if err != nil {
					return err
				}
				wb = append(wb, storage.Write{Key: pointKey, Value: dataBuffer.Bytes()})
			check:
				count++
				if count >= self.writeBatchSize {
					err = self.db.BatchPut(wb)
					if err != nil {
						return err
					}
					count = 0
					wb = make([]storage.Write, 0, self.writeBatchSize)
				}
			}
		}
	}

	return self.db.BatchPut(wb)
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:55,代码来源:shard.go


示例13: zig_zag_encode64

func zig_zag_encode64(L *lua.LState) int {
	b := proto.NewBuffer(nil)
	b.Reset()
	err := b.EncodeZigzag64(uint64(L.CheckNumber(1)))
	if err == nil {
		x, _ := binary.ReadUvarint(bytes.NewBuffer(b.Bytes()))
		L.Push(lua.LNumber(x))
		return 1
	}
	return 0
}
开发者ID:bodogbo,项目名称:OSG-Server-Go,代码行数:11,代码来源:lua_pb_auto.go


示例14: Write

func (self *Shard) Write(database string, series []*protocol.Series) error {
	self.closeLock.RLock()
	defer self.closeLock.RUnlock()
	if self.closed {
		return fmt.Errorf("Shard is closed")
	}

	wb := make([]storage.Write, 0)

	for _, s := range series {
		if len(s.Points) == 0 {
			return errors.New("Unable to write no data. Series was nil or had no points.")
		}
		if len(s.FieldIds) == 0 {
			return errors.New("Unable to write points without fields")
		}

		count := 0
		for fieldIndex, id := range s.FieldIds {
			for _, point := range s.Points {
				// keyBuffer and dataBuffer have to be recreated since we are
				// batching the writes, otherwise new writes will override the
				// old writes that are still in memory
				dataBuffer := proto.NewBuffer(nil)
				var err error

				sk := newStorageKey(id, point.GetTimestamp(), point.GetSequenceNumber())
				if point.Values[fieldIndex].GetIsNull() {
					wb = append(wb, storage.Write{Key: sk.bytes(), Value: nil})
					goto check
				}

				err = dataBuffer.Marshal(point.Values[fieldIndex])
				if err != nil {
					return err
				}
				wb = append(wb, storage.Write{Key: sk.bytes(), Value: dataBuffer.Bytes()})
			check:
				count++
				if count >= self.writeBatchSize {
					err = self.db.BatchPut(wb)
					if err != nil {
						return err
					}
					count = 0
					wb = make([]storage.Write, 0, self.writeBatchSize)
				}
			}
		}
	}

	return self.db.BatchPut(wb)
}
开发者ID:ericcapricorn,项目名称:influxdb,代码行数:53,代码来源:shard.go


示例15: zig_zag_decode64

func zig_zag_decode64(L *lua.LState) int {
	n := L.CheckNumber(1)
	b_buf := bytes.NewBuffer([]byte{})
	binary.Write(b_buf, binary.LittleEndian, n)
	b := proto.NewBuffer(b_buf.Bytes())
	value, err := b.DecodeZigzag64()
	if err == nil {
		L.Push(lua.LNumber(value))
		return 1
	}

	return 0
}
开发者ID:bodogbo,项目名称:OSG-Server-Go,代码行数:13,代码来源:lua_pb_auto.go


示例16: prepareBuf

func prepareBuf(b *testing.B) *proto.Buffer {
	buf := proto.NewBuffer(make([]byte, 0, 4096))
	v := &SampleValueSeries{Value: make([]*SampleValueSeries_Value, 0, numSamples)}
	for i := 0; i < numSamples; i++ {
		v.Value = append(v.Value, &SampleValueSeries_Value{
			Timestamp: proto.Int64(rand.Int63()),
			Value:     proto.Float64(rand.NormFloat64()),
		})
	}
	if err := buf.Marshal(v); err != nil {
		b.Fatal(err)
	}
	return buf
}
开发者ID:juliusv,项目名称:bench_sample_formats,代码行数:14,代码来源:proto_test.go


示例17: receiveMsg

func receiveMsg(conn net.Conn) {
	buf := make([]byte, 128)
	n, err := conn.Read(buf)
	if checkerr(err) {
		os.Exit(-1)
		return
	}
	// fmt.Printf("\nread (%d) byte from %v :\n%v\n", n, conn.RemoteAddr(), buf[:n])

	protobuf := proto.NewBuffer(buf[:n])
	var _msg msg.OSMsg
	pumerr := protobuf.Unmarshal(&_msg)
	if checkerr(pumerr) {
		return
	}
	fmt.Printf("[received <----- message]%v\n", _msg.String())
}
开发者ID:shaalx,项目名称:oschat,代码行数:17,代码来源:main.go


示例18: CreateHekaStream

func CreateHekaStream(msgBytes []byte, outBytes *[]byte,
	msc *message.MessageSigningConfig) error {

	h := &message.Header{}
	h.SetMessageLength(uint32(len(msgBytes)))
	if msc != nil {
		h.SetHmacSigner(msc.Name)
		h.SetHmacKeyVersion(msc.Version)
		var hm hash.Hash
		switch msc.Hash {
		case "sha1":
			hm = hmac.New(sha1.New, []byte(msc.Key))
			h.SetHmacHashFunction(message.Header_SHA1)
		default:
			hm = hmac.New(md5.New, []byte(msc.Key))
		}

		hm.Write(msgBytes)
		h.SetHmac(hm.Sum(nil))
	}
	headerSize := proto.Size(h)
	requiredSize := message.HEADER_FRAMING_SIZE + headerSize + len(msgBytes)
	if requiredSize > message.MAX_RECORD_SIZE {
		return fmt.Errorf("Message too big, requires %d (MAX_RECORD_SIZE = %d)",
			requiredSize, message.MAX_RECORD_SIZE)
	}
	if cap(*outBytes) < requiredSize {
		*outBytes = make([]byte, requiredSize)
	} else {
		*outBytes = (*outBytes)[:requiredSize]
	}
	(*outBytes)[0] = message.RECORD_SEPARATOR
	(*outBytes)[1] = uint8(headerSize)
	// This looks odd but is correct; it effectively "seeks" the initial write
	// position for the protobuf output to be at the
	// `(*outBytes)[message.HEADER_DELIMITER_SIZE]` position.
	pbuf := proto.NewBuffer((*outBytes)[message.HEADER_DELIMITER_SIZE:message.HEADER_DELIMITER_SIZE])
	if err := pbuf.Marshal(h); err != nil {
		return err
	}
	(*outBytes)[headerSize+message.HEADER_DELIMITER_SIZE] = message.UNIT_SEPARATOR
	copy((*outBytes)[message.HEADER_FRAMING_SIZE+headerSize:], msgBytes)
	return nil
}
开发者ID:RogerBai,项目名称:heka,代码行数:44,代码来源:encoders.go


示例19: createStream

func createStream(msgBytes []byte, encoding message.Header_MessageEncoding,
	outBytes *[]byte, msc *message.MessageSigningConfig) error {
	h := &message.Header{}
	h.SetMessageLength(uint32(len(msgBytes)))
	if encoding != message.Default_Header_MessageEncoding {
		h.SetMessageEncoding(encoding)
	}
	if msc != nil {
		h.SetHmacSigner(msc.Name)
		h.SetHmacKeyVersion(msc.Version)
		var hm hash.Hash
		switch msc.Hash {
		case "sha1":
			hm = hmac.New(sha1.New, []byte(msc.Key))
			h.SetHmacHashFunction(message.Header_SHA1)
		default:
			hm = hmac.New(md5.New, []byte(msc.Key))
		}

		hm.Write(msgBytes)
		h.SetHmac(hm.Sum(nil))
	}
	headerSize := uint8(proto.Size(h))
	requiredSize := int(3 + headerSize)
	if cap(*outBytes) < requiredSize {
		*outBytes = make([]byte, requiredSize, requiredSize+len(msgBytes))
	} else {
		*outBytes = (*outBytes)[:requiredSize]
	}
	(*outBytes)[0] = message.RECORD_SEPARATOR
	(*outBytes)[1] = uint8(headerSize)
	pbuf := proto.NewBuffer((*outBytes)[2:2])
	err := pbuf.Marshal(h)
	if err != nil {
		return err
	}
	(*outBytes)[headerSize+2] = message.UNIT_SEPARATOR
	*outBytes = append(*outBytes, msgBytes...)
	return nil
}
开发者ID:hellcoderz,项目名称:heka,代码行数:40,代码来源:encoders.go


示例20: NewBufferPair

func NewBufferPair() *bufferPair {
	return &bufferPair{proto.NewBuffer(nil), proto.NewBuffer(nil)}
}
开发者ID:yanatan16,项目名称:protorpc,代码行数:3,代码来源:transport.go



注:本文中的code/google/com/p/goprotobuf/proto.NewBuffer函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang proto.RegisterEnum函数代码示例发布时间:2022-05-24
下一篇:
Golang proto.MarshalText函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap