cpp-ipc icon indicating copy to clipboard operation
cpp-ipc copied to clipboard

Best approach for handling struct / class

Open Petingo opened this issue 9 months ago • 1 comments

您好,

最近有個專案需要高效能的 IPC 於是嘗試了一下這個庫,感覺相當不錯! 但目前有個問題,就是他經常會報錯 fail: send, there is no receiver on this connection.,但我看起來資料是有正確被接收到的,想請問有什麼原因可能造成這個問題?

另外想請問一下,傳送 struct / class 最好的方式是什麼?目前是用以下的 code,但我的理解是這樣其實是需要一次 copy 的?是否有辦法做到 zero copy?

// sender
while(1) {
    // generate values...

    Message msg(value1, value2);
    outputRoute.wait_for_recv(1);
    outputRoute.send(ipc::buff_t(&msg, sizeof(msg)));
}
// receiver
while(1) {
    auto buf = m_inputRoute.recv();
    auto newMsg = static_cast<Message*>(buf.data());
    if (newMsg == nullptr) continue;

    // process data ...
}

非常感謝!

P.S. 測試平台是 windows 11

Petingo avatar Mar 14 '25 09:03 Petingo

嗯。。现在这个库由于维护了一个粗糙的连接概念,所以对意外退出很不友好。。必须在退出之前保证清理操作。 fail: send, there is no receiver on this connection. 的报错一般是由于某次意外退出导致的。

当前库内的共享内存池并没有提供给外部当作逻辑数据结构使用的接口,而仅作为传递过程来使用,因此确实一定会有一次拷贝。

mutouyun avatar Mar 15 '25 05:03 mutouyun