From 17df5bba836610dae9da832aba9ddf6804182ef6 Mon Sep 17 00:00:00 2001 From: jarodruan Date: Wed, 29 Jan 2020 15:52:39 +0800 Subject: [PATCH] add examples CMakeLists.txt --- CMakeLists.txt | 10 +- cmake/BuildTarsServer.cmake | 57 +++++ examples/CoroutineDemo/AServer/CMakeLists.txt | 1 + examples/CoroutineDemo/BServer/CMakeLists.txt | 1 + examples/CoroutineDemo/CMakeLists.txt | 7 + examples/CoroutineDemo/client/CMakeLists.txt | 1 + examples/CoroutineDemo/client/main.cpp | 2 +- .../CoroutineDemo/testCoro/CMakeLists.txt | 1 + .../testParallelCoro/CMakeLists.txt | 1 + .../CoroutineDemo/testParallelCoro/main.cpp | 198 +++++++++--------- examples/HttpDemo/CMakeLists.txt | 4 + examples/HttpDemo/HttpClient/CMakeLists.txt | 1 + examples/HttpDemo/HttpClient/main.cpp | 2 +- examples/HttpDemo/HttpServer/CMakeLists.txt | 1 + examples/HttpDemo/HttpServer/HttpServer.cpp | 72 ++++--- examples/PromiseDemo/AServer/AServantImp.cpp | 61 ++++-- examples/PromiseDemo/AServer/AServantImp.h | 10 +- examples/PromiseDemo/AServer/CMakeLists.txt | 1 + examples/PromiseDemo/BServer/CMakeLists.txt | 1 + examples/PromiseDemo/CMakeLists.txt | 6 + examples/PromiseDemo/CServer/CMakeLists.txt | 1 + examples/PromiseDemo/Client/CMakeLists.txt | 1 + examples/PushDemo/CMakeLists.txt | 4 + examples/PushDemo/PushClient/CMakeLists.txt | 1 + .../PushDemo/PushClient/TestRecvThread.cpp | 126 ++++++----- examples/PushDemo/PushServer/CMakeLists.txt | 1 + .../PushDemo/PushServer/TestPushServer.cpp | 33 +-- examples/QuickStartDemo/CMakeLists.txt | 5 + .../HelloServer/AsyncClient/CMakeLists.txt | 1 + .../QuickStartDemo/HelloServer/CMakeLists.txt | 5 + .../HelloServer/Client/CMakeLists.txt | 1 + .../QuickStartDemo/HelloServer/Hello.tars | 26 --- .../QuickStartDemo/HelloServer/HelloImp.cpp | 42 ---- .../QuickStartDemo/HelloServer/HelloImp.h | 53 ----- .../HelloServer/HelloServer.cpp | 59 ------ .../QuickStartDemo/HelloServer/HelloServer.h | 50 ----- .../HelloServer/Server/CMakeLists.txt | 1 + examples/QuickStartDemo/HelloServer/makefile | 16 -- .../QuickStartDemo/ProxyServer/CMakeLists.txt | 5 + .../ProxyServer/Client/CMakeLists.txt | 1 + .../QuickStartDemo/ProxyServer/Proxy.tars | 26 --- .../QuickStartDemo/ProxyServer/ProxyImp.cpp | 83 -------- .../QuickStartDemo/ProxyServer/ProxyImp.h | 63 ------ .../ProxyServer/ProxyServer.cpp | 57 ----- .../QuickStartDemo/ProxyServer/ProxyServer.h | 52 ----- .../ProxyServer/Server/CMakeLists.txt | 3 + examples/QuickStartDemo/ProxyServer/makefile | 17 -- examples/StressDemo/CMakeLists.txt | 5 + .../TarsStressClient/CMakeLists.txt | 1 + .../TarsStressServer/CMakeLists.txt | 1 + servant/libservant/Application.cpp | 4 +- servant/libservant/Servant.cpp | 10 + servant/libservant/ServantHelper.cpp | 4 +- servant/promise/promise.h | 30 ++- servant/promise/when_all.h | 87 ++++---- servant/servant/Servant.h | 18 ++ servant/servant/ServantHelper.h | 33 +-- util/src/CMakeLists.txt | 2 - 58 files changed, 501 insertions(+), 865 deletions(-) create mode 100644 cmake/BuildTarsServer.cmake create mode 100644 examples/CoroutineDemo/AServer/CMakeLists.txt create mode 100644 examples/CoroutineDemo/BServer/CMakeLists.txt create mode 100644 examples/CoroutineDemo/CMakeLists.txt create mode 100644 examples/CoroutineDemo/client/CMakeLists.txt create mode 100644 examples/CoroutineDemo/testCoro/CMakeLists.txt create mode 100644 examples/CoroutineDemo/testParallelCoro/CMakeLists.txt create mode 100644 examples/HttpDemo/CMakeLists.txt create mode 100644 examples/HttpDemo/HttpClient/CMakeLists.txt create mode 100644 examples/HttpDemo/HttpServer/CMakeLists.txt create mode 100644 examples/PromiseDemo/AServer/CMakeLists.txt create mode 100644 examples/PromiseDemo/BServer/CMakeLists.txt create mode 100644 examples/PromiseDemo/CMakeLists.txt create mode 100644 examples/PromiseDemo/CServer/CMakeLists.txt create mode 100644 examples/PromiseDemo/Client/CMakeLists.txt create mode 100644 examples/PushDemo/CMakeLists.txt create mode 100644 examples/PushDemo/PushClient/CMakeLists.txt create mode 100644 examples/PushDemo/PushServer/CMakeLists.txt create mode 100644 examples/QuickStartDemo/CMakeLists.txt create mode 100644 examples/QuickStartDemo/HelloServer/AsyncClient/CMakeLists.txt create mode 100644 examples/QuickStartDemo/HelloServer/CMakeLists.txt create mode 100644 examples/QuickStartDemo/HelloServer/Client/CMakeLists.txt delete mode 100644 examples/QuickStartDemo/HelloServer/Hello.tars delete mode 100644 examples/QuickStartDemo/HelloServer/HelloImp.cpp delete mode 100644 examples/QuickStartDemo/HelloServer/HelloImp.h delete mode 100644 examples/QuickStartDemo/HelloServer/HelloServer.cpp delete mode 100644 examples/QuickStartDemo/HelloServer/HelloServer.h create mode 100644 examples/QuickStartDemo/HelloServer/Server/CMakeLists.txt delete mode 100644 examples/QuickStartDemo/HelloServer/makefile create mode 100644 examples/QuickStartDemo/ProxyServer/CMakeLists.txt create mode 100644 examples/QuickStartDemo/ProxyServer/Client/CMakeLists.txt delete mode 100644 examples/QuickStartDemo/ProxyServer/Proxy.tars delete mode 100644 examples/QuickStartDemo/ProxyServer/ProxyImp.cpp delete mode 100644 examples/QuickStartDemo/ProxyServer/ProxyImp.h delete mode 100644 examples/QuickStartDemo/ProxyServer/ProxyServer.cpp delete mode 100644 examples/QuickStartDemo/ProxyServer/ProxyServer.h create mode 100644 examples/QuickStartDemo/ProxyServer/Server/CMakeLists.txt delete mode 100644 examples/QuickStartDemo/ProxyServer/makefile create mode 100644 examples/StressDemo/CMakeLists.txt create mode 100644 examples/StressDemo/TarsStressClient/CMakeLists.txt create mode 100644 examples/StressDemo/TarsStressServer/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index c3f610d..0aebff0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -114,11 +114,15 @@ IF(WIN32) include_directories(${CMAKE_SOURCE_DIR}/util/src/epoll_windows) ENDIF() +include("${CMAKE_SOURCE_DIR}/cmake/BuildTarsServer.cmake") + +include_directories(${CMAKE_SOURCE_DIR}/util/include) +include_directories(${CMAKE_SOURCE_DIR}/servant) +include_directories(${CMAKE_SOURCE_DIR}/servant/protocol) + add_subdirectory(util) add_subdirectory(tools) - -# set(TARS2CPP "${tools_BINARY_DIR}/tars2cpp/tars2cpp") - add_subdirectory(servant) +add_subdirectory(examples) #add_subdirectory(test_deprecated) \ No newline at end of file diff --git a/cmake/BuildTarsServer.cmake b/cmake/BuildTarsServer.cmake new file mode 100644 index 0000000..b9b71c5 --- /dev/null +++ b/cmake/BuildTarsServer.cmake @@ -0,0 +1,57 @@ + +macro(build_tars_server MODULE) + + include_directories(./) + + aux_source_directory(. DIR_SRCS) + + # message("MODULE: ${MODULE}, DIR_SRCS:${DIR_SRCS}") + + FILE(GLOB TARS_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.tars") + + # message("TARS_LIST:${TARS_LIST}") + + set(TARS_LIST_DEPENDS) + + if (TARS_LIST) + set(CLEAN_LIST) + + foreach (TARS_SRC ${TARS_LIST}) + get_filename_component(NAME_WE ${TARS_SRC} NAME_WE) + + set(TARS_H ${NAME_WE}.h) + + set(CUR_TARS_GEN ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) + LIST(APPEND TARS_LIST_DEPENDS ${CUR_TARS_GEN}) + + # message("TARS_H:${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}") + + add_custom_command(OUTPUT ${CUR_TARS_GEN} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS ${TARS2CPP} + COMMAND ${TARS2CPP} ${TARS_SRC} + COMMENT "${TARS2CPP} ${TARS_SRC}") + + list(APPEND CLEAN_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}) + + endforeach () + + set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}") + + set(TARS_TARGET "TARS_${MODULE}") + add_custom_target(${TARS_TARGET} ALL DEPENDS ${TARS_LIST_DEPENDS}) + + add_executable(${MODULE} ${DIR_SRCS}) + + add_dependencies(${MODULE} ${TARS_TARGET}) + + else(TARS_LIST) + add_executable(${MODULE} ${DIR_SRCS}) + + endif (TARS_LIST) + + add_dependencies(${MODULE} tarsutil tarsservant) + + target_link_libraries(${MODULE} tarsutil tarsservant) + +endmacro() \ No newline at end of file diff --git a/examples/CoroutineDemo/AServer/CMakeLists.txt b/examples/CoroutineDemo/AServer/CMakeLists.txt new file mode 100644 index 0000000..7a3eece --- /dev/null +++ b/examples/CoroutineDemo/AServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("AServer") \ No newline at end of file diff --git a/examples/CoroutineDemo/BServer/CMakeLists.txt b/examples/CoroutineDemo/BServer/CMakeLists.txt new file mode 100644 index 0000000..f9d2cdf --- /dev/null +++ b/examples/CoroutineDemo/BServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("BServer") \ No newline at end of file diff --git a/examples/CoroutineDemo/CMakeLists.txt b/examples/CoroutineDemo/CMakeLists.txt new file mode 100644 index 0000000..ca2ee7b --- /dev/null +++ b/examples/CoroutineDemo/CMakeLists.txt @@ -0,0 +1,7 @@ + +add_subdirectory(AServer) +add_subdirectory(BServer) +add_subdirectory(client) +add_subdirectory(testCoro) +add_subdirectory(testParallelCoro) + diff --git a/examples/CoroutineDemo/client/CMakeLists.txt b/examples/CoroutineDemo/client/CMakeLists.txt new file mode 100644 index 0000000..113ea42 --- /dev/null +++ b/examples/CoroutineDemo/client/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("client") \ No newline at end of file diff --git a/examples/CoroutineDemo/client/main.cpp b/examples/CoroutineDemo/client/main.cpp index 9f7a92e..f0cc6f4 100644 --- a/examples/CoroutineDemo/client/main.cpp +++ b/examples/CoroutineDemo/client/main.cpp @@ -129,7 +129,7 @@ int main(int argc,char ** argv) cout << "********************" < BServantCoroCallbackPtr; -//自定义协程类 -class CoroutineClass : public TC_Thread -{ -public: - /** - * 构造函数 - */ - CoroutineClass(); +// //自定义协程类 +// class CoroutineClass : public TC_Thread +// { +// public: +// /** +// * 构造函数 +// */ +// CoroutineClass(); - /** - * 析构函数 - */ - virtual ~CoroutineClass(); +// /** +// * 析构函数 +// */ +// virtual ~CoroutineClass(); - /** - * 返回0,代表成功,-1,表示失败 - */ - int registerFunc(const vector< tars::TC_Callback > &vFunc); +// /** +// * 返回0,代表成功,-1,表示失败 +// */ +// int registerFunc(const vector< std::function > &vFunc); - /** - * 线程初始化 - */ - virtual void initialize() {} +// /** +// * 线程初始化 +// */ +// virtual void initialize() {} - /** - * 线程处理方法 - */ - virtual void run(); +// /** +// * 线程处理方法 +// */ +// virtual void run(); - /** - * 停止线程 - */ - void terminate(); +// /** +// * 停止线程 +// */ +// void terminate(); -protected: - /** - * 线程已经启动, 进入具体协程处理前调用 - */ - virtual void startCoro() {} +// protected: +// /** +// * 线程已经启动, 进入具体协程处理前调用 +// */ +// virtual void startCoro() {} - /** - * 线程马上要退出时调用 - */ - virtual void stopCoro() {} +// /** +// * 线程马上要退出时调用 +// */ +// virtual void stopCoro() {} - /** - * 具体的处理逻辑 - */ - virtual void handleCoro(); +// /** +// * 具体的处理逻辑 +// */ +// virtual void handleCoro(); -protected: - CoroutineScheduler *_coroSched; - uint32_t _iPoolSize; - size_t _iStackSize; - vector< tars::TC_Callback > _vFunc; -}; +// protected: +// CoroutineScheduler *_coroSched; +// uint32_t _iPoolSize; +// size_t _iStackSize; +// vector > _vFunc; +// }; -CoroutineClass::CoroutineClass() -: _coroSched(NULL) -, _iPoolSize(1024) -, _iStackSize(128*1024) -{ -} +// CoroutineClass::CoroutineClass() +// : _coroSched(NULL) +// , _iPoolSize(1024) +// , _iStackSize(128*1024) +// { +// } -CoroutineClass::~CoroutineClass() -{ - if(isAlive()) - { - terminate(); +// CoroutineClass::~CoroutineClass() +// { +// if(isAlive()) +// { +// terminate(); - getThreadControl().join(); - } -} +// getThreadControl().join(); +// } +// } -int CoroutineClass::registerFunc(const vector< tars::TC_Callback > &vFunc) -{ - if(vFunc.size() > _iPoolSize || vFunc.size() <= 0) - { - return -1; - } +// int CoroutineClass::registerFunc(const vector< std::function > &vFunc) +// { +// if(vFunc.size() > _iPoolSize || vFunc.size() <= 0) +// { +// return -1; +// } - _vFunc = vFunc; +// _vFunc = vFunc; - return 0; -} +// return 0; +// } -void CoroutineClass::run() -{ - initialize(); +// void CoroutineClass::run() +// { +// initialize(); - startCoro(); +// startCoro(); - handleCoro(); +// handleCoro(); - stopCoro(); -} +// stopCoro(); +// } -void CoroutineClass::terminate() -{ - if(_coroSched) - { - _coroSched->terminate(); - } -} +// void CoroutineClass::terminate() +// { +// if(_coroSched) +// { +// _coroSched->terminate(); +// } +// } -void CoroutineClass::handleCoro() -{ - _coroSched = new CoroutineScheduler(); +// void CoroutineClass::handleCoro() +// { +// _coroSched = new CoroutineScheduler(); - _coroSched->init(_iPoolSize, _iStackSize); +// _coroSched->init(_iPoolSize, _iStackSize); - ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); +// ServantProxyThreadData * pSptd = ServantProxyThreadData::getData(); - assert(pSptd != NULL); +// assert(pSptd != NULL); - pSptd->_sched = _coroSched; +// pSptd->_sched = _coroSched; - for(size_t i = 0; i < _vFunc.size(); ++i) - { - _coroSched->createCoroutine(_vFunc[i]); - } +// for(size_t i = 0; i < _vFunc.size(); ++i) +// { +// _coroSched->createCoroutine(_vFunc[i]); +// } - _coroSched->run(); +// _coroSched->run(); - delete _coroSched; - _coroSched = NULL; -} +// delete _coroSched; +// _coroSched = NULL; +// } //////////////////////////////////////////// //继承框架的协程类 diff --git a/examples/HttpDemo/CMakeLists.txt b/examples/HttpDemo/CMakeLists.txt new file mode 100644 index 0000000..12ae6f6 --- /dev/null +++ b/examples/HttpDemo/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_subdirectory(HttpClient) +add_subdirectory(HttpServer) + diff --git a/examples/HttpDemo/HttpClient/CMakeLists.txt b/examples/HttpDemo/HttpClient/CMakeLists.txt new file mode 100644 index 0000000..2aae31b --- /dev/null +++ b/examples/HttpDemo/HttpClient/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("HttpClient") \ No newline at end of file diff --git a/examples/HttpDemo/HttpClient/main.cpp b/examples/HttpDemo/HttpClient/main.cpp index 764e482..578ca96 100644 --- a/examples/HttpDemo/HttpClient/main.cpp +++ b/examples/HttpDemo/HttpClient/main.cpp @@ -161,7 +161,7 @@ int main(int argc,char ** argv) tp.exec(fwrapper3); cout << "********************" < &out) +// { +// try +// { +// bool b = in.checkRequest(in.length()); +// if(b) +// { +// out = in; +// in.clearBuffers(); +// //TLOGDEBUG("out size: " << out.size() << endl); +// return TC_EpollServer::PACKET_FULL; +// } +// else +// { +// return TC_EpollServer::PACKET_LESS; +// } +// } +// catch(exception &ex) +// { +// return TC_EpollServer::PACKET_ERR; +// } - return TC_EpollServer::PACKET_LESS; //ʾյİȫ - } +// return TC_EpollServer::PACKET_LESS; //��ʾ�յ��İ�����ȫ +// } -}; +// }; void HttpServer::initialize() @@ -67,7 +65,7 @@ HttpServer::initialize() //... addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj"); - addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&HttpProtocol::parseHttp); + addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&TC_NetWorkBuffer::parseHttp); } ///////////////////////////////////////////////////////////////// void diff --git a/examples/PromiseDemo/AServer/AServantImp.cpp b/examples/PromiseDemo/AServer/AServantImp.cpp index bcf4ca6..adeab46 100644 --- a/examples/PromiseDemo/AServer/AServantImp.cpp +++ b/examples/PromiseDemo/AServer/AServantImp.cpp @@ -31,7 +31,7 @@ public: : _current(current) {} - BServantCallback(TarsCurrentPtr ¤t, const promise::Promise &promise) + BServantCallback(TarsCurrentPtr ¤t, const tars::Promise &promise) : _current(current) , _promise(promise) {} @@ -60,7 +60,7 @@ private: s += "|ret:"; s += TC_Common::tostr(ret); - _promise.setException(promise::copyException(s)); + _promise.setException(tars::copyException(s)); TLOGDEBUG("ServerPrxCallback handExp:" << s << endl); } @@ -68,7 +68,7 @@ private: private: TarsCurrentPtr _current; - promise::Promise _promise; + tars::Promise _promise; }; ////////////////////////////////////////////////////// class CServantCallback : public CServantPrxCallback @@ -79,7 +79,7 @@ public: : _current(current) {} - CServantCallback(TarsCurrentPtr ¤t, const promise::Promise &promise) + CServantCallback(TarsCurrentPtr ¤t, const tars::Promise &promise) : _current(current) , _promise(promise) {} @@ -108,7 +108,7 @@ private: s += "|ret:"; s += TC_Common::tostr(ret); - _promise.setException(promise::copyException(s)); + _promise.setException(tars::copyException(s)); TLOGDEBUG("ServerPrxCallback handExp:" << s << endl); } @@ -116,12 +116,12 @@ private: private: TarsCurrentPtr _current; - promise::Promise _promise; + tars::Promise _promise; }; ////////////////////////////////////////////////////// -promise::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) +tars::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) { - promise::Promise promise; + tars::Promise promise; Test::BServantPrxCallbackPtr cb = new BServantCallback(current, promise); @@ -130,9 +130,9 @@ promise::Future sendBReq(BServantPrx prx, const std::string& sIn, t return promise.getFuture(); } ////////////////////////////////////////////////////// -promise::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) +tars::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) { - promise::Promise promise; + tars::Promise promise; Test::CServantPrxCallbackPtr cb = new CServantCallback(current, promise); @@ -141,7 +141,7 @@ promise::Future sendCReq(CServantPrx prx, const std::string& sIn, t return promise.getFuture(); } ////////////////////////////////////////////////////// -promise::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future& future) +tars::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future& future) { std::string sResult(""); std::string sException(""); @@ -157,13 +157,13 @@ promise::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentP sException = e.what(); } - promise::Promise promise; + tars::Promise promise; promise.setValue(sException); return promise.getFuture(); } ////////////////////////////////////////////////////// -int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future& future) +int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future& future) { int ret = 0; std::string sResult(""); @@ -184,16 +184,16 @@ int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future, promise::Future > >& allFuture) +int handleBCRspAndReturnClient(TarsCurrentPtr current, const tars::Future, tars::Future > >& allFuture) { int ret = 0; std::string sResult(""); try { - promise::Tuple, promise::Future > tupleFuture = allFuture.get(); + std::tuple, tars::Future > tupleFuture = allFuture.get(); - std::string sResult1 = tupleFuture.get<0>().get(); - std::string sResult2 = tupleFuture.get<1>().get(); + std::string sResult1 = std::get<0>(tupleFuture).get(); + std::string sResult2 = std::get<1>(tupleFuture).get(); sResult = sResult1; sResult += "|"; @@ -223,15 +223,30 @@ void AServantImp::initialize() void AServantImp::destroy() { } + +// class Test1 +// { +// public: +// template +// void then(const std::function& callback); +// { +// cout <<"then" << endl; +// } +// } + ////////////////////////////////////////////////////// tars::Int32 AServantImp::queryResultSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) { current->setResponse(false); - promise::Future f = sendBReq(_pPrxB, sIn, current); + tars::Future f = sendBReq(_pPrxB, sIn, current); - f.then(tars::TC_Bind(&handleBRspAndSendCReq, _pPrxC, current)).then(tars::TC_Bind(&handleCRspAndReturnClient, current)); + auto b1 = std::bind(handleBRspAndSendCReq, _pPrxC, current);//, std::placeholders::_3); + auto b2 = std::bind(handleCRspAndReturnClient, current);//, std::placeholders::_2); + f.then(b1).then(b2); + // Test1 t; + // t.then(b1); return 0; } ////////////////////////////////////////////////////// @@ -239,13 +254,13 @@ tars::Int32 AServantImp::queryResultParallel(const std::string& sIn, std::string { current->setResponse(false); - promise::Future f1 = sendBReq(_pPrxB, sIn, current); + tars::Future f1 = sendBReq(_pPrxB, sIn, current); - promise::Future f2 = sendCReq(_pPrxC, sIn, current); + tars::Future f2 = sendCReq(_pPrxC, sIn, current); - promise::Future, promise::Future > > f_all = promise::whenAll(f1, f2); + tars::Future, tars::Future > > f_all = tars::whenAll(f1, f2); - f_all.then(tars::TC_Bind(&handleBCRspAndReturnClient, current)); + f_all.then(std::bind(&handleBCRspAndReturnClient, current)); return 0; } diff --git a/examples/PromiseDemo/AServer/AServantImp.h b/examples/PromiseDemo/AServer/AServantImp.h index 0d2c191..c73bf0b 100644 --- a/examples/PromiseDemo/AServer/AServantImp.h +++ b/examples/PromiseDemo/AServer/AServantImp.h @@ -29,16 +29,16 @@ using namespace Test; /////////////////////////////////// -promise::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); +tars::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); -promise::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future& future); +tars::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future& future); -promise::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); +tars::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); -int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future& future); +int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future& future); /////////////////////////////////// -int handleBCRspAndReturnClient(TarsCurrentPtr current, const promise::Future, promise::Future > >& allFuture); +int handleBCRspAndReturnClient(TarsCurrentPtr current, const tars::Future, tars::Future > >& allFuture); /////////////////////////////////// class AServantImp : public Test::AServant diff --git a/examples/PromiseDemo/AServer/CMakeLists.txt b/examples/PromiseDemo/AServer/CMakeLists.txt new file mode 100644 index 0000000..431ac99 --- /dev/null +++ b/examples/PromiseDemo/AServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("PromiseDemoAServer") \ No newline at end of file diff --git a/examples/PromiseDemo/BServer/CMakeLists.txt b/examples/PromiseDemo/BServer/CMakeLists.txt new file mode 100644 index 0000000..c1f2b64 --- /dev/null +++ b/examples/PromiseDemo/BServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("PromiseDemoBServer") \ No newline at end of file diff --git a/examples/PromiseDemo/CMakeLists.txt b/examples/PromiseDemo/CMakeLists.txt new file mode 100644 index 0000000..f506e86 --- /dev/null +++ b/examples/PromiseDemo/CMakeLists.txt @@ -0,0 +1,6 @@ + +add_subdirectory(AServer) +add_subdirectory(BServer) +add_subdirectory(CServer) +add_subdirectory(Client) + diff --git a/examples/PromiseDemo/CServer/CMakeLists.txt b/examples/PromiseDemo/CServer/CMakeLists.txt new file mode 100644 index 0000000..73f1745 --- /dev/null +++ b/examples/PromiseDemo/CServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("PromiseDemoCServer") \ No newline at end of file diff --git a/examples/PromiseDemo/Client/CMakeLists.txt b/examples/PromiseDemo/Client/CMakeLists.txt new file mode 100644 index 0000000..d3caea6 --- /dev/null +++ b/examples/PromiseDemo/Client/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("PromiseDemoClient") \ No newline at end of file diff --git a/examples/PushDemo/CMakeLists.txt b/examples/PushDemo/CMakeLists.txt new file mode 100644 index 0000000..d1f7d96 --- /dev/null +++ b/examples/PushDemo/CMakeLists.txt @@ -0,0 +1,4 @@ + +add_subdirectory(PushClient) +add_subdirectory(PushServer) + diff --git a/examples/PushDemo/PushClient/CMakeLists.txt b/examples/PushDemo/PushClient/CMakeLists.txt new file mode 100644 index 0000000..ab9804c --- /dev/null +++ b/examples/PushDemo/PushClient/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("PushClient") \ No newline at end of file diff --git a/examples/PushDemo/PushClient/TestRecvThread.cpp b/examples/PushDemo/PushClient/TestRecvThread.cpp index 3e57346..dd5fe2d 100644 --- a/examples/PushDemo/PushClient/TestRecvThread.cpp +++ b/examples/PushDemo/PushClient/TestRecvThread.cpp @@ -5,71 +5,93 @@ /* 响应包解码函数,根据特定格式解码从服务端收到的数据,解析为ResponsePacket */ -static size_t pushResponse(const char* recvBuffer, size_t length, list& done) +static TC_NetWorkBuffer::PACKET_TYPE pushResponse(TC_NetWorkBuffer &in, ResponsePacket& done) { - size_t pos = 0; - while (pos < length) - { - unsigned int len = length - pos; - if(len < sizeof(unsigned int)) - { - break; - } + size_t len = sizeof(tars::Int32); - unsigned int iHeaderLen = ntohl(*(unsigned int*)(recvBuffer + pos)); + if (in.getBufferLength() < len) + { + return TC_NetWorkBuffer::PACKET_LESS; + } - //做一下保护,长度大于M - if (iHeaderLen > 100000 || iHeaderLen < sizeof(unsigned int)) - { - throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen)); - } + string header; + in.getHeader(len, header); - //包没有接收全 - if (len < iHeaderLen) - { - break; - } - else - { - ResponsePacket rsp; - rsp.iRequestId = ntohl(*((unsigned int *)(recvBuffer + pos + sizeof(unsigned int)))); - rsp.sBuffer.resize(iHeaderLen - 2*sizeof(unsigned int)); - ::memcpy(&rsp.sBuffer[0], recvBuffer + pos + 2*sizeof(unsigned int), iHeaderLen - 2*sizeof(unsigned int)); + assert(header.size() == len); - pos += iHeaderLen; + tars::Int32 iHeaderLen = 0; - done.push_back(rsp); - } - } + ::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32)); - return pos; + iHeaderLen = ntohl(iHeaderLen); + + //做一下保护,长度大于M + if (iHeaderLen > 100000 || iHeaderLen < sizeof(unsigned int)) + { + throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen)); + } + + //包没有接收全 + if (in.getBufferLength() < (uint32_t)iHeaderLen) + { + return TC_NetWorkBuffer::PACKET_LESS; + } + + tars::Int32 iRequestId = 0; + string sRequestId; + in.getHeader(sizeof(iHeaderLen), sRequestId); + + ResponsePacket rsp; + rsp.iRequestId = ntohl(*((unsigned int *)(sRequestId.c_str()))); + in.getHeader(iHeaderLen - sizeof(iHeaderLen) - sizeof(iRequestId), rsp.sBuffer); + + return TC_NetWorkBuffer::PACKET_FULL; } /* 请求包编码函数,本函数的打包格式为 整个包长度(字节)+iRequestId(字节)+包内容 */ -static void pushRequest(const RequestPacket& request, string& buff) +static void pushRequest(const RequestPacket& request, shared_ptr& buff) { - unsigned int net_bufflength = htonl(request.sBuffer.size()+8); - unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength); + // unsigned int net_bufflength = htonl(request.sBuffer.size()+8); + // unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength); - buff = ""; - for (int i = 0; i<4; ++i) - { - buff += *bufflengthptr++; - } + // buff = ""; + // for (int i = 0; i<4; ++i) + // { + // buff += *bufflengthptr++; + // } - unsigned int netrequestId = htonl(request.iRequestId); - unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId); + // unsigned int netrequestId = htonl(request.iRequestId); + // unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId); - for (int i = 0; i<4; ++i) - { - buff += *netrequestIdptr++; - } + // for (int i = 0; i<4; ++i) + // { + // buff += *netrequestIdptr++; + // } - string tmp; - tmp.assign((const char*)(&request.sBuffer[0]), request.sBuffer.size()); - buff+=tmp; + // string tmp; + // tmp.assign((const char*)(&request.sBuffer[0]), request.sBuffer.size()); + // buff+=tmp; + + TarsOutputStream os; + + tars::Int32 iHeaderLen = 0; + tars::Int32 iRequestId = request.iRequestId; + +// 先预留8个字节长度 + os.writeBuf((const char *)&iHeaderLen, sizeof(iHeaderLen)); + os.writeBuf((const char *)&iRequestId, sizeof(iRequestId)); + + request.writeTo(os); + + buff->swap(os.getByteBuffer()); + + // assert(buff->length() >= 4); + + iHeaderLen = htonl((int)(buff->length())); + + memcpy((void*)buff->buffer(), (const char *)&iHeaderLen, sizeof(iHeaderLen)); } static void printResult(int iRequestId, const string &sResponseStr) @@ -87,15 +109,15 @@ int TestPushCallBack::onDispatch(ReqMessagePtr msg) if(msg->request.sFuncName == "printResult") { string sRet; - cout << "sBuffer: " << msg->response.sBuffer.size() << endl; - sRet.assign(&(msg->response.sBuffer[0]), msg->response.sBuffer.size()); + cout << "sBuffer: " << msg->response->sBuffer.size() << endl; + sRet.assign(&(msg->response->sBuffer[0]), msg->response->sBuffer.size()); printResult(msg->request.iRequestId, sRet); return 0; } - else if(msg->response.iRequestId == 0) + else if(msg->response->iRequestId == 0) { string sRet; - sRet.assign(&(msg->response.sBuffer[0]), msg->response.sBuffer.size()); + sRet.assign(&(msg->response->sBuffer[0]), msg->response->sBuffer.size()); printPushInfo(sRet); return 0; } diff --git a/examples/PushDemo/PushServer/CMakeLists.txt b/examples/PushDemo/PushServer/CMakeLists.txt new file mode 100644 index 0000000..5607f98 --- /dev/null +++ b/examples/PushDemo/PushServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("PushServer") \ No newline at end of file diff --git a/examples/PushDemo/PushServer/TestPushServer.cpp b/examples/PushDemo/PushServer/TestPushServer.cpp index d8a3dd9..5024578 100644 --- a/examples/PushDemo/PushServer/TestPushServer.cpp +++ b/examples/PushDemo/PushServer/TestPushServer.cpp @@ -7,37 +7,6 @@ TestPushServer g_app; ///////////////////////////////////////////////////////////////// -static int parse(string &in, string &out) -{ - if(in.length() < sizeof(unsigned int)) - { - return TC_EpollServer::PACKET_LESS; - } - - unsigned int iHeaderLen; - - memcpy(&iHeaderLen, in.c_str(), sizeof(unsigned int)); - - iHeaderLen = ntohl(iHeaderLen); - - if(iHeaderLen < (unsigned int)(sizeof(unsigned int))|| iHeaderLen > 1000000) - { - return TC_EpollServer::PACKET_ERR; - } - - if((unsigned int)in.length() < iHeaderLen) - { - return TC_EpollServer::PACKET_LESS; - } - - out = in.substr(0, iHeaderLen); - - in = in.substr(iHeaderLen); - - return TC_EpollServer::PACKET_FULL; -} - - void TestPushServer::initialize() { @@ -46,7 +15,7 @@ TestPushServer::initialize() addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".TestPushServantObj"); - addServantProtocol("Test.TestPushServer.TestPushServantObj", parse); + addServantProtocol("Test.TestPushServer.TestPushServantObj", TC_NetWorkBuffer::parseBinary4<8, 1024*1024*10>); pushThread.start(); diff --git a/examples/QuickStartDemo/CMakeLists.txt b/examples/QuickStartDemo/CMakeLists.txt new file mode 100644 index 0000000..097666f --- /dev/null +++ b/examples/QuickStartDemo/CMakeLists.txt @@ -0,0 +1,5 @@ + +include_directories(HelloServer/Server) +add_subdirectory(HelloServer) +add_subdirectory(ProxyServer) + diff --git a/examples/QuickStartDemo/HelloServer/AsyncClient/CMakeLists.txt b/examples/QuickStartDemo/HelloServer/AsyncClient/CMakeLists.txt new file mode 100644 index 0000000..229d7d5 --- /dev/null +++ b/examples/QuickStartDemo/HelloServer/AsyncClient/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("QuickStartDemoAsyncClient") \ No newline at end of file diff --git a/examples/QuickStartDemo/HelloServer/CMakeLists.txt b/examples/QuickStartDemo/HelloServer/CMakeLists.txt new file mode 100644 index 0000000..23e6fb4 --- /dev/null +++ b/examples/QuickStartDemo/HelloServer/CMakeLists.txt @@ -0,0 +1,5 @@ + +include_directories(Server) +add_subdirectory(Server) +add_subdirectory(Client) +add_subdirectory(AsyncClient) \ No newline at end of file diff --git a/examples/QuickStartDemo/HelloServer/Client/CMakeLists.txt b/examples/QuickStartDemo/HelloServer/Client/CMakeLists.txt new file mode 100644 index 0000000..13d9f3f --- /dev/null +++ b/examples/QuickStartDemo/HelloServer/Client/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("QuickStartDemoClient") \ No newline at end of file diff --git a/examples/QuickStartDemo/HelloServer/Hello.tars b/examples/QuickStartDemo/HelloServer/Hello.tars deleted file mode 100644 index 174c4f3..0000000 --- a/examples/QuickStartDemo/HelloServer/Hello.tars +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module TestApp -{ - -interface Hello -{ - int test(); - int testHello(string sReq, out string sRsp); -}; - -}; diff --git a/examples/QuickStartDemo/HelloServer/HelloImp.cpp b/examples/QuickStartDemo/HelloServer/HelloImp.cpp deleted file mode 100644 index b0985dc..0000000 --- a/examples/QuickStartDemo/HelloServer/HelloImp.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "HelloImp.h" -#include "servant/Application.h" - -using namespace std; - -////////////////////////////////////////////////////// -void HelloImp::initialize() -{ - //initialize servant here: - //... -} - -////////////////////////////////////////////////////// -void HelloImp::destroy() -{ - //destroy servant here: - //... -} - -int HelloImp::testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current) -{ - TLOGDEBUG("HelloImp::testHellosReq:"<(ServerConfig::Application + "." + ServerConfig::ServerName + ".HelloObj"); -} -///////////////////////////////////////////////////////////////// -void -HelloServer::destroyApp() -{ - //destroy application here: - //... -} -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/examples/QuickStartDemo/HelloServer/HelloServer.h b/examples/QuickStartDemo/HelloServer/HelloServer.h deleted file mode 100644 index f35886b..0000000 --- a/examples/QuickStartDemo/HelloServer/HelloServer.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef _HelloServer_H_ -#define _HelloServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class HelloServer : public Application -{ -public: - /** - * - **/ - virtual ~HelloServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -}; - -extern HelloServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/examples/QuickStartDemo/HelloServer/Server/CMakeLists.txt b/examples/QuickStartDemo/HelloServer/Server/CMakeLists.txt new file mode 100644 index 0000000..eec8189 --- /dev/null +++ b/examples/QuickStartDemo/HelloServer/Server/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("QuickStartDemo") diff --git a/examples/QuickStartDemo/HelloServer/makefile b/examples/QuickStartDemo/HelloServer/makefile deleted file mode 100644 index 56961c4..0000000 --- a/examples/QuickStartDemo/HelloServer/makefile +++ /dev/null @@ -1,16 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := TestApp -TARGET := HelloServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- - -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/examples/QuickStartDemo/ProxyServer/CMakeLists.txt b/examples/QuickStartDemo/ProxyServer/CMakeLists.txt new file mode 100644 index 0000000..6345c28 --- /dev/null +++ b/examples/QuickStartDemo/ProxyServer/CMakeLists.txt @@ -0,0 +1,5 @@ + +include_directories(Server) +add_subdirectory(Server) +add_subdirectory(Client) + diff --git a/examples/QuickStartDemo/ProxyServer/Client/CMakeLists.txt b/examples/QuickStartDemo/ProxyServer/Client/CMakeLists.txt new file mode 100644 index 0000000..2c609ca --- /dev/null +++ b/examples/QuickStartDemo/ProxyServer/Client/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("ProxyServerClient") \ No newline at end of file diff --git a/examples/QuickStartDemo/ProxyServer/Proxy.tars b/examples/QuickStartDemo/ProxyServer/Proxy.tars deleted file mode 100644 index 156faeb..0000000 --- a/examples/QuickStartDemo/ProxyServer/Proxy.tars +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -module TestApp -{ - -interface Proxy -{ - int test(); - int testProxy(string sReq, out string sRsp); -}; - -}; diff --git a/examples/QuickStartDemo/ProxyServer/ProxyImp.cpp b/examples/QuickStartDemo/ProxyServer/ProxyImp.cpp deleted file mode 100644 index 5cc0fc4..0000000 --- a/examples/QuickStartDemo/ProxyServer/ProxyImp.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ProxyImp.h" -#include "ProxyServer.h" - -using namespace std; -using namespace tars; - -class HelloCallback : public HelloPrxCallback -{ - -public: - HelloCallback(TarsCurrentPtr ¤t) - : _current(current) - {} - - virtual void callback_testHello(tars::Int32 ret, const std::string& sOut) - { - Proxy::async_response_testProxy(_current, ret, sOut); - } - virtual void callback_testHello_exception(tars::Int32 ret) - { - TLOGERROR("HelloCallback callback_testHello_exception ret:" << ret << endl); - - Proxy::async_response_testProxy(_current, ret, ""); - } - - TarsCurrentPtr _current; -}; - - - -////////////////////////////////////////////////////// -void ProxyImp::initialize() -{ - //initialize servant here: - //... - - _prx = Application::getCommunicator()->stringToProxy("TestApp.HelloServer.HelloObj"); -} - -////////////////////////////////////////////////////// -void ProxyImp::destroy() -{ -} - -////////////////////////////////////////////////////// -tars::Int32 ProxyImp::test(tars::TarsCurrentPtr current) { return 0;} - -////////////////////////////////////////////////////// -tars::Int32 ProxyImp::testProxy(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - try - { - current->setResponse(false); - - TestApp::HelloPrxCallbackPtr cb = new HelloCallback(current); - - _prx->tars_set_timeout(3000)->async_testHello(cb,sIn); - } - catch(std::exception &ex) - { - current->setResponse(true); - - TLOGERROR("ProxyImp::testProxy ex:" << ex.what() << endl); - } - - return 0; -} diff --git a/examples/QuickStartDemo/ProxyServer/ProxyImp.h b/examples/QuickStartDemo/ProxyServer/ProxyImp.h deleted file mode 100644 index 9c924bd..0000000 --- a/examples/QuickStartDemo/ProxyServer/ProxyImp.h +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROXY_IMP_H_ -#define __PROXY_IMP_H_ - -#include "servant/Application.h" -#include "Hello.h" -#include "Proxy.h" - -using namespace TestApp; - -/** - * - * - */ -class ProxyImp : public TestApp::Proxy -{ -public: - /** - * - */ - virtual ~ProxyImp() {} - - /** - * - */ - virtual void initialize(); - - /** - * - */ - virtual void destroy(); - - /** - * - */ - virtual tars::Int32 test(tars::TarsCurrentPtr current); - - /** - * - */ - virtual tars::Int32 testProxy(const std::string& sReq, std::string &sRsp, tars::TarsCurrentPtr current); - -private: - HelloPrx _prx; - -}; -///////////////////////////////////////////////////// -#endif diff --git a/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp b/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp deleted file mode 100644 index 58def12..0000000 --- a/examples/QuickStartDemo/ProxyServer/ProxyServer.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#include "ProxyServer.h" -#include "ProxyImp.h" - -using namespace std; - -ProxyServer g_app; - -///////////////////////////////////////////////////////////////// -void ProxyServer::initialize() -{ - //initialize application here: - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".ProxyObj"); - -} -///////////////////////////////////////////////////////////////// -void ProxyServer::destroyApp() -{ - //destroy application here: - //... -} - -///////////////////////////////////////////////////////////////// -int -main(int argc, char* argv[]) -{ - try - { - g_app.main(argc, argv); - g_app.waitForShutdown(); - } - catch (std::exception& e) - { - cerr << "std::exception:" << e.what() << std::endl; - } - catch (...) - { - cerr << "unknown exception." << std::endl; - } - return -1; -} -///////////////////////////////////////////////////////////////// diff --git a/examples/QuickStartDemo/ProxyServer/ProxyServer.h b/examples/QuickStartDemo/ProxyServer/ProxyServer.h deleted file mode 100644 index 2e6d3a4..0000000 --- a/examples/QuickStartDemo/ProxyServer/ProxyServer.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Tars available. - * - * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -#ifndef __PROXY_SERVER_H_ -#define __PROXY_SERVER_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * - **/ -class ProxyServer : public Application -{ -public: - /** - * - **/ - virtual ~ProxyServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -protected: - -}; - -extern ProxyServer g_app; - -//////////////////////////////////////////// -#endif diff --git a/examples/QuickStartDemo/ProxyServer/Server/CMakeLists.txt b/examples/QuickStartDemo/ProxyServer/Server/CMakeLists.txt new file mode 100644 index 0000000..47024bc --- /dev/null +++ b/examples/QuickStartDemo/ProxyServer/Server/CMakeLists.txt @@ -0,0 +1,3 @@ +build_tars_server("ProxyServer") + + diff --git a/examples/QuickStartDemo/ProxyServer/makefile b/examples/QuickStartDemo/ProxyServer/makefile deleted file mode 100644 index 7138201..0000000 --- a/examples/QuickStartDemo/ProxyServer/makefile +++ /dev/null @@ -1,17 +0,0 @@ - -#----------------------------------------------------------------------- - -APP := TestApp -TARGET := ProxyServer -CONFIG := -STRIP_FLAG:= N -TARS2CPP_FLAG:= - -INCLUDE += -LIB += - -#----------------------------------------------------------------------- -include /home/tarsproto/TestApp/HelloServer/HelloServer.mk -include /usr/local/tars/cpp/makefile/makefile.tars - -#----------------------------------------------------------------------- diff --git a/examples/StressDemo/CMakeLists.txt b/examples/StressDemo/CMakeLists.txt new file mode 100644 index 0000000..e6a5cfd --- /dev/null +++ b/examples/StressDemo/CMakeLists.txt @@ -0,0 +1,5 @@ + +include_directories(TarsStressServer) +add_subdirectory(TarsStressServer) +add_subdirectory(TarsStressClient) + diff --git a/examples/StressDemo/TarsStressClient/CMakeLists.txt b/examples/StressDemo/TarsStressClient/CMakeLists.txt new file mode 100644 index 0000000..3874e8c --- /dev/null +++ b/examples/StressDemo/TarsStressClient/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("TarsStressClient") \ No newline at end of file diff --git a/examples/StressDemo/TarsStressServer/CMakeLists.txt b/examples/StressDemo/TarsStressServer/CMakeLists.txt new file mode 100644 index 0000000..a5ff52a --- /dev/null +++ b/examples/StressDemo/TarsStressServer/CMakeLists.txt @@ -0,0 +1 @@ +build_tars_server("TarsStressServer") \ No newline at end of file diff --git a/servant/libservant/Application.cpp b/servant/libservant/Application.cpp index c041884..2a6e101 100644 --- a/servant/libservant/Application.cpp +++ b/servant/libservant/Application.cpp @@ -61,6 +61,8 @@ static BOOL WINAPI HandlerRoutine(DWORD dwCtrlType) } #endif + +std::string ServerConfig::TarsPath; //服务路径 std::string ServerConfig::Application; //应用名称 std::string ServerConfig::ServerName; //服务名称,一个服务名称含一个或多个服务标识 std::string ServerConfig::LocalIp; //本机IP @@ -1238,7 +1240,7 @@ void Application::initializeServer() if(!ServerConfig::Local.empty()) { - ServantHelperManager::getInstance()->addServant("AdminObj"); + ServantHelperManager::getInstance()->addServant("AdminObj", this); ServantHelperManager::getInstance()->setAdapterServant("AdminAdapter", "AdminObj"); diff --git a/servant/libservant/Servant.cpp b/servant/libservant/Servant.cpp index 1b762bc..dc59d27 100644 --- a/servant/libservant/Servant.cpp +++ b/servant/libservant/Servant.cpp @@ -47,6 +47,16 @@ string Servant::getName() const return _name; } +void Servant::setApplication(Application *application) +{ + _application = application; +} + +Application* Servant::getApplication() const +{ + return _application; +} + void Servant::setHandle(TC_EpollServer::Handle* handle) { _handle = handle; diff --git a/servant/libservant/ServantHelper.cpp b/servant/libservant/ServantHelper.cpp index 98b955f..a24a3d9 100644 --- a/servant/libservant/ServantHelper.cpp +++ b/servant/libservant/ServantHelper.cpp @@ -48,7 +48,7 @@ void ServantHelperManager::setAdapterServant(const string &sAdapter, const strin bool ServantHelperManager::setDyeing(const string & sDyeingKey, const string & sDyeingServant, const string & sDyeingInterface) { - TC_LockT lock(_mutex); + TC_LockT lock(_mutex); _dyeingKey = sDyeingKey; _dyeingServant = sDyeingServant; @@ -61,7 +61,7 @@ bool ServantHelperManager::setDyeing(const string & sDyeingKey, const string & s bool ServantHelperManager::isDyeingReq(const string & sKey, const string & sServant, const string & sInterface) { - TC_LockT lock(_mutex); + TC_LockT lock(_mutex); return ((_dyeingKey == sKey) && (_dyeingServant == sServant) && (_dyeingInterface == "" || _dyeingInterface == sInterface) ); diff --git a/servant/promise/promise.h b/servant/promise/promise.h index b04ff03..a37daeb 100644 --- a/servant/promise/promise.h +++ b/servant/promise/promise.h @@ -746,21 +746,39 @@ public: virtual ~Future() {} template - Future::type> then(const std::function& callback) const + Future then(const std::function&)>& callback) const { - typedef typename detail::resolved_type::type value_type; + // typedef typename detail::resolved_type::type value_type; if (!this->m_future) { throwException(FutureUninitializedException(__FILE__, __LINE__)); } - Promise promise; - this->m_future->registerCallback(TC_Bind(&detail::SequentialCallback::template run, - tc_owned(new detail::SequentialCallback(callback, promise)))); + Promise promise; + shared_ptr> ptr(new detail::SequentialCallback(callback, promise)); + + this->m_future->registerCallback(std::bind(&detail::SequentialCallback::template run, ptr)); return promise.getFuture(); } + + // template + // Future::type> then(const std::function&)>& callback) const + // { + // typedef typename detail::resolved_type::type value_type; + + // if (!this->m_future) + // { + // throwException(FutureUninitializedException(__FILE__, __LINE__)); + // } + + // Promise promise; + // this->m_future->registerCallback(TC_Bind(&detail::SequentialCallback::template run, + // tc_owned(new detail::SequentialCallback(callback, promise)))); + // return promise.getFuture(); + // } + private: Future(const typename detail::FutureBase::FuturePtr& future) @@ -784,7 +802,7 @@ public: virtual ~Future() {} template - Future::type> then(const std::function& callback) const + Future::type> then(const std::function&)>& callback) const { typedef typename detail::resolved_type::type value_type; diff --git a/servant/promise/when_all.h b/servant/promise/when_all.h index 10ae504..9efed10 100644 --- a/servant/promise/when_all.h +++ b/servant/promise/when_all.h @@ -56,10 +56,10 @@ namespace tars }; - // FutureAllValueType is supposed to be of type promise::std::tuple or - // std::vector. + // FutureAllValueType is supposed to be of type tars::std::tuple or + // std::vector. // - // This implementation is for promise::std::tuple<>. + // This implementation is for tars::std::tuple<>. template class ParallelAllCallback : private ParallelAllCallbackBase { @@ -88,7 +88,7 @@ namespace tars } catch (...) { - this->m_promise_all.setException(promise::currentException()); + this->m_promise_all.setException(currentException()); } return; } @@ -100,7 +100,7 @@ namespace tars }; - // This implementation is specialized for std::vector >. + // This implementation is specialized for std::vector >. template class ParallelAllCallback > : private ParallelAllCallbackBase > { @@ -133,7 +133,7 @@ namespace tars } catch (...) { - this->m_promise_all.setException(promise::currentException()); + this->m_promise_all.setException(tars::currentException()); } return; } @@ -181,8 +181,8 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); return promise_all.getFuture(); } @@ -196,9 +196,9 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); + future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback))); return promise_all.getFuture(); } @@ -212,10 +212,10 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); + future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback))); + future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback))); return promise_all.getFuture(); } @@ -230,11 +230,11 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); + future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback))); + future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback))); + future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback))); return promise_all.getFuture(); } @@ -249,12 +249,12 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); + future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback))); + future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback))); + future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback))); + future6.then(std::bind(&WhenAllCallback::template on_future<5>, (future_callback))); return promise_all.getFuture(); } @@ -269,13 +269,13 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback))); - future7.then(tars::TC_Bind(&WhenAllCallback::template on_future<6>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); + future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback))); + future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback))); + future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback))); + future6.then(std::bind(&WhenAllCallback::template on_future<5>, (future_callback))); + future7.then(std::bind(&WhenAllCallback::template on_future<6>, (future_callback))); return promise_all.getFuture(); } @@ -290,14 +290,14 @@ namespace tars PromiseAll promise_all; std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback))); - future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback))); - future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback))); - future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback))); - future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback))); - future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback))); - future7.then(tars::TC_Bind(&WhenAllCallback::template on_future<6>, tars::tc_shared(future_callback))); - future8.then(tars::TC_Bind(&WhenAllCallback::template on_future<7>, tars::tc_shared(future_callback))); + future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback))); + future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback))); + future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback))); + future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback))); + future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback))); + future6.then(std::bind(&WhenAllCallback::template on_future<5>, (future_callback))); + future7.then(std::bind(&WhenAllCallback::template on_future<6>, (future_callback))); + future8.then(std::bind(&WhenAllCallback::template on_future<7>, (future_callback))); return promise_all.getFuture(); } @@ -321,8 +321,9 @@ namespace tars for (i = 0; first != last; ++first, ++i) { - first->then(tars::TC_Bind(&detail::VectorParallelCallback::on_future, - tars::tc_owned(new detail::VectorParallelCallback(when_all_callback, i)))); + std::shared_ptr> ptr(new detail::VectorParallelCallback(when_all_callback, i)); + first->then(std::bind(&detail::VectorParallelCallback::on_future, ptr)); + // tars::tc_owned(new detail::VectorParallelCallback(when_all_callback, i)))); } } return promise_all.getFuture(); diff --git a/servant/servant/Servant.h b/servant/servant/Servant.h index 8ea5761..40c1063 100644 --- a/servant/servant/Servant.h +++ b/servant/servant/Servant.h @@ -27,6 +27,7 @@ namespace tars { class BaseNotify; +class Application; //////////////////////////////////////////////////////////////////// /** * 每个对象的基类 @@ -62,6 +63,18 @@ public: */ void setHandle(TC_EpollServer::Handle* handle); + /** + * 设置全局的应用 + * @param application + */ + void setApplication(Application *application); + + /** + * 获取应用 + * @return + */ + Application* getApplication() const; + /** * 获取所属的Handle * @return HandlePtr& @@ -166,6 +179,11 @@ protected: */ string _name; + /** + * 应用 + */ + Application *_application; + /** * 所属的Handle */ diff --git a/servant/servant/ServantHelper.h b/servant/servant/ServantHelper.h index 366a299..85bf709 100644 --- a/servant/servant/ServantHelper.h +++ b/servant/servant/ServantHelper.h @@ -41,10 +41,13 @@ typedef TC_AutoPtr ServantHelperCreationPtr; /** * Servant */ + template struct ServantCreation : public ServantHelperCreation { - ServantPtr create(const string &s) { T *p = new T; p->setName(s); return p; } + ServantCreation(Application *application) : _application(application){} + ServantPtr create(const string &s) { T *p = new T; p->setName(s); p->setApplication(_application); return p; } + Application *_application; }; ////////////////////////////////////////////////////////////////////////////// @@ -69,14 +72,14 @@ public: * @param id */ template - void addServant(const string &id,bool check = false) + void addServant(const string &id, Application *application, bool check = false) { if(check && _servant_adapter.end() == _servant_adapter.find(id)) { - cerr<<"[TARS]ServantHelperManager::addServant "<< id <<" not find adapter.(maybe not conf in the web)"<(); + _servant_creator[id] = new ServantCreation(application); } /** @@ -99,14 +102,15 @@ public: * @param sAdapter * @return string */ - string getAdapterServant(const string &sAdapter) + const string &getAdapterServant(const string &sAdapter) const { - map::iterator it = _adapter_servant.find(sAdapter); + static const string s = "(NO TARS PROTOCOL)"; + auto it = _adapter_servant.find(sAdapter); if(it != _adapter_servant.end()) { return it->second; } - return "(NO TARS PROTOCOL)"; + return s; } /** @@ -114,21 +118,22 @@ public: * @param sServant * @return string */ - string getServantAdapter(const string& sServant) + const string &getServantAdapter(const string& sServant) const { - map::iterator it = _servant_adapter.find(sServant); + static const string s = ""; + + auto it = _servant_adapter.find(sServant); if(it != _servant_adapter.end()) { return it->second; } - return ""; + return s; } - /** * 获取Adapter/Servant对应表 * @return map */ - map getAdapterServant() {return _adapter_servant;} + const map & getAdapterServant() const {return _adapter_servant;} /** * 设置染色信息 @@ -177,7 +182,7 @@ protected: /** * 锁 */ - TC_ThreadMutex _mutex; + TC_SpinLock _mutex; /** * 是否染色 diff --git a/util/src/CMakeLists.txt b/util/src/CMakeLists.txt index c0874ae..8bc35da 100644 --- a/util/src/CMakeLists.txt +++ b/util/src/CMakeLists.txt @@ -1,7 +1,5 @@ set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) -include_directories(${PROJECT_SOURCE_DIR}/include) - aux_source_directory(. DIR_SRCS) # if (CMAKE_SIZEOF_VOID_P EQUAL 8)