117 lines
3.9 KiB
C++
117 lines
3.9 KiB
C++
|
#include <iostream>
|
||
|
#include <map>
|
||
|
|
||
|
#include <thread>
|
||
|
#include "timer.hpp"
|
||
|
#include "tcp.hpp"
|
||
|
|
||
|
#include "hprose_types.hpp"
|
||
|
#include "hprose_srv.hpp"
|
||
|
#include "hprose_cli.hpp"
|
||
|
#include "hprose_conn.hpp"
|
||
|
#include "worker.hpp"
|
||
|
|
||
|
struct PushData {
|
||
|
int result;
|
||
|
int uid;
|
||
|
std::vector<int> stars;
|
||
|
HPROSE_REFLECT(PushData, result, uid, stars)
|
||
|
};
|
||
|
|
||
|
struct HandlerObject {
|
||
|
std::string Hello(std::string& str) {
|
||
|
return std::string("Hello ").append(str).append("!");
|
||
|
}
|
||
|
|
||
|
std::tuple<int, int, HproseError> Test(const std::tuple<int, int>& a, int b) {
|
||
|
std::cout << "Test called." << std::endl;
|
||
|
return std::make_tuple(std::get<0>(a) * b, std::get<1>(a) * b, HproseError("Error!!!"));
|
||
|
}
|
||
|
|
||
|
std::tuple<int, int, HproseError> Swap(int& a, int& b) {
|
||
|
a += 100;
|
||
|
b += 123;
|
||
|
return std::make_tuple(a, b, HproseError());
|
||
|
}
|
||
|
|
||
|
std::map<int, int> Maptest(const std::unordered_map<int, int>& m) {
|
||
|
std::map<int, int> ret;
|
||
|
for(auto& iter : m) {
|
||
|
ret[iter.first] = iter.second + 100;
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
PushData Pushs(PushData& data) {
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
int main() {
|
||
|
std::thread t1([]() {
|
||
|
TimerdWorker worker;
|
||
|
HproseServerConnectorTCP<HandlerObject> connector(&worker, 12345);
|
||
|
HproseServer<HandlerObject> srv;
|
||
|
auto obj = std::make_shared<HandlerObject>();
|
||
|
srv.SetObjectAllocator([&obj]() -> auto {
|
||
|
return obj;
|
||
|
});
|
||
|
srv.UseConnector(connector);
|
||
|
srv.Register("hello", &HandlerObject::Hello);
|
||
|
srv.Register("test", &HandlerObject::Test);
|
||
|
srv.Register("swap", &HandlerObject::Swap);
|
||
|
srv.Register("maptest", &HandlerObject::Maptest);
|
||
|
srv.Register("Pushs", &HandlerObject::Pushs);
|
||
|
asio::signal_set signals(worker, SIGINT, SIGTERM);
|
||
|
signals.async_wait([&worker](const asio::error_code& error, int signal_number) {
|
||
|
std::cout << "Interrupted." << std::endl;
|
||
|
worker.stop();
|
||
|
});
|
||
|
worker.run();
|
||
|
});
|
||
|
// std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||
|
// std::thread t2([]() {
|
||
|
// TimerdWorker worker;
|
||
|
// BufferMgr bufmgr;
|
||
|
// HproseClientConnectorTCP connector(&worker, &bufmgr, "127.0.0.1", 12345);
|
||
|
// HproseClient client;
|
||
|
// client.UseConnector(connector);
|
||
|
// client.MultiCall("hello").PushArgs("world").Expect([](std::string& ret) {
|
||
|
// std::cout << ret << std::endl;
|
||
|
// }).Catch([](const std::string& msg) {
|
||
|
// std::cout << msg << std::endl;
|
||
|
// });
|
||
|
// client.MultiCall("test").PushArgs(std::make_tuple(123, 678), 10).Expect([](int a, int b, HproseError err) {
|
||
|
// std::cout << a << " " << b << " " << err.IsNull() << std::endl;
|
||
|
// }).Catch([](const std::string& msg) {
|
||
|
// std::cout << msg << std::endl;
|
||
|
// });
|
||
|
// int a = 100;
|
||
|
// int b = 200;
|
||
|
// client.MultiCall("swap").PushRefArgs(a, b).Expect([&a, &b]() {
|
||
|
// std::cout << a << b << std::endl;
|
||
|
// }).Catch([](const std::string& msg) {
|
||
|
// std::cout << msg << std::endl;
|
||
|
// });
|
||
|
// std::map<int, int> mp{{1, 100}, {2, 400}, {3, 900}};
|
||
|
// client.MultiCall("maptest").PushArgs(mp).Expect([](const std::map<int, int>& ret) {
|
||
|
// for(auto& iter : ret) {
|
||
|
// std::cout << "===" << iter.first << iter.second << std::endl;
|
||
|
// }
|
||
|
// }).Catch([](const std::string& msg) {
|
||
|
// std::cout << msg << std::endl;
|
||
|
// });
|
||
|
// client.MultiApply();
|
||
|
// client.Call("sttest").PushArgs(testst{1, "hello", 3.0, innst{1, 2}}).Expect([](testst ret) {
|
||
|
// std::cout << ret.a << ret.b << ret.c << std::endl;
|
||
|
// }).Catch([](const std::string& msg) {
|
||
|
// std::cout << msg << std::endl;
|
||
|
// });
|
||
|
// worker.run();
|
||
|
// });
|
||
|
t1.join();
|
||
|
// t2.join();
|
||
|
return 0;
|
||
|
}
|