提交 7ba0e697 编写于 作者: Y yang li

More comments in demos to help you understand passive receiving and message handling more.

上级 06e0cdd2
......@@ -123,6 +123,8 @@ protected:
//except we can bear message disordering.
return 1;
//if we indeed handled some messages, do return 1
//if we handled nothing, return 1 is also okey but will very slightly impact performance (if msg_can is not empty), return 0 is suggested
}
#endif
#ifdef ASCS_DISPATCH_BATCH_MSG
......@@ -136,6 +138,8 @@ protected:
ascs::do_something_to_all(tmp_can, [this](out_msg_type& msg) {this->handle_msg(msg);});
return 1;
//if we indeed handled some messages, do return 1
//if we handled nothing, return 1 is also okey but will very slightly impact performance, return 0 is suggested
}
#else
virtual bool on_msg_handle(out_msg_type& msg) {handle_msg(msg); return true;}
......
......@@ -117,6 +117,8 @@ protected:
msg_can.clear();
return 1;
//if we indeed handled some messages, do return 1
//if we handled nothing, return 1 is also okey but will very slightly impact performance (if msg_can is not empty), return 0 is suggested
}
#endif
......@@ -135,6 +137,8 @@ protected:
//following statement can avoid one memory replication if the type of out_msg_type and in_msg_type are identical.
ascs::do_something_to_all(tmp_can, [this](out_msg_type& msg) {this->send_msg(std::move(msg), true);});
return 1;
//if we indeed handled some messages, do return 1
//if we handled nothing, return 1 is also okey but will very slightly impact performance, return 0 is suggested
}
#else
//following statement can avoid one memory replication if the type of out_msg_type and in_msg_type are identical.
......
......@@ -65,9 +65,46 @@ public:
protected:
//msg handling
#ifdef ASCS_SYNC_DISPATCH
virtual size_t on_msg(list<out_msg_type>& msg_can)
{
//ascs will never append empty message automatically for on_msg (if no message nor error returned from the unpacker) even with
// macro ASCS_PASSIVE_RECV, but will do it for on_msg_handle (with macro ASCS_PASSIVE_RECV), please note.
if (msg_can.empty())
handle_msg(out_msg_type()); //we need empty message as a notification, it's just our business logic.
else
{
ascs::do_something_to_all(msg_can, [this](out_msg_type& msg) {this->handle_msg(msg);});
msg_can.clear();
}
recv_msg(); //we always handled all messages, so calling recv_msg() at here is very reasonable.
return 1;
//if we indeed handled some messages, do return 1
//if we handled nothing, return 1 is also okey but will very slightly impact performance (if msg_can is not empty), return 0 is suggested
}
#endif
#ifdef ASCS_DISPATCH_BATCH_MSG
virtual size_t on_msg_handle(out_queue_type& msg_can)
{
//msg_can can't be empty, with macro ASCS_PASSIVE_RECV, ascs will append an empty message automatically for on_msg_handle if no message nor
// error returned from the unpacker to provide a chance to call recv_msg (calling recv_msg out of on_msg and on_msg_handle is forbidden), please note.
assert(!msg_can.empty());
out_container_type tmp_can;
msg_can.swap(tmp_can);
ascs::do_something_to_all(tmp_can, [this](out_msg_type& msg) {this->handle_msg(msg);});
recv_msg(); //we always handled all messages, so calling recv_msg() at here is very reasonable.
return 1;
//if we indeed handled some messages, do return 1
//if we handled nothing, return 1 is also okey but will very slightly impact performance, return 0 is suggested
}
#else
virtual bool on_msg_handle(out_msg_type& msg) {handle_msg(msg); if (0 == get_pending_recv_msg_size()) recv_msg(); return true;}
//only raise recv_msg() invocation after recveiving buffer becomes empty, it's very important, otherwise we must use mutex to guarantee that at any time,
//there only exists one or zero asynchronous reception.
#endif
//msg handling end
virtual void on_connect()
......
......@@ -114,6 +114,8 @@ public:
{
if (0 == size_in_byte)
size_in_byte = ascs::get_size_in_byte(src);
else
assert(ascs::get_size_in_byte(src) == size_in_byte);
this->splice(this->end(), src);
total_size += size_in_byte;
......
......@@ -269,7 +269,7 @@ private:
on_unpack_error(); //the user will decide whether to reset the unpacker or not in this callback
#ifdef ASCS_PASSIVE_RECV
reading = false; //clear reading flag before call handle_msg() to make sure that recv_msg() can be called successfully in on_msg_handle()
reading = false; //clear reading flag before calling handle_msg() to make sure that recv_msg() is available in on_msg() and on_msg_handle()
#endif
if (handle_msg()) //if macro ASCS_PASSIVE_RECV been defined, handle_msg will always return false
do_recv_msg(); //receive msg in sequence
......@@ -277,7 +277,7 @@ private:
else
{
#ifdef ASCS_PASSIVE_RECV
reading = false; //clear reading flag before call handle_msg() to make sure that recv_msg() can be called successfully in on_msg_handle()
reading = false; //clear reading flag before calling handle_msg() to make sure that recv_msg() is available in on_msg() and on_msg_handle()
#endif
if (ec)
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册