亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 C 庫中檢索 probuf

如何從 C 庫中檢索 probuf

Go
精慕HU 2023-07-10 18:01:39
我正在嘗試使用 Google Protocol Buffers 與 Go 中的 C 庫進行通信,但無法使其工作。我在嘗試將 protobuf 發送到 C 庫時遇到錯誤我將發布最小代碼來重現我現在面臨的錯誤(我已刪除了大部分 C 部分,因為它與此錯誤無關):    /*    #cgo CFLAGS: -I@CURRENT_SOURCE_DIR@/../../library/crnd/include -     I@CMAKE_CURRENT_BINARY_DIR@/../../library    #cgo LDFLAGS: -L@CRND_LIBRARY_PATH@ -lcrnd    #include <api_c.h>    #include <stdint.h>    #include <stdio.h>    #include <inttypes.h>    typedef struct SerializedStruct {        void* data;        int64_t size;    } Serialized;    */    import "C"    func (crnd wrapper) MyFunction() {        // Create a protobuf        sample_request := &messages.SampleRequest { ... add members ...}        fmt.Printf(proto.MarshalTextString(sample_request));  // print the message to check it works        data, err := proto.Marshal(sample_request)        if err != nil {            log.Fatal("marshaling error: ", err)        }        sample_request_serialized := C.Serialized {            data: pointer.Save(data),            size: C.int64_t(len(data)),        }        // Here I would send my protobuf to the C library, but it is not        //  working, so I'm trying to reconstruct the protobuf to check        //  if something is broken before getting to the C side        // Let's reconstruct the protobuf back        data_req := (*[1 << 30]byte)(sample_request_serialized.data)[:int(sample_request_serialized.size):int(sample_request_serialized.size)]        sample_req := &messages.SampleRequest{}        err := proto.Unmarshal(data_req, sample_req)        if err != nil {            log.Fatal("unmarshaling error: ", err)        }    }我收到以下錯誤:unmarshaling error: proto: crnd.SampleRequest: illegal tag 0 (wire type 0)
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

我設法讓它工作創建緩沖區的副本:

...


? ? p := C.malloc(C.size_t(len(data)))

? ? defer C.free(p)

? ? cBuf := (*[1 << 30]byte)(p)

? ? copy(cBuf[:], data)


? ? sample_request_serialized := &C.Serialized {

? ? ? ? data: p,

? ? ? ? size: C.int64_t(len(data)),

? ? }


查看完整回答
反對 回復 2023-07-10
  • 1 回答
  • 0 關注
  • 181 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號