| | |
| | | |
| | | class ShmSocket : private boost::noncopyable |
| | | { |
| | | bool SendImpl(const void *valid_remote, const MsgI &imsg, SendQ::OnMsgEvent onExpire = SendQ::OnMsgEvent()) |
| | | bool SendImpl(const void *valid_remote, MsgI const &imsg, SendQ::OnMsgEvent onExpire = SendQ::OnMsgEvent()) |
| | | { |
| | | send_buffer_->Append(*static_cast<const MQId *>(valid_remote), imsg, onExpire); |
| | | // if (!mq().TrySend(*(MQId *) valid_remote, imsg)) { |
| | | send_buffer_.Append(*static_cast<const MQId *>(valid_remote), imsg, onExpire); |
| | | // } |
| | | return true; |
| | | } |
| | | |
| | |
| | | ShmSocket(Shm &shm, const int len = 12); |
| | | ~ShmSocket(); |
| | | static bool Remove(SharedMemory &shm, const MQId &id) { return Queue::Remove(shm, id); } |
| | | bool Remove() { return Remove(shm(), id()); } |
| | | const MQId &id() const { return mq().Id(); } |
| | | // start recv. |
| | | bool Start(int nworker = 1, const RecvCB &onData = RecvCB(), const IdleCB &onIdle = IdleCB()); |
| | |
| | | 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; |
| | | return msg.Make(shm(), head, body) && SendImpl(valid_remote, msg); |
| | | } |
| | | |
| | | 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 { |
| | | SetLastError(ENOMEM, "Out of mem"); |
| | | } |
| | | return false; |
| | | } |
| | |
| | | }; |
| | | |
| | | Synced<AsyncCBs> per_msg_cbs_; |
| | | Synced<SendQ> send_buffer_; |
| | | SendQ send_buffer_; |
| | | // Synced<SendQ> send_buffer_; |
| | | }; |
| | | |
| | | #endif // end of include guard: SOCKET_GWTJHBPO |