#include #include #include #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 stars; HPROSE_REFLECT(PushData, result, uid, stars) }; struct HandlerObject { std::string Hello(std::string& str) { return std::string("Hello ").append(str).append("!"); } std::tuple Test(const std::tuple& 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 Swap(int& a, int& b) { a += 100; b += 123; return std::make_tuple(a, b, HproseError()); } std::map Maptest(const std::unordered_map& m) { std::map 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 connector(&worker, 12345); HproseServer srv; auto obj = std::make_shared(); 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 mp{{1, 100}, {2, 400}, {3, 900}}; // client.MultiCall("maptest").PushArgs(mp).Expect([](const std::map& 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; }