C Programer  - 讨论区

标题:使用cxxtools开发json rpc服务

2014年01月28日 星期二 11:34

cxxtools是一组内容非常丰富的C++ Library,包含了json rpc的服务端和客户端的实现。

json rpc并没有约束底层传输协议,传输层可以是http,也可以是raw tcp socket,很明显,后者由于协议开销小,运行效率会更高。

如下代码示例演示了使用cxxtools开发json rpc over raw tcp socket的用法。

Person.h

#include <string>
#include <vector>
#include <cxxtools/serializationinfo.h>

struct Person {
    int age;
    std::string name;
    std::vector<std::string> emails;
};

inline void operator <<= 
    (cxxtools::SerializationInfo &si, const Person & p) {
    si.addMember("age") <<= p.age;
    si.addMember("name") <<= p.name;
    si.addMember("emails") <<= p.emails;
}

inline void operator >>= 
    (const cxxtools::SerializationInfo &si,Person &p) {
    si.getMember("age") >>= p.age;
    si.getMember("name") >>= p.name;
    si.getMember("emails") >>= p.emails;
}

 

jsonrpcserver.cpp

#include <iostream>
#include <vector>
#include <map>
#include <cxxtools/eventloop.h>
#include <cxxtools/json/rpcserver.h>
#include <cxxtools/serializationinfo.h>
#include "Person.h"

using namespace std;
using namespace cxxtools;

class calc {
public:
    calc() {};
    ~calc() {};
    static int sum(int a,int b) {
        return a+b;
    }
    static int sub(int a,int b) {
        return a-b;
    }
    static int sum_all(vector<int> v) {
        int result=0;
        for(unsigned int i=0;i<v.size();i++) {
            result += v[i];
        } 
        return result;
    }
    static int sum_kv(map<string,int> m) {
        int result=0;
        map<string,int>::iterator it;
        for(it=m.begin();it!=m.end();it++) {
            result += it->second;
        } 
        return result;
    }
    static int sum_age(Person p1,Person p2) {
        return p1.age + p2.age;
    }
    static int sum_all_age(vector<Person> vp) {
        int result=0;
        for(unsigned int i=0;i<vp.size();i++) {
            result += vp[i].age;
        }
        return result;
    }
};

int main(int argc,char **argv) {
    
    try {
        EventLoop loop;
        json::RpcServer srv(loop,9999);
        srv.registerFunction("calc_sum",calc::sum);
        srv.registerFunction("calc_sub",calc::sub);
        srv.registerFunction("calc_sum_all",calc::sum_all);
        srv.registerFunction("calc_sum_kv",calc::sum_kv);
        srv.registerFunction("calc_sum_age",calc::sum_age);
        srv.registerFunction("calc_sum_all_age",calc::sum_all_age);
        loop.run();

    } catch (const exception &e) {
        cout << "ERROR: " << e.what() << endl;
    }
    return 0;
}

 

jsonrpcclient.cpp

#include <iostream>
#include <cxxtools/json/rpcclient.h>
#include <cxxtools/remoteprocedure.h>
#include <vector>
#include <map>
#include "Person.h"

using namespace std;
using namespace cxxtools;

int main(int argc,char **argv) {
    
    try {
        json::RpcClient cli("10.1.1.161",9999);
        RemoteProcedure<int,int,int> 
            calc_sum(cli,"calc_sum");
        RemoteProcedure<int,int,int> 
            calc_sub(cli,"calc_sub");
        RemoteProcedure<int,vector<int> > 
            calc_sum_all(cli,"calc_sum_all");
        RemoteProcedure<int,map<string,int> > 
            calc_sum_kv(cli,"calc_sum_kv");
        RemoteProcedure<int,Person,Person > 
            calc_sum_age(cli,"calc_sum_age");
        RemoteProcedure<int,vector<Person> > 
            calc_sum_all_age(cli,"calc_sum_all_age");

        /*
        for(int i=0;i<10000;i++) {
            if(i % 1000 == 0) {
                cout << i << endl;
            }
            calc_sum(100,200);
            calc_sub(200,100);
        }
        */
        cout << "100 + 200 = " << calc_sum(100,200) << endl;
        cout << "100 - 90 = " << calc_sub(100,90) << endl;
        vector<int> v;
        for(int i=1;i<=100;i++) {
            v.push_back(i);
        }
        cout << "1 + 2 + ... + 100 = " << calc_sum_all(v) << endl;

        map<string,int> m;
        m["mengguang"]=100;
        m["mengkang"]=200;
        cout << "sum of kv: " << calc_sum_kv(m) << endl;

        Person p1;
        p1.age=30;
        p1.name="mengguang";
        p1.emails.push_back("mengguang@gmail.com");
        p1.emails.push_back("xmgu2008@163.com");
        Person p2;
        p2.age=32;
        p2.name="mengkang";
        p2.emails.push_back("mengkang@gmail.com");
        p2.emails.push_back("mk2014@163.com");
        cout << "sum age: " << calc_sum_age(p1,p2) << endl;
        vector<Person> vp;
        vp.push_back(p1);
        vp.push_back(p2);
        cout << "sum all age: " << calc_sum_all_age(vp) << endl;

    } catch (const exception &e) {
        cout << "ERROR: " << e.what() << endl;
    }
    return 0;
}

 

参考资料:

http://www.tntnet.org/howto/jsonrpc-howto.html

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号