Created
February 18, 2017 05:04
-
-
Save sdamashek/aaf7b315ff01382d84ed29d08da8e391 to your computer and use it in GitHub Desktop.
Protobuf lengths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from google.protobuf.internal.decoder import _DecodeVarint | |
from google.protobuf.internal.encoder import _VarintBytes | |
import datapoint_pb2 | |
def read_message(buf): # buf is a bytestring | |
pos = 0 # Start of varint32 | |
value, new_pos = _DecodeVarint(buf, pos) | |
msg = datapoint_pb2.DataPoint() | |
msg.ParseFromString(buf[new_pos:new_pos+value]) | |
return msg | |
def write_message(msg, stream): | |
message = msg.SerializeToString() | |
delimiter = _VarintBytes(len(message)) | |
stream.write(delimter + message) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment