protobuf repeated 类型与C++的Vector,list类似,这里可以理解为指定类型的数组。
首先定义一个protobuf结构,如下:
//进入房间(广播)
message Actor_GamerEnterRoom_Ntt // IActorMessage
{
int32 RpcId = 90;
int64 ActorId = 93;
repeated GamerInfo Gamers = 1;
}
下面我们以例子简单说明如何使用:
public partial class Actor_GamerEnterRoom_Ntt : pb::IMessage {
private static readonly pb::MessageParser<Actor_GamerEnterRoom_Ntt> _parser = new pb::MessageParser<Actor_GamerEnterRoom_Ntt>(() => (Actor_GamerEnterRoom_Ntt)MessagePool.Instance.Fetch(typeof(Actor_GamerEnterRoom_Ntt)));
public static pb::MessageParser<Actor_GamerEnterRoom_Ntt> Parser { get { return _parser; } }
private int rpcId_;
public int RpcId {
get { return rpcId_; }
set {
rpcId_ = value;
}
}
private long actorId_;
public long ActorId {
get { return actorId_; }
set {
actorId_ = value;
}
}
private static readonly pb::FieldCodec<global::ETModel.GamerInfo> _repeated_gamers_codec
= pb::FieldCodec.ForMessage(10, global::ETModel.GamerInfo.Parser);
private pbc::RepeatedField<global::ETModel.GamerInfo> gamers_ = new pbc::RepeatedField<global::ETModel.GamerInfo>();
public pbc::RepeatedField<global::ETModel.GamerInfo> Gamers {
get { return gamers_; }
set { gamers_ = value; }
}
public void WriteTo(pb::CodedOutputStream output) {
gamers_.WriteTo(output, _repeated_gamers_codec);
if (RpcId != 0) {
output.WriteRawTag(208, 5);
output.WriteInt32(RpcId);
}
if (ActorId != 0L) {
output.WriteRawTag(232, 5);
output.WriteInt64(ActorId);
}
}
public int CalculateSize() {
int size = 0;
if (RpcId != 0) {
size += 2 + pb::CodedOutputStream.ComputeInt32Size(RpcId);
}
if (ActorId != 0L) {
size += 2 + pb::CodedOutputStream.ComputeInt64Size(ActorId);
}
size += gamers_.CalculateSize(_repeated_gamers_codec);
return size;
}
public void MergeFrom(pb::CodedInputStream input) {
for (int i = 0; i < gamers_.Count; i++) { MessagePool.Instance.Recycle(gamers_[i]); }
gamers_.Clear();
rpcId_ = 0;
actorId_ = 0;
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
input.SkipLastField();
break;
case 10: {
gamers_.AddEntriesFrom(input, _repeated_gamers_codec);
break;
}
case 720: {
RpcId = input.ReadInt32();
break;
}
case 744: {
ActorId = input.ReadInt64();
break;
}
}
}
}
}