From 94a455aba299af5ffe476560d859dfd007cd5467 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期五, 16 四月 2021 12:13:43 +0800 Subject: [PATCH] fix crash by using normal timeout; add sendq todo. --- src/socket.h | 32 ++++++++++++-------------------- 1 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/socket.h b/src/socket.h index 96af6e7..66f716e 100644 --- a/src/socket.h +++ b/src/socket.h @@ -66,32 +66,24 @@ size_t Pending() const { return mq().Pending(); } template <class Body> - bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body) + bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const RecvCB &cb = RecvCB()) { - MsgI msg; - if (msg.Make(shm(), head, body)) { - DEFER1(if (msg.IsCounted()) { msg.Release(shm()); }); - return SendImpl(valid_remote, msg); - } - return false; - } - - template <class Body> - bool Send(const void *valid_remote, const BHMsgHead &head, const Body &body, const RecvCB &cb) - { - //TODO send_buffer_ need flag, and remove callback on expire. MsgI msg; if (msg.Make(shm(), head, body)) { DEFER1(if (msg.IsCounted()) { msg.Release(shm()); }); std::string msg_id(head.msg_id()); - per_msg_cbs_->Add(msg_id, cb); - auto onExpireRemoveCB = [this, msg_id](MsgI const &msg) { - RecvCB cb_no_use; - per_msg_cbs_->Find(msg_id, cb_no_use); - }; - return SendImpl(valid_remote, msg, onExpireRemoveCB); + if (!cb) { + return SendImpl(valid_remote, msg); + } else { + per_msg_cbs_->Add(msg_id, cb); + auto onExpireRemoveCB = [this, msg_id](MsgI const &msg) { + RecvCB cb_no_use; + per_msg_cbs_->Find(msg_id, cb_no_use); + }; + return SendImpl(valid_remote, msg, onExpireRemoveCB); + } } else { - printf("out of mem?, avail: %ld\n", shm().get_free_memory()); + SetLastError(ENOMEM, "Out of mem"); } return false; } -- Gitblit v1.8.0