有人可以给我一个包装\解包消息的示例,并在protobuf-c中重复显示子消息吗?
empty关于此事的官方文件。
答案 0 :(得分:2)
message B {
required int64 i = 1;
};
message A {
repeated B numbers = 1;
};
A a;
a.ParseFromString(<str>)
for (int i = 0; i < a.numbers_size(); ++i) {
const B& b = a.numbers(i);
printf("%d\n", b.i());
}
答案 1 :(得分:0)
原型:
java.lang.IllegalArgumentException: Invalid primitive conversion from boolean to int
at java.lang.reflect.Field.getInt(Native Method)
at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:946)
at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
at android.os.Parcel.writeSerializable(Parcel.java:1442)
at android.os.Parcel.writeValue(Parcel.java:1395)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.os.Parcel.writeValue(Parcel.java:1319)
at android.os.Parcel.writeSparseArray(Parcel.java:778)
at android.os.Parcel.writeValue(Parcel.java:1347)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at androidx.fragment.app.FragmentState.writeToParcel(FragmentState.java:127)
at android.os.Parcel.writeTypedList(Parcel.java:1148)
at androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51)
at android.os.Parcel.writeParcelable(Parcel.java:1416)
at android.os.Parcel.writeValue(Parcel.java:1322)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:665)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1330)
at android.os.Bundle.writeToParcel(Bundle.java:1079)
at android.os.Parcel.writeBundle(Parcel.java:690)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3152)
at java.lang.reflect.Method.invoke(Native Method)
at f.r.c.i.a.e.invoke(ActivityManagerHook.java:89)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy7.activityStopped(Unknown Source)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3417)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
pack(不要忘记释放内存(free(...)):
message SubMsg {
bytes value = 1;
}
message MainMsg {
repeated SubMsg msgs = 1;
}
解压缩(不要忘记释放内存(..._ free_unpacked(...)):
MainMsg mainMsg = { };
main_msg__init(&mainMsg);
SubMsg **subMsgs = calloc(LEN, sizeof(SubMsg*));
for (int i = 0; i < LEN; i++) {
subMsgs[i] = calloc(1, sizeof(SubMsg));
sub_msg__init(subMsgs[i]);
subMsgs[i]->value.data = DATA;
subMsgs[i]->value.len = DATALEN;
}
mainMsg.msgs = subMsgs;
mainMsg.n_msgs = LEN;
...
main_msg__pack(&mainMsg, BUFFER);
答案 2 :(得分:-2)
此外,STL的for_each似乎也可行(语法糖,或多或少): 的std :: for_each的(a.numbers()()开始 ,a.numbers()结束。() ,[](const B&amp; b){ std :: cout&lt;&lt; b.i()&lt;&lt; std :: endl; } );