Linuxc/C ++ programming BASICS (27) "Heartbeat" Information Processing

Source: Internet
Author: User

Preface: add the downstream data

1. Implement the handle function as follows:

# Include "jsoncpp/JSON. H"
# Include "downstreamhandler. H"
Void downstreamhandler: handle (clientsession * client, const char * pack, int length ){
JSON: Value V;
JSON: reader;
If (reader. parse (pack, V )){
Int uri = V ["Uri"]. asuint ();
STD: String MSG = V ["MSG"]. asstring ();
# Ifdef open_log
Printf ("[downstreamhandler: handle] URI is % d \ n", Uri );
Printf ("[downstreamhandler: handle] MSG is % s \ n", MSG. c_str ());
# Endif
Switch (URI ){
Case ploginres:
Onloginres (client, pack, length );
Break;
Case ppingres:
Onpingres (client, pack, length );
Break;
Default:
Printf ("[downstreamhandler: handle] unknown URI: % d \ n", Uri );
Break;
}
}
}

2. Implementation of the login information response function:
Void downstreamhandler: onloginres (clientsession * client, const char * pack, int length ){
# Ifdef open_log
Printf ("[downstreamhandler: onloginres] ~~~~~~~~~ Invoke ~~~~~~~~~ \ N ");
# Endif
JSON: Value JV;
JSON: fastwriter writer;
Int uri = 3;
STD: String MSG = "USTC ";
JV ["Uri"] = JSON: Value (URI );
JV ["MSG"] = JSON: Value (MSG );
STD: String loginres = writer. Write (jv );
Client-> senddata (loginres );
}
Description: JSON is an open-source library that returns response information in JSON format.
3. Implement the ping heartbeat response function as follows:
Void downstreamhandler: onpingres (clientsession * client, const char * pack, int length ){
# Ifdef open_log
Printf ("[downstreamhandler: onpingres] ~~~~~~~~~ Invoke ~~~~~~~~~ \ N ");
# Endif
JSON: Value JV;
JSON: fastwriter writer;
Int uri = 5;
STD: String MSG = "linyanwen ";
JV ["Uri"] = JSON: Value (URI );
JV ["MSG"] = JSON: Value (MSG );
STD: String pingres = writer. Write (jv );
Client-> senddata (pingres );
}

Description: JSON is an open-source library that returns response information in JSON format.


~ To be continued

Reprinted please indicate the source: zhujian blog, http://blog.csdn.net/linyanwen99/article/details/8315883

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.