當前位置: 首頁>>代碼示例>>Golang>>正文


Golang proto.NewBuffer函數代碼示例

本文整理匯總了Golang中code/google/com/p/goprotobuf/proto.NewBuffer函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewBuffer函數的具體用法?Golang NewBuffer怎麽用?Golang NewBuffer使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewBuffer函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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


注:本文中的code/google/com/p/goprotobuf/proto.NewBuffer函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。