本文整理汇总了Golang中github.com/golang/protobuf/proto.DecodeVarint函数的典型用法代码示例。如果您正苦于以下问题:Golang DecodeVarint函数的具体用法?Golang DecodeVarint怎么用?Golang DecodeVarint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DecodeVarint函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: decodeValue
func decodeValue(encodedValue []byte) ([]byte, uint64) {
version, len1 := proto.DecodeVarint(encodedValue)
deleteMarker, len2 := proto.DecodeVarint(encodedValue[len1:])
if deleteMarker == 1 {
return nil, version
}
value := encodedValue[len1+len2:]
return value, version
}
开发者ID:hyperledger,项目名称:fabric,代码行数:9,代码来源:couchdb_txmgr.go
示例2: processMessages
func (gaugeListener *GaugeListener) processMessages(buffer *bytes.Buffer) {
for {
messageLength, bytesRead := proto.DecodeVarint(buffer.Bytes())
if messageLength > 0 && messageLength < uint64(buffer.Len()) {
message := &gauge_messages.Message{}
messageBoundary := int(messageLength) + bytesRead
err := proto.Unmarshal(buffer.Bytes()[bytesRead:messageBoundary], message)
if err != nil {
log.Printf("Failed to read proto message: %s\n", err.Error())
} else {
if *message.MessageType == gauge_messages.Message_KillProcessRequest {
gaugeListener.connection.Close()
os.Exit(0)
}
if *message.MessageType == gauge_messages.Message_SuiteExecutionResult {
result := message.GetSuiteExecutionResult()
gaugeListener.onResultHandler(result)
}
buffer.Next(messageBoundary)
if buffer.Len() == 0 {
return
}
}
} else {
return
}
}
}
开发者ID:jean,项目名称:html-report,代码行数:28,代码来源:gaugeListener.go
示例3: listen
func (client *Client) listen() {
buffer := new(bytes.Buffer)
data := make([]byte, 8192)
for {
n, err := client.Conn.Read(data)
if err != nil {
if err == io.EOF {
log.Println("Client exited")
return
}
log.Println(err.Error())
}
buffer.Write(data[0:n])
messageLength, bytesRead := proto.DecodeVarint(buffer.Bytes())
if messageLength > 0 && messageLength < uint64(buffer.Len()) {
response := &limitd.Response{}
err = proto.Unmarshal(buffer.Bytes()[bytesRead:messageLength+uint64(bytesRead)], response)
if err != nil {
log.Printf("Failed to read proto response: %s\n", err.Error())
} else {
responseChannel := client.PendingRequests[*response.RequestId]
responseChannel <- response
delete(client.PendingRequests, *response.RequestId)
buffer.Reset()
}
}
}
}
开发者ID:limitd,项目名称:go-client,代码行数:34,代码来源:limitd.go
示例4: DecodeOrderPreservingVarUint64
// DecodeOrderPreservingVarUint64 decodes the number from the bytes obtained from method 'EncodeOrderPreservingVarUint64'.
// Also, returns the number of bytes that are consumed in the process
func DecodeOrderPreservingVarUint64(bytes []byte) (uint64, int) {
s, _ := proto.DecodeVarint(bytes)
size := int(s)
decodedBytes := make([]byte, 8)
copy(decodedBytes[8-size:], bytes[1:size+1])
numBytesConsumed := size + 1
return binary.BigEndian.Uint64(decodedBytes), numBytesConsumed
}
开发者ID:butine,项目名称:research,代码行数:10,代码来源:util.go
示例5: newDataKeyFromEncodedBytes
func newDataKeyFromEncodedBytes(encodedBytes []byte) *dataKey {
bucketNum, l := proto.DecodeVarint(encodedBytes)
if !bytes.Equal(encodedBytes[l:l+1], []byte{byte(0)}) {
panic(fmt.Errorf("[%#v] is not a valid data key", encodedBytes))
}
compositeKey := encodedBytes[l+1:]
return &dataKey{newBucketKeyAtLowestLevel(int(bucketNum)), compositeKey}
}
开发者ID:masterDev1985,项目名称:obc-peer,代码行数:8,代码来源:data_key.go
示例6: fetchTransaction
func (mgr *blockfileMgr) fetchTransaction(lp *fileLocPointer) (*pb.Transaction, error) {
var err error
var txEnvelopeBytes []byte
if txEnvelopeBytes, err = mgr.fetchRawBytes(lp); err != nil {
return nil, err
}
_, n := proto.DecodeVarint(txEnvelopeBytes)
return extractTransaction(txEnvelopeBytes[n:])
}
开发者ID:hyperledger,项目名称:fabric,代码行数:9,代码来源:blockfile_mgr.go
示例7: readDelimited
// readDelimited decodes a message from the provided length-delimited stream,
// where the length is encoded as 32-bit varint prefix to the message body.
// It returns the total number of bytes read and any applicable error. This is
// roughly equivalent to the companion Java API's
// MessageLite#parseDelimitedFrom. As per the reader contract, this function
// calls r.Read repeatedly as required until exactly one message including its
// prefix is read and decoded (or an error has occurred). The function never
// reads more bytes from the stream than required. The function never returns
// an error if a message has been read and decoded correctly, even if the end
// of the stream has been reached in doing so. In that case, any subsequent
// calls return (0, io.EOF).
func readDelimited(r io.Reader, m proto.Message, base64Decode bool, newline bool) (int, error) {
// Per AbstractParser#parsePartialDelimitedFrom with
// CodedInputStream#readRawVarint32.
headerBuf := make([]byte, binary.MaxVarintLen32)
var bytesRead, varIntBytes int
var messageLength uint64
for varIntBytes == 0 { // i.e. no varint has been decoded yet.
if bytesRead >= len(headerBuf) {
return bytesRead, errInvalidVarint
}
// We have to read byte by byte here to avoid reading more bytes
// than required. Each read byte is appended to what we have
// read before.
newBytesRead, err := r.Read(headerBuf[bytesRead : bytesRead+1])
if newBytesRead == 0 {
if err != nil {
return bytesRead, err
}
// A Reader should not return (0, nil), but if it does,
// it should be treated as no-op (according to the
// Reader contract). So let's go on...
continue
}
bytesRead += newBytesRead
// Now present everything read so far to the varint decoder and
// see if a varint can be decoded already.
messageLength, varIntBytes = proto.DecodeVarint(headerBuf[:bytesRead])
}
messageBuf := make([]byte, messageLength)
newBytesRead, err := io.ReadFull(r, messageBuf)
bytesRead += newBytesRead
if err != nil {
return bytesRead, err
}
if base64Decode {
messageBuf, err = base64.StdEncoding.DecodeString(string(messageBuf))
if err != nil {
return bytesRead, err
}
}
if newline {
newlineBuf := make([]byte, 1)
n, err := io.ReadFull(r, newlineBuf)
bytesRead += n
if err != nil {
return bytesRead, err
}
}
return bytesRead, proto.Unmarshal(messageBuf, m)
}
开发者ID:ModelRocket,项目名称:openstorage,代码行数:62,代码来源:io.go
示例8: Read
func (decoder *ProtobufDecoderV1) Read(ctx context.Context, req *http.Request) error {
body := req.Body
bufferedBody := bufio.NewReaderSize(body, 32768)
for {
log.WithField("body", body).Debug("Starting protobuf loop")
buf, err := bufferedBody.Peek(1)
if err == io.EOF {
log.Debug("EOF")
return nil
}
buf, err = bufferedBody.Peek(4) // should be big enough for any varint
if err != nil {
log.WithField("err", err).Info("peek error")
return err
}
num, bytesRead := proto.DecodeVarint(buf)
if bytesRead == 0 {
// Invalid varint?
return errInvalidProtobufVarint
}
if num > 32768 {
// Sanity check
return errProtobufTooLarge
}
// Get the varint out
buf = make([]byte, bytesRead)
io.ReadFull(bufferedBody, buf)
// Get the structure out
buf = make([]byte, num)
_, err = io.ReadFull(bufferedBody, buf)
if err != nil {
return fmt.Errorf("unable to fully read protobuf message: %s", err)
}
var msg com_signalfx_metrics_protobuf.DataPoint
err = proto.Unmarshal(buf, &msg)
if err != nil {
return err
}
if datapointProtobufIsInvalidForV1(&msg) {
return errInvalidProtobuf
}
mt := decoder.TypeGetter.GetMetricTypeFromMap(msg.GetMetric())
if dp, err := NewProtobufDataPointWithType(&msg, mt); err == nil {
decoder.Sink.AddDatapoints(ctx, []*datapoint.Datapoint{dp})
}
}
}
开发者ID:tomzhang,项目名称:metricproxy,代码行数:49,代码来源:signalfxlistener.go
示例9: processMessage
func (connectionHandler *GaugeConnectionHandler) processMessage(buffer *bytes.Buffer, conn net.Conn) {
for {
messageLength, bytesRead := proto.DecodeVarint(buffer.Bytes())
if messageLength > 0 && messageLength < uint64(buffer.Len()) {
messageBoundary := int(messageLength) + bytesRead
receivedBytes := buffer.Bytes()[bytesRead : messageLength+uint64(bytesRead)]
connectionHandler.messageHandler.MessageBytesReceived(receivedBytes, conn)
buffer.Next(messageBoundary)
if buffer.Len() == 0 {
return
}
} else {
return
}
}
}
开发者ID:andrewmkrug,项目名称:gauge,代码行数:16,代码来源:connectionHandler.go
示例10: readMessageBytes
func readMessageBytes(conn net.Conn) ([]byte, error) {
buffer := new(bytes.Buffer)
data := make([]byte, c.MaxMessageSize)
for {
n, err := conn.Read(data)
if err != nil {
conn.Close()
return nil, fmt.Errorf("Connection closed [%s] cause: %s", conn.RemoteAddr(), err.Error())
}
buffer.Write(data[0:n])
messageLength, bytesRead := proto.DecodeVarint(buffer.Bytes())
if messageLength > 0 && messageLength < uint64(buffer.Len()) {
return buffer.Bytes()[bytesRead : messageLength+uint64(bytesRead)], nil
}
}
}
开发者ID:manuviswam,项目名称:gauge-go,代码行数:18,代码来源:Reader.go
示例11: TestSerializedBlockInfo
func TestSerializedBlockInfo(t *testing.T) {
block := testutil.ConstructTestBlock(t, 10, 100)
bb, info, err := serializeBlock(block)
testutil.AssertNoError(t, err, "")
infoFromBB, err := extractSerializedBlockInfo(bb)
testutil.AssertNoError(t, err, "")
testutil.AssertEquals(t, infoFromBB, info)
testutil.AssertEquals(t, len(info.txOffsets), len(block.Data.Data))
for txIndex, txEnvBytes := range block.Data.Data {
txid, err := extractTxID(txEnvBytes)
testutil.AssertNoError(t, err, "")
indexInfo := info.txOffsets[txIndex]
indexTxID := indexInfo.txID
indexOffset := indexInfo.loc
testutil.AssertEquals(t, txid, indexTxID)
b := bb[indexOffset.offset:]
len, num := proto.DecodeVarint(b)
txEnvBytesFromBB := b[num : num+int(len)]
testutil.AssertEquals(t, txEnvBytesFromBB, txEnvBytes)
}
}
开发者ID:hyperledger,项目名称:fabric,代码行数:23,代码来源:block_serialization_test.go
示例12: receiveRpcs
func (c *Client) receiveRpcs() {
var sz [4]byte
for {
err := c.readFully(sz[:])
if err != nil {
c.setSendErr(err)
c.errorEncountered()
return
}
buf := make([]byte, binary.BigEndian.Uint32(sz[:]))
err = c.readFully(buf)
if err != nil {
c.setSendErr(err)
c.errorEncountered()
return
}
resp := &pb.ResponseHeader{}
respLen, nb := proto.DecodeVarint(buf)
buf = buf[nb:]
err = proto.UnmarshalMerge(buf[:respLen], resp)
buf = buf[respLen:]
if err != nil {
// Failed to deserialize the response header
c.setSendErr(err)
c.errorEncountered()
return
}
if resp.CallId == nil {
// Response doesn't have a call ID
log.Error("Response doesn't have a call ID!")
c.setSendErr(ErrMissingCallID)
c.errorEncountered()
return
}
c.sentRPCsMutex.Lock()
rpc, ok := c.sentRPCs[*resp.CallId]
c.sentRPCsMutex.Unlock()
if !ok {
log.WithFields(log.Fields{
"CallId": *resp.CallId,
}).Error("Received a response with an unexpected call ID")
log.Error("Waiting for responses to the following calls:")
c.sentRPCsMutex.Lock()
for id, call := range c.sentRPCs {
log.Errorf("\t\t%d: %v", id, call)
}
c.sentRPCsMutex.Unlock()
c.setSendErr(fmt.Errorf("HBase sent a response with an unexpected call ID: %d",
resp.CallId))
c.errorEncountered()
return
}
var rpcResp proto.Message
if resp.Exception == nil {
respLen, nb = proto.DecodeVarint(buf)
buf = buf[nb:]
rpcResp = rpc.NewResponse()
err = proto.UnmarshalMerge(buf, rpcResp)
buf = buf[respLen:]
} else {
javaClass := *resp.Exception.ExceptionClassName
err = fmt.Errorf("HBase Java exception %s: \n%s", javaClass,
*resp.Exception.StackTrace)
if _, ok := javaRetryableExceptions[javaClass]; ok {
// This is a recoverable error. The client should retry.
err = RetryableError{err}
}
}
rpc.GetResultChan() <- hrpc.RPCResult{Msg: rpcResp, Error: err}
c.sentRPCsMutex.Lock()
delete(c.sentRPCs, *resp.CallId)
c.sentRPCsMutex.Unlock()
}
}
开发者ID:jfrabaute,项目名称:gohbase,代码行数:82,代码来源:client.go
示例13: decodeBlockNum
func decodeBlockNum(blockNumBytes []byte) uint64 {
blockNum, _ := proto.DecodeVarint(blockNumBytes)
return blockNum
}
开发者ID:hyperledger,项目名称:fabric,代码行数:4,代码来源:blockindex.go
示例14: toInt
func toInt(balanceBytes []byte) int {
v, _ := proto.DecodeVarint(balanceBytes)
return int(v)
}
开发者ID:hyperledger,项目名称:fabric,代码行数:4,代码来源:app.go
示例15: decodeBlockNumber
func decodeBlockNumber(blockNumberBytes []byte) (blockNumber uint64) {
blockNumber, _ = proto.DecodeVarint(blockNumberBytes)
return
}
开发者ID:masterDev1985,项目名称:obc-peer,代码行数:4,代码来源:blockchain_indexes.go
注:本文中的github.com/golang/protobuf/proto.DecodeVarint函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论