diff --git a/cmake/modules/FindMySQL.cmake b/cmake/modules/FindMySQL.cmake new file mode 100644 index 0000000..0168725 --- /dev/null +++ b/cmake/modules/FindMySQL.cmake @@ -0,0 +1,78 @@ +# - Try to find MySQL. +# Once done this will define: +# MYSQL_FOUND - If false, do not try to use MySQL. +# MYSQL_INCLUDE_DIRS - Where to find mysql.h, etc. +# MYSQL_LIBRARIES - The libraries to link against. +# MYSQL_VERSION_STRING - Version in a string of MySQL. +# +# Created by RenatoUtsch based on eAthena implementation. +# +# + +find_path(MYSQL_INCLUDE_DIR + NAMES mysql.h + PATHS /usr/local/opt/mysql-client/include/mysql /usr/include/mysql /usr/local/include/mysql /usr/local/mysql/include/ + $ENV{PROGRAMFILES}/MySQL/*/include $ENV{SYSTEMDRIVE}/MySQL/*/include C:/mysql/include) + +set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".lib") + +find_library(MYSQL_LIBRARY + NAMES mysqlclient mysqlclient_r + PATHS /usr/local/opt/mysql-client/lib /lib/mysql /lib64/mysql /usr/lib/mysql /usr/lib64/mysql /usr/local/lib/mysql /usr/local/lib64/mysql /usr/local/mysql/lib/ + $ENV{PROGRAMFILES}/MySQL/*/lib $ENV{SYSTEMDRIVE}/MySQL/*/lib C:/mysql/lib/vs14) + +find_path(SSL_INCLUDE_DIR + NAMES openssl/ssl.h + PATHS + /usr/local/Cellar/openssl@1.1/1.1.1d /usr/include /usr/local/include /opt/local/include /sw/include + PATH_SUFFIXES + include) + +find_library(_SSL_LIB + NAMES ssl + PATHS + /usr/local/Cellar/openssl@1.1/1.1.1d /usr/lib /usr/local/lib /opt/local/lib /sw/lib + PATH_SUFFIXES + lib) + +find_library(_CRYPTO_LIB + NAMES crypto + PATHS + /usr/local/Cellar/openssl@1.1/1.1.1d /usr/lib /usr/local/lib /opt/local/lib /sw/lib + PATH_SUFFIXES + lib) + + +if (MYSQL_INCLUDE_DIR AND EXISTS "${MYSQL_INCLUDE_DIR}/mysql_version.h") + file(STRINGS "${MYSQL_INCLUDE_DIR}/mysql_version.h" + MYSQL_VERSION_H REGEX "^#define[ \t]+MYSQL_SERVER_VERSION[ \t]+\"[^\"]+\".*$") + string(REGEX REPLACE + "^.*MYSQL_SERVER_VERSION[ \t]+\"([^\"]+)\".*$" "\\1" MYSQL_VERSION_STRING + "${MYSQL_VERSION_H}") +endif () + +# handle the QUIETLY and REQUIRED arguments and set MYSQL_FOUND to TRUE if +# all listed variables are TRUE +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(MYSQL DEFAULT_MSG MYSQL_LIBRARY MYSQL_INCLUDE_DIR MYSQL_VERSION_STRING) + +if(MYSQL_FOUND) + set(MYSQL_INCLUDE_DIRS ${MYSQL_INCLUDE_DIR}) + set(MYSQL_LIBRARIES ${MYSQL_LIBRARY}) +endif() + +mark_as_advanced(MYSQL_INCLUDE_DIR MYSQL_LIBRARY) + +find_package_handle_standard_args(ssl DEFAULT_MSG _SSL_LIB _CRYPTO_LIB SSL_INCLUDE_DIR) +mark_as_advanced(SSL_INCLUDE_DIR _SSL_LIB _CRYPTO_LIB) + +if(SSL_FOUND) + set(MYSQL_INCLUDE_DIRS ${MYSQL_INCLUDE_DIR} ${SSL_INCLUDE_DIR}) + set(MYSQL_LIBRARIES ${MYSQL_LIBRARY} ${_SSL_LIB} ${_CRYPTO_LIB}) +endif() + +message("------------MySQL------------") +message("MYSQL_VERSION_STRING=${MYSQL_VERSION_STRING}") +message("MYSQL_INCLUDE_DIRS=${MYSQL_INCLUDE_DIRS}") +message("MYSQL_LIBRARIES=${MYSQL_LIBRARIES}") +message("------------MySQL------------") \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..5ccf7c1 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,11 @@ + +add_subdirectory(CoroutineDemo) +add_subdirectory(HttpDemo) +add_subdirectory(PushDemo) +add_subdirectory(QuickStartDemo) +add_subdirectory(StressDemo) + +add_custom_target(run-quick-start + DEPENDS QuickStartDemo QuickStartDemoClient + COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/scripts/run-quick-start.sh + COMMENT "call") diff --git a/examples/CoroutineDemo/AServer/AServant.h b/examples/CoroutineDemo/AServer/AServant.h new file mode 100644 index 0000000..afa315d --- /dev/null +++ b/examples/CoroutineDemo/AServer/AServant.h @@ -0,0 +1,648 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __ASERVANT_H_ +#define __ASERVANT_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class AServantPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~AServantPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testInt(tars::Int32 ret, tars::Int32 iOut) + { throw std::runtime_error("callback_testInt() override incorrect."); } + virtual void callback_testInt_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testInt_exception() override incorrect."); } + + virtual void callback_testStr(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testStr() override incorrect."); } + virtual void callback_testStr_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testStr_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __AServant_all[]= + { + "test", + "testInt", + "testStr" + }; + pair r = equal_range(__AServant_all, __AServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __AServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testInt_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + tars::Int32 iOut; + _is.read(iOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testInt(_ret, iOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testStr_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testStr(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr AServantPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class AServantCoroPrxCallback: public AServantPrxCallback + { + public: + virtual ~AServantCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __AServant_all[]= + { + "test", + "testInt", + "testStr" + }; + + pair r = equal_range(__AServant_all, __AServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __AServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testInt_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + tars::Int32 iOut; + _is.read(iOut, 2, true); + setResponseContext(msg->response->context); + + callback_testInt(_ret, iOut); + + } + catch(std::exception &ex) + { + callback_testInt_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testInt_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testStr_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testStr(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testStr_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testStr_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr AServantCoroPrxCallbackPtr; + + /* proxy for client */ + class AServantProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(AServantPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(AServantCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(iIn, 1); + _os.write(iOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testInt", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(iOut, 2, true); + return _ret; + } + + void async_testInt(AServantPrxCallbackPtr callback,tars::Int32 iIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(iIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback); + } + + void coro_testInt(AServantCoroPrxCallbackPtr callback,tars::Int32 iIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(iIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback); + } + + tars::Int32 testStr(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testStr", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testStr(AServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback); + } + + void coro_testStr(AServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback); + } + + AServantProxy* tars_hash(int64_t key) + { + return (AServantProxy*)ServantProxy::tars_hash(key); + } + + AServantProxy* tars_consistent_hash(int64_t key) + { + return (AServantProxy*)ServantProxy::tars_consistent_hash(key); + } + + AServantProxy* tars_set_timeout(int msecond) + { + return (AServantProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "AServantProxy"; } + }; + typedef tars::TC_AutoPtr AServantPrx; + + /* servant for server */ + class AServant : public tars::Servant + { + public: + virtual ~AServant(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testInt(tars::TarsCurrentPtr current, tars::Int32 _ret, tars::Int32 iOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("iOut", iOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(iOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testStr(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testStr(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__AServant_all[]= + { + "test", + "testInt", + "testStr" + }; + + pair r = equal_range(__Test__AServant_all, __Test__AServant_all+3, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__AServant_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + tars::Int32 iIn; + tars::Int32 iOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("iIn", iIn); + tarsAttr.getByDefault("iOut", iOut, iOut); + } + else + { + _is.read(iIn, 1, true); + _is.read(iOut, 2, false); + } + tars::Int32 _ret = testInt(iIn,iOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("iOut", iOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(iOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testStr(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/CoroutineDemo/BServer/AServant.h b/examples/CoroutineDemo/BServer/AServant.h new file mode 100644 index 0000000..afa315d --- /dev/null +++ b/examples/CoroutineDemo/BServer/AServant.h @@ -0,0 +1,648 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __ASERVANT_H_ +#define __ASERVANT_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class AServantPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~AServantPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testInt(tars::Int32 ret, tars::Int32 iOut) + { throw std::runtime_error("callback_testInt() override incorrect."); } + virtual void callback_testInt_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testInt_exception() override incorrect."); } + + virtual void callback_testStr(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testStr() override incorrect."); } + virtual void callback_testStr_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testStr_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __AServant_all[]= + { + "test", + "testInt", + "testStr" + }; + pair r = equal_range(__AServant_all, __AServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __AServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testInt_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + tars::Int32 iOut; + _is.read(iOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testInt(_ret, iOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testStr_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testStr(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr AServantPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class AServantCoroPrxCallback: public AServantPrxCallback + { + public: + virtual ~AServantCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __AServant_all[]= + { + "test", + "testInt", + "testStr" + }; + + pair r = equal_range(__AServant_all, __AServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __AServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testInt_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + tars::Int32 iOut; + _is.read(iOut, 2, true); + setResponseContext(msg->response->context); + + callback_testInt(_ret, iOut); + + } + catch(std::exception &ex) + { + callback_testInt_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testInt_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testStr_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testStr(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testStr_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testStr_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr AServantCoroPrxCallbackPtr; + + /* proxy for client */ + class AServantProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(AServantPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(AServantCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(iIn, 1); + _os.write(iOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testInt", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(iOut, 2, true); + return _ret; + } + + void async_testInt(AServantPrxCallbackPtr callback,tars::Int32 iIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(iIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback); + } + + void coro_testInt(AServantCoroPrxCallbackPtr callback,tars::Int32 iIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(iIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback); + } + + tars::Int32 testStr(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testStr", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testStr(AServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback); + } + + void coro_testStr(AServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback); + } + + AServantProxy* tars_hash(int64_t key) + { + return (AServantProxy*)ServantProxy::tars_hash(key); + } + + AServantProxy* tars_consistent_hash(int64_t key) + { + return (AServantProxy*)ServantProxy::tars_consistent_hash(key); + } + + AServantProxy* tars_set_timeout(int msecond) + { + return (AServantProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "AServantProxy"; } + }; + typedef tars::TC_AutoPtr AServantPrx; + + /* servant for server */ + class AServant : public tars::Servant + { + public: + virtual ~AServant(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testInt(tars::TarsCurrentPtr current, tars::Int32 _ret, tars::Int32 iOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("iOut", iOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(iOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testStr(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testStr(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__AServant_all[]= + { + "test", + "testInt", + "testStr" + }; + + pair r = equal_range(__Test__AServant_all, __Test__AServant_all+3, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__AServant_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + tars::Int32 iIn; + tars::Int32 iOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("iIn", iIn); + tarsAttr.getByDefault("iOut", iOut, iOut); + } + else + { + _is.read(iIn, 1, true); + _is.read(iOut, 2, false); + } + tars::Int32 _ret = testInt(iIn,iOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("iOut", iOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(iOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testStr(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/CoroutineDemo/BServer/BServant.h b/examples/CoroutineDemo/BServer/BServant.h new file mode 100644 index 0000000..b3092d6 --- /dev/null +++ b/examples/CoroutineDemo/BServer/BServant.h @@ -0,0 +1,648 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __BSERVANT_H_ +#define __BSERVANT_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class BServantPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~BServantPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testCoroParallel(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroParallel() override incorrect."); } + virtual void callback_testCoroParallel_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroParallel_exception() override incorrect."); } + + virtual void callback_testCoroSerial(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroSerial() override incorrect."); } + virtual void callback_testCoroSerial_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroSerial_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr BServantPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class BServantCoroPrxCallback: public BServantPrxCallback + { + public: + virtual ~BServantCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr BServantCoroPrxCallbackPtr; + + /* proxy for client */ + class BServantProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(BServantPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(BServantCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroParallel(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroSerial(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + BServantProxy* tars_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_hash(key); + } + + BServantProxy* tars_consistent_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_consistent_hash(key); + } + + BServantProxy* tars_set_timeout(int msecond) + { + return (BServantProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "BServantProxy"; } + }; + typedef tars::TC_AutoPtr BServantPrx; + + /* servant for server */ + class BServant : public tars::Servant + { + public: + virtual ~BServant(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroParallel(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroSerial(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__Test__BServant_all, __Test__BServant_all+3, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__BServant_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroParallel(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroSerial(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/CoroutineDemo/client/BServant.h b/examples/CoroutineDemo/client/BServant.h new file mode 100644 index 0000000..b3092d6 --- /dev/null +++ b/examples/CoroutineDemo/client/BServant.h @@ -0,0 +1,648 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __BSERVANT_H_ +#define __BSERVANT_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class BServantPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~BServantPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testCoroParallel(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroParallel() override incorrect."); } + virtual void callback_testCoroParallel_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroParallel_exception() override incorrect."); } + + virtual void callback_testCoroSerial(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroSerial() override incorrect."); } + virtual void callback_testCoroSerial_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroSerial_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr BServantPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class BServantCoroPrxCallback: public BServantPrxCallback + { + public: + virtual ~BServantCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr BServantCoroPrxCallbackPtr; + + /* proxy for client */ + class BServantProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(BServantPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(BServantCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroParallel(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroSerial(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + BServantProxy* tars_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_hash(key); + } + + BServantProxy* tars_consistent_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_consistent_hash(key); + } + + BServantProxy* tars_set_timeout(int msecond) + { + return (BServantProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "BServantProxy"; } + }; + typedef tars::TC_AutoPtr BServantPrx; + + /* servant for server */ + class BServant : public tars::Servant + { + public: + virtual ~BServant(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroParallel(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroSerial(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__Test__BServant_all, __Test__BServant_all+3, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__BServant_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroParallel(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroSerial(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/CoroutineDemo/testCoro/BServant.h b/examples/CoroutineDemo/testCoro/BServant.h new file mode 100644 index 0000000..b3092d6 --- /dev/null +++ b/examples/CoroutineDemo/testCoro/BServant.h @@ -0,0 +1,648 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __BSERVANT_H_ +#define __BSERVANT_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class BServantPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~BServantPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testCoroParallel(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroParallel() override incorrect."); } + virtual void callback_testCoroParallel_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroParallel_exception() override incorrect."); } + + virtual void callback_testCoroSerial(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroSerial() override incorrect."); } + virtual void callback_testCoroSerial_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroSerial_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr BServantPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class BServantCoroPrxCallback: public BServantPrxCallback + { + public: + virtual ~BServantCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr BServantCoroPrxCallbackPtr; + + /* proxy for client */ + class BServantProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(BServantPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(BServantCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroParallel(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroSerial(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + BServantProxy* tars_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_hash(key); + } + + BServantProxy* tars_consistent_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_consistent_hash(key); + } + + BServantProxy* tars_set_timeout(int msecond) + { + return (BServantProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "BServantProxy"; } + }; + typedef tars::TC_AutoPtr BServantPrx; + + /* servant for server */ + class BServant : public tars::Servant + { + public: + virtual ~BServant(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroParallel(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroSerial(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__Test__BServant_all, __Test__BServant_all+3, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__BServant_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroParallel(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroSerial(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/CoroutineDemo/testParallelCoro/BServant.h b/examples/CoroutineDemo/testParallelCoro/BServant.h new file mode 100644 index 0000000..b3092d6 --- /dev/null +++ b/examples/CoroutineDemo/testParallelCoro/BServant.h @@ -0,0 +1,648 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __BSERVANT_H_ +#define __BSERVANT_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class BServantPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~BServantPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testCoroParallel(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroParallel() override incorrect."); } + virtual void callback_testCoroParallel_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroParallel_exception() override incorrect."); } + + virtual void callback_testCoroSerial(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testCoroSerial() override incorrect."); } + virtual void callback_testCoroSerial_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testCoroSerial_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr BServantPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class BServantCoroPrxCallback: public BServantPrxCallback + { + public: + virtual ~BServantCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__BServant_all, __BServant_all+3, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __BServant_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroParallel_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroParallel(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroParallel_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testCoroSerial_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testCoroSerial(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testCoroSerial_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr BServantCoroPrxCallbackPtr; + + /* proxy for client */ + class BServantProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(BServantPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(BServantCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroParallel(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback); + } + + tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testCoroSerial(BServantPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback); + } + + BServantProxy* tars_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_hash(key); + } + + BServantProxy* tars_consistent_hash(int64_t key) + { + return (BServantProxy*)ServantProxy::tars_consistent_hash(key); + } + + BServantProxy* tars_set_timeout(int msecond) + { + return (BServantProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "BServantProxy"; } + }; + typedef tars::TC_AutoPtr BServantPrx; + + /* servant for server */ + class BServant : public tars::Servant + { + public: + virtual ~BServant(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroParallel(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testCoroSerial(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__BServant_all[]= + { + "test", + "testCoroParallel", + "testCoroSerial" + }; + + pair r = equal_range(__Test__BServant_all, __Test__BServant_all+3, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__BServant_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroParallel(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 2: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testCoroSerial(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/PromiseDemo/AServer/AServant.tars b/examples/PromiseDemo/AServer/AServant.tars deleted file mode 100644 index 1255254..0000000 --- a/examples/PromiseDemo/AServer/AServant.tars +++ /dev/null @@ -1,27 +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 Test -{ - -interface AServant -{ - int queryResultSerial(string sIn, out string sOut); - - int queryResultParallel(string sIn, out string sOut); -}; - -}; diff --git a/examples/PromiseDemo/AServer/AServantImp.cpp b/examples/PromiseDemo/AServer/AServantImp.cpp deleted file mode 100644 index adeab46..0000000 --- a/examples/PromiseDemo/AServer/AServantImp.cpp +++ /dev/null @@ -1,267 +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 "AServantImp.h" -#include "AServer.h" -#include "servant/Application.h" -#include "servant/Communicator.h" - -using namespace std; -using namespace tars; - -////////////////////////////////////////////////////// -class BServantCallback : public BServantPrxCallback -{ - -public: - BServantCallback(TarsCurrentPtr ¤t) - : _current(current) - {} - - BServantCallback(TarsCurrentPtr ¤t, const tars::Promise &promise) - : _current(current) - , _promise(promise) - {} - - void callback_queryResult(tars::Int32 ret, const std::string &sOut) - { - if(ret == 0) - { - _promise.setValue(sOut); - } - else - { - handExp("callback_queryResult", ret); - } - } - void callback_queryResult_exception(tars::Int32 ret) - { - handExp("callback_queryResult_exception", ret); - } - -private: - void handExp(const std::string &sFuncName, tars::Int32 ret) - { - string s("sFuncName:"); - s += sFuncName; - s += "|ret:"; - s += TC_Common::tostr(ret); - - _promise.setException(tars::copyException(s)); - - TLOGDEBUG("ServerPrxCallback handExp:" << s << endl); - } - -private: - - TarsCurrentPtr _current; - tars::Promise _promise; -}; -////////////////////////////////////////////////////// -class CServantCallback : public CServantPrxCallback -{ - -public: - CServantCallback(TarsCurrentPtr ¤t) - : _current(current) - {} - - CServantCallback(TarsCurrentPtr ¤t, const tars::Promise &promise) - : _current(current) - , _promise(promise) - {} - - void callback_queryResult(tars::Int32 ret, const std::string &sOut) - { - if(ret == 0) - { - _promise.setValue(sOut); - } - else - { - handExp("callback_queryResult", ret); - } - } - void callback_queryResult_exception(tars::Int32 ret) - { - handExp("callback_queryResult_exception", ret); - } - -private: - void handExp(const std::string &sFuncName, tars::Int32 ret) - { - string s("sFuncName:"); - s += sFuncName; - s += "|ret:"; - s += TC_Common::tostr(ret); - - _promise.setException(tars::copyException(s)); - - TLOGDEBUG("ServerPrxCallback handExp:" << s << endl); - } - -private: - - TarsCurrentPtr _current; - tars::Promise _promise; -}; -////////////////////////////////////////////////////// -tars::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) -{ - tars::Promise promise; - - Test::BServantPrxCallbackPtr cb = new BServantCallback(current, promise); - - prx->async_queryResult(cb, sIn); - - return promise.getFuture(); -} -////////////////////////////////////////////////////// -tars::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current) -{ - tars::Promise promise; - - Test::CServantPrxCallbackPtr cb = new CServantCallback(current, promise); - - prx->async_queryResult(cb, sIn); - - return promise.getFuture(); -} -////////////////////////////////////////////////////// -tars::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future& future) -{ - std::string sResult(""); - std::string sException(""); - try - { - sResult = future.get(); - - return sendCReq(prx, sResult, current); - } - catch (exception& e) - { - TLOGDEBUG("Exception:" << e.what() << endl); - sException = e.what(); - } - - tars::Promise promise; - promise.setValue(sException); - - return promise.getFuture(); -} -////////////////////////////////////////////////////// -int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future& future) -{ - int ret = 0; - std::string sResult(""); - try - { - sResult = future.get(); - } - catch (exception& e) - { - ret = -1; - sResult = e.what(); - - TLOGDEBUG("Exception:" << e.what() << endl); - } - - AServant::async_response_queryResultSerial(current, ret, sResult); - - return 0; -} -////////////////////////////////////////////////////// -int handleBCRspAndReturnClient(TarsCurrentPtr current, const tars::Future, tars::Future > >& allFuture) -{ - int ret = 0; - std::string sResult(""); - try - { - std::tuple, tars::Future > tupleFuture = allFuture.get(); - - std::string sResult1 = std::get<0>(tupleFuture).get(); - std::string sResult2 = std::get<1>(tupleFuture).get(); - - sResult = sResult1; - sResult += "|"; - sResult += sResult2; - } - catch (exception& e) - { - ret = -1; - sResult = e.what(); - - TLOGDEBUG("Exception:" << e.what() << endl); - } - - AServant::async_response_queryResultParallel(current, ret, sResult); - - return 0; -} -////////////////////////////////////////////////////// -void AServantImp::initialize() -{ - //initialize servant here: - //... - _pPrxB = Application::getCommunicator()->stringToProxy("Test.BServer.BServantObj"); - _pPrxC = Application::getCommunicator()->stringToProxy("Test.CServer.CServantObj"); -} -////////////////////////////////////////////////////// -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); - - tars::Future f = sendBReq(_pPrxB, sIn, 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; -} -////////////////////////////////////////////////////// -tars::Int32 AServantImp::queryResultParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - current->setResponse(false); - - tars::Future f1 = sendBReq(_pPrxB, sIn, current); - - tars::Future f2 = sendCReq(_pPrxC, sIn, current); - - tars::Future, tars::Future > > f_all = tars::whenAll(f1, f2); - - f_all.then(std::bind(&handleBCRspAndReturnClient, current)); - - return 0; -} - diff --git a/examples/PromiseDemo/AServer/AServantImp.h b/examples/PromiseDemo/AServer/AServantImp.h deleted file mode 100644 index c73bf0b..0000000 --- a/examples/PromiseDemo/AServer/AServantImp.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 _BServantImp_H_ -#define _BServantImp_H_ - -#include "servant/Application.h" -#include "AServant.h" -#include "BServant.h" -#include "CServant.h" -#include "promise/promise.h" -//#include "promise/tuple.h" - -#include "promise/when_all.h" - -using namespace Test; - -/////////////////////////////////// -tars::Future sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); - -tars::Future handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future& future); - -tars::Future sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current); - -int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future& future); - -/////////////////////////////////// -int handleBCRspAndReturnClient(TarsCurrentPtr current, const tars::Future, tars::Future > >& allFuture); - -/////////////////////////////////// -class AServantImp : public Test::AServant -{ -public: - - virtual ~AServantImp() {} - - virtual void initialize(); - - virtual void destroy(); - - tars::Int32 queryResultSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - - tars::Int32 queryResultParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); - -private: - BServantPrx _pPrxB; - CServantPrx _pPrxC; -}; -///////////////////////////////////////////////////// -#endif diff --git a/examples/PromiseDemo/AServer/AServer.cpp b/examples/PromiseDemo/AServer/AServer.cpp deleted file mode 100644 index 09dbf2c..0000000 --- a/examples/PromiseDemo/AServer/AServer.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 "AServer.h" -#include "AServantImp.h" - -using namespace std; - -AServer g_app; - -///////////////////////////////////////////////////////////////// -void AServer::initialize() -{ - //initialize application here: - //... - - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".AServantObj"); -} -///////////////////////////////////////////////////////////////// -void AServer::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/PromiseDemo/AServer/BServant.tars b/examples/PromiseDemo/AServer/BServant.tars deleted file mode 100644 index 4f498e6..0000000 --- a/examples/PromiseDemo/AServer/BServant.tars +++ /dev/null @@ -1,25 +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 Test -{ - -interface BServant -{ - int queryResult(string sIn, out string sOut); -}; - -}; diff --git a/examples/PromiseDemo/AServer/CMakeLists.txt b/examples/PromiseDemo/AServer/CMakeLists.txt deleted file mode 100644 index 431ac99..0000000 --- a/examples/PromiseDemo/AServer/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -build_tars_server("PromiseDemoAServer") \ No newline at end of file diff --git a/examples/PromiseDemo/AServer/CServant.tars b/examples/PromiseDemo/AServer/CServant.tars deleted file mode 100644 index d8dd8ad..0000000 --- a/examples/PromiseDemo/AServer/CServant.tars +++ /dev/null @@ -1,25 +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 Test -{ - -interface CServant -{ - int queryResult(string sIn, out string sOut); -}; - -}; diff --git a/examples/PromiseDemo/BServer/CMakeLists.txt b/examples/PromiseDemo/BServer/CMakeLists.txt deleted file mode 100644 index c1f2b64..0000000 --- a/examples/PromiseDemo/BServer/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -build_tars_server("PromiseDemoBServer") \ No newline at end of file diff --git a/examples/PromiseDemo/BServer/makefile b/examples/PromiseDemo/BServer/makefile deleted file mode 100644 index 156f400..0000000 --- a/examples/PromiseDemo/BServer/makefile +++ /dev/null @@ -1,11 +0,0 @@ -#----------------------------------------------------------------------- - -APP := Test -TARGET := BServer -CONFIG := -STRIP_FLAG:= N -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/examples/PromiseDemo/CMakeLists.txt b/examples/PromiseDemo/CMakeLists.txt deleted file mode 100644 index f506e86..0000000 --- a/examples/PromiseDemo/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ - -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 deleted file mode 100644 index 73f1745..0000000 --- a/examples/PromiseDemo/CServer/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -build_tars_server("PromiseDemoCServer") \ No newline at end of file diff --git a/examples/PromiseDemo/CServer/CServantImp.cpp b/examples/PromiseDemo/CServer/CServantImp.cpp deleted file mode 100644 index 7e55a9f..0000000 --- a/examples/PromiseDemo/CServer/CServantImp.cpp +++ /dev/null @@ -1,45 +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 "CServantImp.h" -#include "CServer.h" -#include "servant/Application.h" -#include "servant/Communicator.h" - -using namespace std; -using namespace tars; - -////////////////////////////////////////////////////// -void CServantImp::initialize() -{ - //initialize servant here: - //... -} -////////////////////////////////////////////////////// -void CServantImp::destroy() -{ -} -////////////////////////////////////////////////////// -tars::Int32 CServantImp::queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - sOut = "[sResult:"; - sOut += sIn; - sOut += "]"; - - return 0; -} - - diff --git a/examples/PromiseDemo/CServer/CServer.h b/examples/PromiseDemo/CServer/CServer.h deleted file mode 100644 index 79cb745..0000000 --- a/examples/PromiseDemo/CServer/CServer.h +++ /dev/null @@ -1,53 +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 _BServer_H_ -#define _BServer_H_ - -#include -#include "servant/Application.h" - -using namespace tars; - -/** - * * - * **/ -class CServer : public Application -{ -public: - /** - * - **/ - virtual ~CServer() {}; - - /** - * - **/ - virtual void initialize(); - - /** - * - **/ - virtual void destroyApp(); -protected: - bool cmdprofile(const string& command, const string& params, string& result); -}; - -extern CServer g_app; - -//////////////////////////////////////////// -#endif - diff --git a/examples/PromiseDemo/CServer/makefile b/examples/PromiseDemo/CServer/makefile deleted file mode 100644 index 303c3f3..0000000 --- a/examples/PromiseDemo/CServer/makefile +++ /dev/null @@ -1,11 +0,0 @@ -#----------------------------------------------------------------------- -APP := Test -TARGET := CServer -CONFIG := -STRIP_FLAG:= N - -INCLUDE += - -#----------------------------------------------------------------------- -include /usr/local/tars/cpp/makefile/makefile.tars -#----------------------------------------------------------------------- diff --git a/examples/PromiseDemo/Client/CMakeLists.txt b/examples/PromiseDemo/Client/CMakeLists.txt deleted file mode 100644 index d3caea6..0000000 --- a/examples/PromiseDemo/Client/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -build_tars_server("PromiseDemoClient") \ No newline at end of file diff --git a/examples/PromiseDemo/Client/main.cpp b/examples/PromiseDemo/Client/main.cpp deleted file mode 100644 index 83ca95f..0000000 --- a/examples/PromiseDemo/Client/main.cpp +++ /dev/null @@ -1,138 +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 "AServant.h" -#include "servant/Communicator.h" -#include "util/tc_thread_pool.h" -#include - -using namespace std; -using namespace Test; -using namespace tars; - -class Test1 -{ -public: - Test1(const string &sStr); - - ~Test1(); - - void queryResult(int iFlag, int iExecuteNum); - -private: - Communicator _comm; - AServantPrx prx; -}; - -Test1::Test1(const string &sStr) -{ - _comm.setProperty("locator", "tars.tarsregistry.QueryObj@ tcp -h 10.208.139.242 -p 17890 -t 10000 "); - _comm.stringToProxy(sStr, prx); -} - -Test1::~Test1() -{ - -} - -void Test1::queryResult(int iFlag, int iExecuteNum) -{ - string sIn(10,'a'); - string sOut(""); - - tars::Int32 count = 0; - unsigned long sum = 0; - - time_t _iTime=TC_TimeProvider::getInstance()->getNowMs(); - - for(int i=0; iqueryResultSerial(sIn, sOut); - } - else - { - ret = prx->queryResultParallel(sIn, sOut); - } - - if(ret == 0) - { - ++sum; - ++count; - if(count == iExecuteNum) - { - cout << "pthread id: " << pthread_self() << " | " << TC_TimeProvider::getInstance()->getNowMs() - _iTime << endl; - _iTime=TC_TimeProvider::getInstance()->getNowMs(); - count = 0; - } - } - } - catch(TC_Exception &e) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "exception: " << e.what() << endl; - } - catch(...) - { - cout << "pthread id: " << pthread_self() << "id: " << i << "unknown exception." << endl; - } - } - cout << "succ:" << sum << endl; - cout << "sOut:" << sOut << endl; -} - -int main(int argc,char ** argv) -{ - if(argc != 5) - { - cout << "usage: " << argv[0] << " sObj ThreadNum CallTimes CallMode" << endl; - return -1; - } - - string s = string(argv[1]); - - Test1 test1(s); - try - { - tars::Int32 threads = TC_Common::strto(string(argv[2])); - TC_ThreadPool tp; - tp.init(threads); - tp.start(); - tars::Int32 times = TC_Common::strto(string(argv[3])); - tars::Int32 callMode = TC_Common::strto(string(argv[4])); - - for(int i = 0; i +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace TestApp +{ + + /* callback of async proxy for client */ + class HelloPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~HelloPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testHello(tars::Int32 ret, const std::string& sRsp) + { throw std::runtime_error("callback_testHello() override incorrect."); } + virtual void callback_testHello_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testHello_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __Hello_all[]= + { + "test", + "testHello" + }; + pair r = equal_range(__Hello_all, __Hello_all+2, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Hello_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testHello_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sRsp; + _is.read(sRsp, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testHello(_ret, sRsp); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr HelloPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class HelloCoroPrxCallback: public HelloPrxCallback + { + public: + virtual ~HelloCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __Hello_all[]= + { + "test", + "testHello" + }; + + pair r = equal_range(__Hello_all, __Hello_all+2, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Hello_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testHello_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sRsp; + _is.read(sRsp, 2, true); + setResponseContext(msg->response->context); + + callback_testHello(_ret, sRsp); + + } + catch(std::exception &ex) + { + callback_testHello_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testHello_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr HelloCoroPrxCallbackPtr; + + /* proxy for client */ + class HelloProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(HelloPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(HelloCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testHello(const std::string & sReq,std::string &sRsp,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sReq, 1); + _os.write(sRsp, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testHello", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sRsp, 2, true); + return _ret; + } + + void async_testHello(HelloPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sReq, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback); + } + + void coro_testHello(HelloCoroPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sReq, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback); + } + + HelloProxy* tars_hash(int64_t key) + { + return (HelloProxy*)ServantProxy::tars_hash(key); + } + + HelloProxy* tars_consistent_hash(int64_t key) + { + return (HelloProxy*)ServantProxy::tars_consistent_hash(key); + } + + HelloProxy* tars_set_timeout(int msecond) + { + return (HelloProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "HelloProxy"; } + }; + typedef tars::TC_AutoPtr HelloPrx; + + /* servant for server */ + class Hello : public tars::Servant + { + public: + virtual ~Hello(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current) = 0; + static void async_response_testHello(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sRsp) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sRsp", sRsp); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sRsp, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __TestApp__Hello_all[]= + { + "test", + "testHello" + }; + + pair r = equal_range(__TestApp__Hello_all, __TestApp__Hello_all+2, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __TestApp__Hello_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sReq; + std::string sRsp; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sReq", sReq); + tarsAttr.getByDefault("sRsp", sRsp, sRsp); + } + else + { + _is.read(sReq, 1, true); + _is.read(sRsp, 2, false); + } + tars::Int32 _ret = testHello(sReq,sRsp, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sRsp", sRsp); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sRsp, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/PromiseDemo/CServer/CServant.tars b/examples/QuickStartDemo/HelloServer/Server/Hello.tars similarity index 88% rename from examples/PromiseDemo/CServer/CServant.tars rename to examples/QuickStartDemo/HelloServer/Server/Hello.tars index d8dd8ad..174c4f3 100644 --- a/examples/PromiseDemo/CServer/CServant.tars +++ b/examples/QuickStartDemo/HelloServer/Server/Hello.tars @@ -14,12 +14,13 @@ * specific language governing permissions and limitations under the License. */ -module Test +module TestApp { -interface CServant +interface Hello { - int queryResult(string sIn, out string sOut); + int test(); + int testHello(string sReq, out string sRsp); }; }; diff --git a/examples/PromiseDemo/BServer/BServantImp.cpp b/examples/QuickStartDemo/HelloServer/Server/HelloImp.cpp similarity index 71% rename from examples/PromiseDemo/BServer/BServantImp.cpp rename to examples/QuickStartDemo/HelloServer/Server/HelloImp.cpp index ffa2ecc..b0985dc 100644 --- a/examples/PromiseDemo/BServer/BServantImp.cpp +++ b/examples/QuickStartDemo/HelloServer/Server/HelloImp.cpp @@ -14,32 +14,29 @@ * specific language governing permissions and limitations under the License. */ -#include "BServantImp.h" -#include "BServer.h" +#include "HelloImp.h" #include "servant/Application.h" -#include "servant/Communicator.h" using namespace std; -using namespace tars; ////////////////////////////////////////////////////// -void BServantImp::initialize() +void HelloImp::initialize() { //initialize servant here: //... } -////////////////////////////////////////////////////// -void BServantImp::destroy() -{ -} -////////////////////////////////////////////////////// -tars::Int32 BServantImp::queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current) -{ - sOut = "[sResult:"; - sOut += sIn; - sOut += "]"; +////////////////////////////////////////////////////// +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 + ".CServantObj"); + addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".HelloObj"); } ///////////////////////////////////////////////////////////////// -void CServer::destroyApp() +void +HelloServer::destroyApp() { //destroy application here: //... } - ///////////////////////////////////////////////////////////////// -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { try { diff --git a/examples/PromiseDemo/BServer/BServer.h b/examples/QuickStartDemo/HelloServer/Server/HelloServer.h similarity index 82% rename from examples/PromiseDemo/BServer/BServer.h rename to examples/QuickStartDemo/HelloServer/Server/HelloServer.h index 7038cd8..f35886b 100644 --- a/examples/PromiseDemo/BServer/BServer.h +++ b/examples/QuickStartDemo/HelloServer/Server/HelloServer.h @@ -14,8 +14,8 @@ * specific language governing permissions and limitations under the License. */ -#ifndef _BServer_H_ -#define _BServer_H_ +#ifndef _HelloServer_H_ +#define _HelloServer_H_ #include #include "servant/Application.h" @@ -25,13 +25,13 @@ using namespace tars; /** * **/ -class BServer : public Application +class HelloServer : public Application { public: /** * **/ - virtual ~BServer() {}; + virtual ~HelloServer() {}; /** * @@ -42,11 +42,9 @@ public: * **/ virtual void destroyApp(); -protected: - bool cmdprofile(const string& command, const string& params, string& result); }; -extern BServer g_app; +extern HelloServer g_app; //////////////////////////////////////////// #endif diff --git a/examples/QuickStartDemo/HelloServer/Server/config.conf b/examples/QuickStartDemo/HelloServer/Server/config.conf new file mode 100755 index 0000000..30b96f3 --- /dev/null +++ b/examples/QuickStartDemo/HelloServer/Server/config.conf @@ -0,0 +1,70 @@ + + + + #tarsregistry locator + locator = tars.tarsregistry.QueryObj@tcp -h 127.0.0.1 -p 17890 + #max invoke timeout + max-invoke-timeout = 5000 + #refresh endpoint interval + refresh-endpoint-interval = 10000 + #stat obj + stat = tars.tarsstat.StatObj + #max send queue length limit + sendqueuelimit = 100000 + #async queue length limit + asyncqueuecap = 100000 + #async callback thread num + asyncthread = 3 + #net thread + netthread = 1 + #merge net and sync thread + mergenetasync = 0 + #module name + modulename = TestApp.HelloServer + + + + #not cout + closecout = 0 + #app name + app = TestApp + #server name + server = HelloServer + #path + basepath = ./ + datapath = ./ + #log path + logpath = ./ + #merge net and imp thread + mergenetimp = 0 + #local ip, for tarsnode +# local = tcp -h 127.0.0.1 -p 15001 -t 10000 + + #tarsnode +# node = ServerObj@tcp -h 127.0.0.1 -p 2345 -t 10000 + #config obj +# config = tars.tarsconfig.ConfigObj + #notify obj +# notify = tars.tarsconfig.NotifyObj + #log obj +# log = tars.tarslog.LogObj + + + #ip:port:timeout + endpoint = tcp -h 127.0.0.1 -p 8999 -t 10000 + #allow ip + allow = + #max connection num + maxconns = 4096 + #imp thread num + threads = 5 + #servant + servant = TestApp.HelloServer.HelloObj + #queue capacity + queuecap = 1000000 + #tars protocol + protocol = tars + + + + diff --git a/examples/PromiseDemo/AServer/makefile b/examples/QuickStartDemo/HelloServer/Server/makefile similarity index 79% rename from examples/PromiseDemo/AServer/makefile rename to examples/QuickStartDemo/HelloServer/Server/makefile index 9922db3..56961c4 100644 --- a/examples/PromiseDemo/AServer/makefile +++ b/examples/QuickStartDemo/HelloServer/Server/makefile @@ -1,12 +1,16 @@ #----------------------------------------------------------------------- -APP := Test -TARGET := AServer + +APP := TestApp +TARGET := HelloServer CONFIG := STRIP_FLAG:= N -INCLUDE += +INCLUDE += +LIB += #----------------------------------------------------------------------- + include /usr/local/tars/cpp/makefile/makefile.tars + #----------------------------------------------------------------------- diff --git a/examples/QuickStartDemo/ProxyServer/Server/Proxy.h b/examples/QuickStartDemo/ProxyServer/Server/Proxy.h new file mode 100644 index 0000000..4bce919 --- /dev/null +++ b/examples/QuickStartDemo/ProxyServer/Server/Proxy.h @@ -0,0 +1,471 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __PROXY_H_ +#define __PROXY_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace TestApp +{ + + /* callback of async proxy for client */ + class ProxyPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~ProxyPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testProxy(tars::Int32 ret, const std::string& sRsp) + { throw std::runtime_error("callback_testProxy() override incorrect."); } + virtual void callback_testProxy_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testProxy_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __Proxy_all[]= + { + "test", + "testProxy" + }; + pair r = equal_range(__Proxy_all, __Proxy_all+2, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Proxy_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testProxy_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sRsp; + _is.read(sRsp, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testProxy(_ret, sRsp); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr ProxyPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class ProxyCoroPrxCallback: public ProxyPrxCallback + { + public: + virtual ~ProxyCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __Proxy_all[]= + { + "test", + "testProxy" + }; + + pair r = equal_range(__Proxy_all, __Proxy_all+2, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Proxy_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testProxy_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sRsp; + _is.read(sRsp, 2, true); + setResponseContext(msg->response->context); + + callback_testProxy(_ret, sRsp); + + } + catch(std::exception &ex) + { + callback_testProxy_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testProxy_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr ProxyCoroPrxCallbackPtr; + + /* proxy for client */ + class ProxyProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(ProxyPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(ProxyCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testProxy(const std::string & sReq,std::string &sRsp,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sReq, 1); + _os.write(sRsp, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testProxy", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sRsp, 2, true); + return _ret; + } + + void async_testProxy(ProxyPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sReq, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testProxy", _os, context, _mStatus, callback); + } + + void coro_testProxy(ProxyCoroPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sReq, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testProxy", _os, context, _mStatus, callback); + } + + ProxyProxy* tars_hash(int64_t key) + { + return (ProxyProxy*)ServantProxy::tars_hash(key); + } + + ProxyProxy* tars_consistent_hash(int64_t key) + { + return (ProxyProxy*)ServantProxy::tars_consistent_hash(key); + } + + ProxyProxy* tars_set_timeout(int msecond) + { + return (ProxyProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "ProxyProxy"; } + }; + typedef tars::TC_AutoPtr ProxyPrx; + + /* servant for server */ + class Proxy : public tars::Servant + { + public: + virtual ~Proxy(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testProxy(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current) = 0; + static void async_response_testProxy(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sRsp) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sRsp", sRsp); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sRsp, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __TestApp__Proxy_all[]= + { + "test", + "testProxy" + }; + + pair r = equal_range(__TestApp__Proxy_all, __TestApp__Proxy_all+2, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __TestApp__Proxy_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sReq; + std::string sRsp; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sReq", sReq); + tarsAttr.getByDefault("sRsp", sRsp, sRsp); + } + else + { + _is.read(sReq, 1, true); + _is.read(sRsp, 2, false); + } + tars::Int32 _ret = testProxy(sReq,sRsp, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sRsp", sRsp); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sRsp, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/PromiseDemo/BServer/BServant.tars b/examples/QuickStartDemo/ProxyServer/Server/Proxy.tars similarity index 88% rename from examples/PromiseDemo/BServer/BServant.tars rename to examples/QuickStartDemo/ProxyServer/Server/Proxy.tars index 4f498e6..156faeb 100644 --- a/examples/PromiseDemo/BServer/BServant.tars +++ b/examples/QuickStartDemo/ProxyServer/Server/Proxy.tars @@ -14,12 +14,13 @@ * specific language governing permissions and limitations under the License. */ -module Test +module TestApp { -interface BServant +interface Proxy { - int queryResult(string sIn, out string sOut); + int test(); + int testProxy(string sReq, out string sRsp); }; }; diff --git a/examples/QuickStartDemo/ProxyServer/Server/ProxyImp.cpp b/examples/QuickStartDemo/ProxyServer/Server/ProxyImp.cpp new file mode 100644 index 0000000..5cc0fc4 --- /dev/null +++ b/examples/QuickStartDemo/ProxyServer/Server/ProxyImp.cpp @@ -0,0 +1,83 @@ +/** + * 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/PromiseDemo/BServer/BServantImp.h b/examples/QuickStartDemo/ProxyServer/Server/ProxyImp.h similarity index 64% rename from examples/PromiseDemo/BServer/BServantImp.h rename to examples/QuickStartDemo/ProxyServer/Server/ProxyImp.h index 1ca696d..9c924bd 100644 --- a/examples/PromiseDemo/BServer/BServantImp.h +++ b/examples/QuickStartDemo/ProxyServer/Server/ProxyImp.h @@ -14,26 +14,49 @@ * specific language governing permissions and limitations under the License. */ -#ifndef _BServantImp_H_ -#define _BServantImp_H_ +#ifndef __PROXY_IMP_H_ +#define __PROXY_IMP_H_ #include "servant/Application.h" -#include "BServant.h" -#include "promise/promise.h" +#include "Hello.h" +#include "Proxy.h" -using namespace Test; +using namespace TestApp; -class BServantImp : public Test::BServant +/** + * + * + */ +class ProxyImp : public TestApp::Proxy { public: + /** + * + */ + virtual ~ProxyImp() {} - virtual ~BServantImp() {} - + /** + * + */ virtual void initialize(); + /** + * + */ virtual void destroy(); - tars::Int32 queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); + /** + * + */ + virtual tars::Int32 test(tars::TarsCurrentPtr current); + + /** + * + */ + virtual tars::Int32 testProxy(const std::string& sReq, std::string &sRsp, tars::TarsCurrentPtr current); + +private: + HelloPrx _prx; }; ///////////////////////////////////////////////////// diff --git a/examples/PromiseDemo/BServer/BServer.cpp b/examples/QuickStartDemo/ProxyServer/Server/ProxyServer.cpp similarity index 83% rename from examples/PromiseDemo/BServer/BServer.cpp rename to examples/QuickStartDemo/ProxyServer/Server/ProxyServer.cpp index d203ec5..58def12 100644 --- a/examples/PromiseDemo/BServer/BServer.cpp +++ b/examples/QuickStartDemo/ProxyServer/Server/ProxyServer.cpp @@ -14,30 +14,30 @@ * specific language governing permissions and limitations under the License. */ -#include "BServer.h" -#include "BServantImp.h" +#include "ProxyServer.h" +#include "ProxyImp.h" using namespace std; -BServer g_app; +ProxyServer g_app; ///////////////////////////////////////////////////////////////// -void BServer::initialize() +void ProxyServer::initialize() { //initialize application here: - //... + addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".ProxyObj"); - addServant(ServerConfig::Application + "." + ServerConfig::ServerName + ".BServantObj"); } ///////////////////////////////////////////////////////////////// -void BServer::destroyApp() +void ProxyServer::destroyApp() { //destroy application here: //... } ///////////////////////////////////////////////////////////////// -int main(int argc, char* argv[]) +int +main(int argc, char* argv[]) { try { diff --git a/examples/PromiseDemo/AServer/AServer.h b/examples/QuickStartDemo/ProxyServer/Server/ProxyServer.h similarity index 83% rename from examples/PromiseDemo/AServer/AServer.h rename to examples/QuickStartDemo/ProxyServer/Server/ProxyServer.h index 9e6238a..2e6d3a4 100644 --- a/examples/PromiseDemo/AServer/AServer.h +++ b/examples/QuickStartDemo/ProxyServer/Server/ProxyServer.h @@ -14,8 +14,8 @@ * specific language governing permissions and limitations under the License. */ -#ifndef _BServer_H_ -#define _BServer_H_ +#ifndef __PROXY_SERVER_H_ +#define __PROXY_SERVER_H_ #include #include "servant/Application.h" @@ -25,13 +25,13 @@ using namespace tars; /** * **/ -class AServer : public Application +class ProxyServer : public Application { public: /** * **/ - virtual ~AServer() {}; + virtual ~ProxyServer() {}; /** * @@ -43,10 +43,10 @@ public: **/ virtual void destroyApp(); protected: - bool cmdprofile(const string& command, const string& params, string& result); + }; -extern AServer g_app; +extern ProxyServer g_app; //////////////////////////////////////////// #endif diff --git a/examples/PromiseDemo/Client/makefile b/examples/QuickStartDemo/ProxyServer/Server/makefile similarity index 66% rename from examples/PromiseDemo/Client/makefile rename to examples/QuickStartDemo/ProxyServer/Server/makefile index 0381ec5..7138201 100644 --- a/examples/PromiseDemo/Client/makefile +++ b/examples/QuickStartDemo/ProxyServer/Server/makefile @@ -1,13 +1,17 @@ #----------------------------------------------------------------------- -APP := Test -TARGET := myClientPromise + +APP := TestApp +TARGET := ProxyServer CONFIG := STRIP_FLAG:= N +TARS2CPP_FLAG:= -INCLUDE += +INCLUDE += +LIB += #----------------------------------------------------------------------- -include /home/tarsproto/Test/AServer/AServer.mk +include /home/tarsproto/TestApp/HelloServer/HelloServer.mk include /usr/local/tars/cpp/makefile/makefile.tars + #----------------------------------------------------------------------- diff --git a/examples/StressDemo/TarsStressServer/Stress.h b/examples/StressDemo/TarsStressServer/Stress.h new file mode 100644 index 0000000..5dabc31 --- /dev/null +++ b/examples/StressDemo/TarsStressServer/Stress.h @@ -0,0 +1,471 @@ +// ********************************************************************** +// This file was generated by a TARS parser! +// TARS version 1.4.0. +// ********************************************************************** + +#ifndef __STRESS_H_ +#define __STRESS_H_ + +#include +#include +#include +#include "tup/Tars.h" +#include "tup/TarsJson.h" +using namespace std; +#include "servant/ServantProxy.h" +#include "servant/Servant.h" + + +namespace Test +{ + + /* callback of async proxy for client */ + class StressPrxCallback: public tars::ServantProxyCallback + { + public: + virtual ~StressPrxCallback(){} + virtual void callback_test(tars::Int32 ret) + { throw std::runtime_error("callback_test() override incorrect."); } + virtual void callback_test_exception(tars::Int32 ret) + { throw std::runtime_error("callback_test_exception() override incorrect."); } + + virtual void callback_testStr(tars::Int32 ret, const std::string& sOut) + { throw std::runtime_error("callback_testStr() override incorrect."); } + virtual void callback_testStr_exception(tars::Int32 ret) + { throw std::runtime_error("callback_testStr_exception() override incorrect."); } + + public: + virtual const map & getResponseContext() const + { + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + if(!pCbtd->getContextValid()) + { + throw TC_Exception("cann't get response context"); + } + return pCbtd->getResponseContext(); + } + + public: + virtual int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __Stress_all[]= + { + "test", + "testStr" + }; + pair r = equal_range(__Stress_all, __Stress_all+2, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Stress_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_test(_ret); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testStr_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + CallbackThreadData * pCbtd = CallbackThreadData::getData(); + assert(pCbtd != NULL); + + pCbtd->setResponseContext(msg->response->context); + + callback_testStr(_ret, sOut); + + pCbtd->delResponseContext(); + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + }; + typedef tars::TC_AutoPtr StressPrxCallbackPtr; + + /* callback of coroutine async proxy for client */ + class StressCoroPrxCallback: public StressPrxCallback + { + public: + virtual ~StressCoroPrxCallback(){} + public: + virtual const map & getResponseContext() const { return _mRspContext; } + + virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } + + public: + int onDispatch(tars::ReqMessagePtr msg) + { + static ::std::string __Stress_all[]= + { + "test", + "testStr" + }; + + pair r = equal_range(__Stress_all, __Stress_all+2, string(msg->request.sFuncName)); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Stress_all) + { + case 0: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_test_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + setResponseContext(msg->response->context); + + callback_test(_ret); + + } + catch(std::exception &ex) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_test_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + if (msg->response->iRet != tars::TARSSERVERSUCCESS) + { + callback_testStr_exception(msg->response->iRet); + + return msg->response->iRet; + } + tars::TarsInputStream _is; + + _is.setBuffer(msg->response->sBuffer); + try + { + tars::Int32 _ret; + _is.read(_ret, 0, true); + + std::string sOut; + _is.read(sOut, 2, true); + setResponseContext(msg->response->context); + + callback_testStr(_ret, sOut); + + } + catch(std::exception &ex) + { + callback_testStr_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + catch(...) + { + callback_testStr_exception(tars::TARSCLIENTDECODEERR); + + return tars::TARSCLIENTDECODEERR; + } + + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + + protected: + map _mRspContext; + }; + typedef tars::TC_AutoPtr StressCoroPrxCallbackPtr; + + /* proxy for client */ + class StressProxy : public tars::ServantProxy + { + public: + typedef map TARS_CONTEXT; + tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + return _ret; + } + + void async_test(StressPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + void coro_test(StressCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); + } + + tars::Int32 testStr(const std::string & sIn,std::string &sOut,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + _os.write(sOut, 2); + std::map _mStatus; + shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testStr", _os, context, _mStatus); + if(pResponseContext) + { + pResponseContext->swap(rep->context); + } + + tars::TarsInputStream _is; + _is.setBuffer(rep->sBuffer); + tars::Int32 _ret; + _is.read(_ret, 0, true); + _is.read(sOut, 2, true); + return _ret; + } + + void async_testStr(StressPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback); + } + + void coro_testStr(StressCoroPrxCallbackPtr callback,const std::string &sIn,const map& context = TARS_CONTEXT()) + { + tars::TarsOutputStream _os; + _os.write(sIn, 1); + std::map _mStatus; + tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback); + } + + StressProxy* tars_hash(int64_t key) + { + return (StressProxy*)ServantProxy::tars_hash(key); + } + + StressProxy* tars_consistent_hash(int64_t key) + { + return (StressProxy*)ServantProxy::tars_consistent_hash(key); + } + + StressProxy* tars_set_timeout(int msecond) + { + return (StressProxy*)ServantProxy::tars_set_timeout(msecond); + } + + static const char* tars_prxname() { return "StressProxy"; } + }; + typedef tars::TC_AutoPtr StressPrx; + + /* servant for server */ + class Stress : public tars::Servant + { + public: + virtual ~Stress(){} + virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; + static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + virtual tars::Int32 testStr(const std::string & sIn,std::string &sOut,tars::TarsCurrentPtr current) = 0; + static void async_response_testStr(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sOut) + { + if (current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + + vector sTupResponseBuffer; + tarsAttr.encode(sTupResponseBuffer); + current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + + _os.write(sOut, 2); + + current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); + } + } + + public: + int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) + { + static ::std::string __Test__Stress_all[]= + { + "test", + "testStr" + }; + + pair r = equal_range(__Test__Stress_all, __Test__Stress_all+2, _current->getFuncName()); + if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; + switch(r.first - __Test__Stress_all) + { + case 0: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + } + else + { + } + tars::Int32 _ret = test(_current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + case 1: + { + tars::TarsInputStream _is; + _is.setBuffer(_current->getRequestBuffer()); + std::string sIn; + std::string sOut; + if (_current->getRequestVersion() == TUPVERSION) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.decode(_current->getRequestBuffer()); + tarsAttr.get("sIn", sIn); + tarsAttr.getByDefault("sOut", sOut, sOut); + } + else + { + _is.read(sIn, 1, true); + _is.read(sOut, 2, false); + } + tars::Int32 _ret = testStr(sIn,sOut, _current); + if(_current->isResponse()) + { + if (_current->getRequestVersion() == TUPVERSION ) + { + UniAttribute tarsAttr; + tarsAttr.setVersion(_current->getRequestVersion()); + tarsAttr.put("", _ret); + tarsAttr.put("sOut", sOut); + tarsAttr.encode(_sResponseBuffer); + } + else + { + tars::TarsOutputStream _os; + _os.write(_ret, 0); + _os.write(sOut, 2); + _os.swap(_sResponseBuffer); + } + } + return tars::TARSSERVERSUCCESS; + + } + } + return tars::TARSSERVERNOFUNCERR; + } + }; + + +} + + + +#endif diff --git a/examples/scripts/run-quick-start.sh b/examples/scripts/run-quick-start.sh new file mode 100644 index 0000000..89e30d0 --- /dev/null +++ b/examples/scripts/run-quick-start.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +echo "run-quick-start.sh" + +WORKDIR=$(pwd) + +echo ${WORKDIR} + +killall -9 QuickStartDemo + +${WORKDIR}/../bin/QuickStartDemo --config=${WORKDIR}/../../examples/QuickStartDemo/HelloServer/Server/config.conf & + +sleep 3 + +${WORKDIR}/../bin/QuickStartDemoClient + + diff --git a/servant/promise/exception_ptr.h b/servant/promise/exception_ptr.h deleted file mode 100644 index e4edff4..0000000 --- a/servant/promise/exception_ptr.h +++ /dev/null @@ -1,235 +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 __TARS_PROMISE_EXCEPTION_PTR_H_ -#define __TARS_PROMISE_EXCEPTION_PTR_H_ - -// #include "util/tc_shared_ptr.h" -#include "promise_exception.h" -#include -#include -#include -#include - -using namespace std; - -namespace tars -{ -namespace detail -{ - class ExceptionBase : public std::exception - { - public: - ExceptionBase() {} - - virtual ~ExceptionBase() throw() {} - - virtual ExceptionBase* clone() const { return new ExceptionBase(*this); } - - virtual void rethrow() const { throw *this; } - - virtual const char* what() const throw() { return "ExceptionBase"; } - - virtual int getErrCode() const { return -1; } - }; - - class ExceptionBaseImpl : public ExceptionBase - { - public: - ExceptionBaseImpl() {} - - ExceptionBaseImpl(const std::string& s) - : _sInfo(s) - , _error(-1) - {} - - ExceptionBaseImpl(const std::string& s, int err) - : _sInfo(s) - , _error(err) - {} - - virtual ~ExceptionBaseImpl() throw() {} - - virtual ExceptionBase* clone() const - { - return new ExceptionBaseImpl(*this); - } - - virtual void rethrow() const - { - throw *this; - } - - virtual const char* what() const throw() - { - return _sInfo.c_str(); - } - - virtual int getErrCode() const - { - return _error; - } - private: - std::string _sInfo; - int _error; - }; - -} // namespace detail - -typedef shared_ptr ExceptionPtr; - -class CurrentExceptionUnknownException : PromiseException -{ -private: - - const char *name() const - { - return "CurrentExceptionUnknownException"; - } -}; - -/** - * - */ -inline ExceptionPtr currentException() -{ - try - { - try - { - throw; - } - catch (detail::ExceptionBaseImpl& e) - { - return ExceptionPtr(e.clone()); - } - catch (detail::ExceptionBase& e) - { - return ExceptionPtr(e.clone()); - } - catch (std::domain_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::invalid_argument& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::length_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::out_of_range& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::range_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::overflow_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::underflow_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::logic_error& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_alloc& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_cast& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_typeid& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::bad_exception& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (PromiseException& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (std::exception& e) - { - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - } - catch (std::bad_alloc& e) - { - //return detail::ExceptionPtrStaticExceptionObject::e; - return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) )); - } - catch (...) - { - //return detail::ExceptionPtrStaticExceptionObject::e; - return ExceptionPtr(new detail::ExceptionBaseImpl(string("PromiseUnknownException") )); - } -} - - -inline detail::ExceptionBaseImpl enableCurrentException(const string& t) -{ - return detail::ExceptionBaseImpl(t); -} - -inline detail::ExceptionBaseImpl enableCurrentException(const string& t, int err) -{ - return detail::ExceptionBaseImpl(t, err); -} - -template -inline void throwException(const E& e) -{ - throw enableCurrentException(string(e.what(), strlen(e.what()))); -} - -inline ExceptionPtr copyException(const string& e) -{ - try - { - throw enableCurrentException(e); - } - catch (...) - { - return currentException(); - } -} - -inline ExceptionPtr copyException(const string& e, int err) -{ - try - { - throw enableCurrentException(e, err); - } - catch (...) - { - return currentException(); - } -} - -} // namespace promise - -#endif // __EXCEPTION_PTR_H__ diff --git a/servant/promise/promise.h b/servant/promise/promise.h deleted file mode 100644 index a37daeb..0000000 --- a/servant/promise/promise.h +++ /dev/null @@ -1,904 +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 __TARS_PROMISE_H_ -#define __TARS_PROMISE_H_ - -// #include "util/tc_callback.h" -#include "exception_ptr.h" -#include -#include -#include "promise_exception.h" -// #include "util/tc_bind.h" -// #include "util/tc_enable_shared_from_this.h" -// #include "util/detail/assert.h" -// #include "util/detail/tc_template_util.h" -#include "util/tc_monitor.h" - -// using namespace tars; - -namespace tars -{ - -class PromiseAlreadySatisfiedException : public PromiseException -{ -public: - - PromiseAlreadySatisfiedException(const char *filename, int line) - : PromiseException(filename, line) - {} - -protected: - - virtual const char *name() const - { - return "PromiseAlreadySatisfiedException"; - } -}; - -class FutureUninitializedException : public PromiseException -{ -public: - FutureUninitializedException(const char *filename, int line) - : PromiseException(filename, line) - {} - -protected: - virtual const char *name() const - { - return "FutureUninitializedException"; - } -}; - -template class Future; - -template struct is_future_type : std::false_type {}; -template struct is_future_type > : std::true_type {}; - -template class Promise; - -namespace detail -{ - template - struct resolved_type - { - typedef T type; - }; - - template - struct resolved_type > - { - typedef T type; - }; - - template - struct FutureTraits - { - // typedef TC_ScopedPtr storage_type; - typedef std::unique_ptr storage_type; - typedef const T& rvalue_source_type; - typedef const T& move_dest_type; - typedef T& dest_reference_type; - - static void init(storage_type& storage, rvalue_source_type t) - { - storage.reset(new T(t)); - } - - static void assign(dest_reference_type dest, const storage_type& storage) - { - dest = *storage; - } - }; - - template - struct FutureTraits - { - typedef T* storage_type; - typedef T& rvalue_source_type; - typedef T& move_dest_type; - typedef T*& dest_reference_type; - - static void init(storage_type& storage, T& t) - { - storage = &t; - } - - static void assign(dest_reference_type dest, const storage_type& storage) - { - dest = storage; - } - }; - - template <> - struct FutureTraits - { - typedef void rvalue_source_type; - typedef void move_dest_type; - }; - - template - class FutureObjectInterface - { - public: - typedef typename FutureTraits::rvalue_source_type rvalue_source_type; - typedef typename FutureTraits::move_dest_type move_dest_type; - typedef typename FutureTraits::dest_reference_type dest_reference_type; - typedef std::function&)> CallbackType; - - virtual ~FutureObjectInterface() {} - - virtual bool isDone() const = 0; - virtual bool hasValue() const = 0; - virtual bool hasException() const = 0; - virtual void setValue(rvalue_source_type v) = 0; - virtual void setException(const ExceptionPtr& e) = 0; - virtual move_dest_type get() const = 0; - virtual bool tryGet(dest_reference_type v) const = 0; - virtual void registerCallback(const CallbackType& callback) = 0; - }; - - template <> - class FutureObjectInterface - { - public: - typedef FutureTraits::move_dest_type move_dest_type; - typedef std::function&)> CallbackType; - - virtual ~FutureObjectInterface() {} - - virtual bool isDone() const = 0; - virtual bool hasValue() const = 0; - virtual bool hasException() const = 0; - virtual void set() = 0; - virtual void setException(const ExceptionPtr& e) = 0; - virtual move_dest_type get() const = 0; - virtual void registerCallback(const CallbackType& callback) = 0; - }; - - template - class PromptFutureObject : public FutureObjectInterface, - public std::enable_shared_from_this > - { - public: - typedef typename FutureObjectInterface::move_dest_type move_dest_type; - typedef typename FutureObjectInterface::rvalue_source_type rvalue_source_type; - typedef typename FutureObjectInterface::dest_reference_type dest_reference_type; - typedef typename FutureObjectInterface::CallbackType CallbackType; - - PromptFutureObject(rvalue_source_type v) - { - FutureTraits::init(m_value, v); - } - - PromptFutureObject(const ExceptionPtr& e) - : m_exception_ptr(e) - {} - - virtual ~PromptFutureObject() {} - - virtual bool isDone() const { return true; } - - virtual bool hasValue() const - { - if (m_exception_ptr) - return false; - return true; - } - - virtual bool hasException() const { return !!m_exception_ptr; } - - virtual void setValue(rvalue_source_type /*v*/) {} - virtual void setException(const ExceptionPtr& /*e*/) {} - - virtual move_dest_type get() const - { - assert(m_exception_ptr || m_value); - if (m_exception_ptr) - m_exception_ptr->rethrow(); - return *m_value; - } - - virtual bool tryGet(dest_reference_type v) const { FutureTraits::assign(v, m_value); return true; } - - virtual void registerCallback(const CallbackType& callback) - { - assert(callback); - try - { - callback(this->shared_from_this()); - } - catch (...) - { - } - } - - private: - typename FutureTraits::storage_type m_value; - ExceptionPtr m_exception_ptr; - }; - - template <> - class PromptFutureObject : public FutureObjectInterface, - public std::enable_shared_from_this > - { - public: - typedef FutureObjectInterface::move_dest_type move_dest_type; - typedef FutureObjectInterface::CallbackType CallbackType; - - PromptFutureObject() {} - - PromptFutureObject(const ExceptionPtr& e) - : m_exception_ptr(e) - {} - - virtual ~PromptFutureObject() {} - - virtual bool isDone() const { return true; } - - virtual bool hasValue() const - { - if (m_exception_ptr) - return false; - return true; - } - - virtual bool hasException() const { return (m_exception_ptr != NULL); } - - virtual void set() {} - virtual void setException(const ExceptionPtr& /*e*/) {} - - virtual move_dest_type get() const - { - if (m_exception_ptr) - m_exception_ptr->rethrow(); - } - - virtual void registerCallback(const CallbackType& callback) - { - assert(callback); - try - { - callback(this->shared_from_this()); - } - catch (...) - { - } - } - - private: - ExceptionPtr m_exception_ptr; - }; - - struct FutureObjectBase - { - FutureObjectBase() - : m_is_done(false) - {} - - virtual ~FutureObjectBase() {} - - bool isDone() const - { - TC_ThreadLock::Lock lock(m_monitor); - return m_is_done; - } - - bool hasValue() const - { - TC_ThreadLock::Lock lock(m_monitor); - return m_is_done && !m_exception_ptr; - } - - bool hasException() const - { - TC_ThreadLock::Lock lock(m_monitor); - return m_is_done && m_exception_ptr; - } - - void markFinishedWithException(const ExceptionPtr& e) - { - assert(e); - - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - throw PromiseAlreadySatisfiedException(__FILE__, __LINE__); - } - m_exception_ptr = e; - markFinishedInternal(); - } - - void markFinishedInternal() - { - m_is_done = true; - - m_monitor.notifyAll(); - } - - void wait() const - { - { - TC_ThreadLock::Lock lock(m_monitor); - while(!m_is_done) - { - m_monitor.wait(); - } - } - - if (m_exception_ptr) - m_exception_ptr->rethrow(); - } - - TC_ThreadLock m_monitor; - bool m_is_done; - ExceptionPtr m_exception_ptr; - }; - - template - class FutureObject : public FutureObjectInterface, - public std::enable_shared_from_this >, - private FutureObjectBase - { - public: - - typedef typename FutureObjectInterface::move_dest_type move_dest_type; - typedef typename FutureObjectInterface::rvalue_source_type rvalue_source_type; - typedef typename FutureObjectInterface::CallbackType CallbackType; - typedef typename FutureObjectInterface::dest_reference_type dest_reference_type; - - FutureObject() {} - - virtual ~FutureObject() {} - - virtual bool isDone() const - { - return FutureObjectBase::isDone(); - } - - virtual bool hasValue() const - { - return FutureObjectBase::hasValue(); - } - - virtual bool hasException() const - { - return FutureObjectBase::hasException(); - } - - virtual move_dest_type get() const - { - wait(); - - assert(m_value); - return *m_value; - } - - virtual bool tryGet(dest_reference_type v) const - { - if (!isDone()) - return false; - if (m_exception_ptr) - m_exception_ptr->rethrow(); - FutureTraits::assign(v, m_value); - return true; - } - - virtual void setException(const ExceptionPtr& e) - { - markFinishedWithException(e); - doPendingCallbacks(); - } - - virtual void setValue(rvalue_source_type t) - { - { - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - throwException(PromiseAlreadySatisfiedException(__FILE__, __LINE__)); - } - m_value.reset(new T(t)); - markFinishedInternal(); - } - doPendingCallbacks(); - } - - virtual void registerCallback(const CallbackType& callback) - { - assert(callback); - - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - lock.release(); - try - { - callback(this->shared_from_this()); - } - catch (...) - { - } - } - else - { - m_pending_callbacks.push_back(callback); - } - } - - private: - - void doPendingCallbacks() - { - std::list callbacks; - { - TC_ThreadLock::Lock lock(m_monitor); - callbacks.swap(m_pending_callbacks); - } - - for (typename std::list::const_iterator it = callbacks.begin(), - end = callbacks.end(); - it != end; ++it) - { - try - { - (*it)(this->shared_from_this()); - } - catch (...) - { - - } - } - } - - typename FutureTraits::storage_type m_value; - std::list m_pending_callbacks; - }; - - template <> - class FutureObject : public FutureObjectInterface, - public std::enable_shared_from_this >, - private FutureObjectBase - { - public: - - using typename FutureObjectInterface::CallbackType; - - FutureObject() {} - - virtual ~FutureObject() {} - - virtual bool isDone() const - { - return FutureObjectBase::isDone(); - } - - virtual bool hasValue() const - { - return FutureObjectBase::hasValue(); - } - - virtual bool hasException() const - { - return FutureObjectBase::hasException(); - } - - virtual void get() const - { - wait(); - } - - virtual void set() - { - { - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - throwException(PromiseAlreadySatisfiedException(__FILE__, __LINE__)); - } - markFinishedInternal(); - } - - doPendingCallbacks(); - } - - virtual void setException(const ExceptionPtr& e) - { - markFinishedWithException(e); - doPendingCallbacks(); - } - - void registerCallback(const CallbackType& callback) - { - assert(callback); - - TC_ThreadLock::Lock lock(m_monitor); - if (m_is_done) - { - lock.release(); - try - { - callback(this->shared_from_this()); - } - catch (...) - { - - } - return; - } - m_pending_callbacks.push_back(callback); - } - - private: - void doPendingCallbacks() - { - std::list callbacks; - { - TC_ThreadLock::Lock lock(m_monitor); - callbacks.swap(m_pending_callbacks); - } - - for (std::list::const_iterator it = callbacks.begin(), - end = callbacks.end(); - it != end; ++it) - { - (*it)(this->shared_from_this()); - } - } - - std::list m_pending_callbacks; - }; - - template class ForwardValue; - - template - class SequentialCallback - { - private: - typedef typename detail::resolved_type::type value_type; - typedef std::shared_ptr > FuturePtr; - public: - SequentialCallback(const std::function&)>& callback, - const Promise& promise) - : m_callback(callback) - , m_promise(promise) - {} - - template - typename std::enable_if::value >::type run(const FuturePtr& future) - { - try - { - m_callback(future); - m_promise.set(); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - template - typename std::enable_if::value && !is_future_type::value>::type - run(const FuturePtr& future) - { - try - { - m_promise.setValue(m_callback(future)); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - template - typename std::enable_if::value >::type run(const FuturePtr& future) - { - try - { - m_callback(future).then(TC_Bind(&ForwardValue::template run, - tc_owned(new ForwardValue(m_promise)))); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - private: - - std::function&)> m_callback; - Promise m_promise; - }; - - template - class ForwardValue - { - public: - ForwardValue(const Promise& promise) - : m_promise(promise) - {} - - template - typename std::enable_if::value >::type run(const Future& future) - { - try - { - future.get(); - m_promise.set(); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - template - typename std::enable_if::value >::type run(const Future& future) - { - try - { - m_promise.setValue(future.get()); - } - catch (...) - { - m_promise.setException(currentException()); - } - } - - private: - Promise m_promise; - }; - - template - class FutureBase - { - private: - typedef typename detail::FutureTraits::move_dest_type move_dest_type; - typedef typename detail::FutureTraits::rvalue_source_type rvalue_source_type; - public: - typedef bool (FutureBase::*unspecified_bool_type)() const; - - FutureBase() {} - - FutureBase(const ExceptionPtr& e) - : m_future(std::shared_ptr >(new detail::PromptFutureObject(e))) - {} - - virtual ~FutureBase() {} - - move_dest_type get() const - { - if (!m_future) - { - throwException(FutureUninitializedException(__FILE__, __LINE__)); - } - return m_future->get(); - } - - bool isDone() const - { - if (!m_future) - return false; - return m_future && m_future->isDone(); - } - - bool hasValue() const - { - if (!m_future) - return false; - return m_future && m_future->hasValue(); - } - - bool hasException() const - { - if (!m_future) - return false; - return m_future && m_future->hasException(); - } - - operator unspecified_bool_type() const - { - return m_future ? &FutureBase::isDone : NULL; - } - - protected: - - typedef std::shared_ptr > FuturePtr; - - FutureBase(const FuturePtr& future) - : m_future(future) - {} - - FuturePtr m_future; - }; - -} // namespace detail - -template -class Future : public detail::FutureBase -{ -public: - Future() {} - - explicit Future(typename detail::FutureTraits::rvalue_source_type t) - : detail::FutureBase(std::shared_ptr >(new detail::PromptFutureObject(t))) - {} - - Future(const ExceptionPtr& e) - : detail::FutureBase(e) - {} - - virtual ~Future() {} - - template - Future then(const std::function&)>& callback) const - { - // typedef typename detail::resolved_type::type value_type; - - if (!this->m_future) - { - throwException(FutureUninitializedException(__FILE__, __LINE__)); - } - - 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) - : detail::FutureBase(future) - {} - - template friend class detail::SequentialCallback; - friend class Promise; -}; - -template <> -class Future : public detail::FutureBase -{ -public: - Future() {} - - Future(const ExceptionPtr& e) - : detail::FutureBase(e) - {} - - virtual ~Future() {} - - 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 detail::FutureBase::FuturePtr& future) - : detail::FutureBase(future) - {} - - friend Future makeFuture(); - - template friend class detail::SequentialCallback; - friend class Promise; -}; - -template -// typename std::disable_if, Future >::type -typename std::enable_if::value, Future >::type -makeFuture(typename detail::FutureTraits::rvalue_source_type t) -{ - return Future(t); -} - -inline Future makeFuture() -{ - return Future(std::shared_ptr >(new detail::PromptFutureObject())); -} - -template -class Promise -{ -public: - - Promise() - : m_future(std::shared_ptr >(new detail::FutureObject())) - {} - - void setValue(typename detail::FutureTraits::rvalue_source_type v) - { - assert(m_future); - m_future->setValue(v); - } - - void setException(const ExceptionPtr& e) - { - m_future->setException(e); - } - - Future getFuture() const - { - return Future(m_future); - } - -private: - std::shared_ptr > m_future; -}; - -template <> -class Promise -{ -private: - std::shared_ptr > m_future; -public: - - Promise() - : m_future(std::shared_ptr >(new detail::FutureObject())) - {} - - void set() - { - assert(m_future); - m_future->set(); - } - - void setException(const ExceptionPtr& e) - { - m_future->setException(e); - } - - Future getFuture() const - { - return Future(m_future); - } -}; - -} // namespace promise - -#endif // __TARS_PROMISE_H_ diff --git a/servant/promise/promise_exception.h b/servant/promise/promise_exception.h deleted file mode 100644 index 7313219..0000000 --- a/servant/promise/promise_exception.h +++ /dev/null @@ -1,200 +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 __TARS_PROMISE_EXCEPTION_H_ -#define __TARS_PROMISE_EXCEPTION_H_ - -#include -#include -#include -#include -#include -// #include "util/tc_shared_ptr.h" -// #include "util/detail/tc_template_util.h" - -namespace tars -{ - -class PromiseException : public std::exception -{ -public: - - PromiseException() - : _filename(NULL) - , _line(0) - , _sep(':') - {} - - PromiseException(const char *filename, int line) - : _filename(filename) - , _line(line) - , _sep(':') - {} - - virtual ~PromiseException() throw() {} - - virtual const char *what() const throw() - { - try - { - if (!_what.unique()) - _what.reset(new std::string(*_what)); - - if (!_what || _what->empty()) - { - std::stringstream ss; - ss << name() << " thrown"; - - if (_filename && _line > 0) - ss << " at " << _filename << ':' << _line; - - if (!_what) - _what.reset(new std::string()); - - const std::string& s = getErrorInfo(); - if (!s.empty()) - { - ss << _sep << ' ' << s; - _sep = ';'; - } - ss.str().swap(*_what); - } - - if (!_data.unique()) - _data.reset(new std::string(*_data)); - - if (_data && !_data->empty()) - { - *_what += _sep; - _sep = ';'; - *_what += ' '; - _what->append(*_data); - _data.reset(); - } - return _what->c_str(); - } - catch (...) - { - - } - return ""; - } - -private: - - virtual const char *name() const - { - return "PromiseException"; - } - - virtual std::string getErrorInfo() const - { - return ""; - } - - const char *_filename; - int _line; - - mutable std::shared_ptr _what; - mutable std::shared_ptr _data; - mutable char _sep; - - template - void injectErrorInfo(const T& t) const; - - template - friend typename std::enable_if::value, const Exception&>::type - operator<<(const Exception& e, const T& t) - { - e.injectErrorInfo(t); - return e; - } -}; - -template -void PromiseException::injectErrorInfo(const T& t) const -{ - std::stringstream ss; - ss << t; - - if (!_data.unique()) - _data.reset(new std::string(*_data)); - - if (!_data) - { - _data.reset(new std::string(ss.str())); - } - else - { - (*_data) += ss.str(); - } -} - -namespace detail -{ - inline std::string errnoToString(int errorno) - { - return strerror(errorno); - } - -} // namespace detail - -class SyscallException : public PromiseException -{ -public: - - SyscallException(const char *filename, int line, int err) - : PromiseException(filename, line) - , _errno(err) - {} - - int error() const { return _errno; } - -protected: - - virtual const char *name() const - { - return "SyscallException"; - } - - virtual std::string getErrorInfo() const - { - return detail::errnoToString(_errno); - } - -private: - - int _errno; -}; - -class AssertionFailureException : public PromiseException -{ -public: - AssertionFailureException(const char *filename, int line) - : PromiseException(filename, line) - { - } - -private: - virtual const char *name() const - { - return "AssertionFailureException"; - } -}; - -} // namespace promise - -#endif // __TARS_PROMISE_EXCEPTION_H__ diff --git a/servant/promise/tuple.h b/servant/promise/tuple.h deleted file mode 100644 index 684433b..0000000 --- a/servant/promise/tuple.h +++ /dev/null @@ -1,591 +0,0 @@ -#ifndef __TARS_PROMISE_TUPLE_H_ -#define __TARS_PROMISE_TUPLE_H_ - -// #include "util/detail/tc_assert.h" -#include - -namespace tars -{ - namespace detail - { - struct null_type {}; - - inline bool operator==(const null_type&, const null_type&) - { return true; } - - inline bool operator!=(const null_type&, const null_type&) - { return false; } - - inline bool operator<(const null_type&, const null_type&) - { return false; } - - // A helper function to make a const temporary. - inline const null_type make_null() { return null_type(); } - - template - struct Cons - { - typedef HT head_type; - typedef TT tail_type; - - Cons() - : head() - , tail() - {} - - Cons(const head_type& h, const tail_type& t) - : head(h) - , tail(t) - {} - - template - Cons(T0& t0, T1& t1, T2& t2, T3& t3, T4& t4, - T5& t5, T6& t6, T7& t7, T8& t8, T9& t9) - : head(t0) - , tail(t1, t2, t3, t4, t5, t6, t7, t8, t9, make_null()) - {} - - template - Cons(const Cons& o) - : head(o.head) - , tail(o.tail) - {} - - template - Cons& operator=(const Cons& o) - { - head = o.head; - tail = o.tail; - return *this; - } - - void swap(const Cons& o) - { - std::swap(head, o.head); - std::swap(tail, o.tail); - } - - // Because the C++ standard prohibits the function template partial - // specialization, we have to define `operator==()' and `operator!=' as the - // member function. - - bool operator==(const Cons& o) const - { - return head == o.head && tail == o.tail; - } - - bool operator!=(const Cons& o) const - { - return !(operator==(o)); - } - - bool operator<(const Cons& o) const - { - return (head < o.head) || (!(o.head < head) && tail < o.tail); - } - - head_type head; - tail_type tail; - }; - - template - struct Cons - { - typedef HT head_type; - typedef null_type tail_type; - - Cons() - : head() - {} - - template - Cons(T0& t0, const null_type&, const null_type&, const null_type&, const null_type&, - const null_type&, const null_type&, const null_type&, const null_type&, const null_type&) - : head(t0) - {} - - template - Cons& operator=(const Cons& o) - { - head = o.head; - return *this; - } - - void swap(Cons& o) - { - std::swap(head, o.head); - } - - // Because the C++ standard prohibits the function template partial - // specialization, we have to define `operator==()' and `operator!=' as the - // member function. - - bool operator==(const Cons& o) const - { - return head == o.head; - } - - bool operator!=(const Cons& o) const - { - return !(operator==(o)); - } - - bool operator<(const Cons& o) const - { - return head < o.head; - } - - head_type head; - }; - - template - struct map_tuple_to_cons - { - typedef Cons::type> type; - }; - - template <> - struct map_tuple_to_cons - { - typedef null_type type; - }; - - // Gets the N-th tail of a tuple. - template - struct drop_front - { - typedef typename drop_front::type::tail_type type; - static const type& get_tail(const Tuple& tuple) - { - return drop_front::get_tail(tuple).tail; - } - }; - - template - struct drop_front<0, Tuple> - { - typedef Tuple type; - static const type& get_tail(const Tuple& tuple) - { - return tuple; - } - }; - - // Gets the type of the N-th element of a tuple. - template - struct element - { - typedef typename drop_front::type::head_type type; - }; - - // Gets the length of a tuple. - template - struct length - { - static const int value = length::value + 1; - }; - - template <> - struct length - { - static const int value = 0; - }; - - // Helper classes to print Cons. - template - struct print_cons_helper - { - static std::ostream& print(std::ostream& o, const Cons& cons) - { - o << cons.head; - o << ", "; - print_cons_helper::print(o, cons.tail); - return o; - } - }; - - template - struct print_cons_helper - { - static std::ostream& print(std::ostream& o, const Cons& cons) - { - o << cons.head; - return o; - } - }; - - template - inline std::ostream& print_cons(std::ostream& o, const Cons& cons) - { - return print_cons_helper::print(o, cons); - } - - inline std::ostream& print_cons(std::ostream& o, const null_type&) - { - return o; - } - - } // namespace detail - - template - class Tuple - { - private: - typedef typename detail::map_tuple_to_cons::type tuple_impl_type; - - public: - - // Returns the type of the N-th element of this tuple. - template - struct element - { - typedef typename detail::element::type type; - }; - - template - struct access_traits - { - typedef const T& parameter_type; - typedef const T& const_type; - typedef T& non_const_type; - }; - - template - struct access_traits - { - typedef T& parameter_type; - typedef T& const_type; - typedef T& non_const_type; - }; - - // Number of elements of this tuple. - enum - { - LENGTH = detail::length::value - }; - - Tuple() {} - - Tuple(typename access_traits::parameter_type t0) - : m_tuple_impl(t0, detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1) - : m_tuple_impl(t0, t1, detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2) - : m_tuple_impl(t0, t1, t2, detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3) - : m_tuple_impl(t0, t1, t2, t3, detail::make_null(), detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4) - : m_tuple_impl(t0, t1, t2, t3, t4, detail::make_null(), detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, detail::make_null(), detail::make_null(), - detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, detail::make_null(), detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6, - typename access_traits::parameter_type t7) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, detail::make_null(), detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6, - typename access_traits::parameter_type t7, - typename access_traits::parameter_type t8) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, t8, detail::make_null()) - {} - - Tuple(typename access_traits::parameter_type t0, - typename access_traits::parameter_type t1, - typename access_traits::parameter_type t2, - typename access_traits::parameter_type t3, - typename access_traits::parameter_type t4, - typename access_traits::parameter_type t5, - typename access_traits::parameter_type t6, - typename access_traits::parameter_type t7, - typename access_traits::parameter_type t8, - typename access_traits::parameter_type t9) - : m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9) - {} - - template - Tuple(const Tuple& o) - : m_tuple_impl(o.m_tuple_impl) - {} - - template - Tuple& operator=(const Tuple& o) - { - m_tuple_impl = o.m_tuple_impl; - return *this; - } - - template - typename access_traits::type>::const_type - get() const - { - TC_STATIC_ASSERT(N < LENGTH, index_out_of_bounds); - return detail::drop_front::get_tail(m_tuple_impl).head; - } - - template - typename access_traits::type>::non_const_type - get() - { - TC_STATIC_ASSERT(N < LENGTH, index_out_of_bounds); - typedef detail::drop_front impl; - typedef typename impl::type cons_element; - return const_cast(impl::get_tail(m_tuple_impl)).head; - } - - void swap(Tuple& o) - { - m_tuple_impl.swap(o.m_tuple_impl); - } - - private: - tuple_impl_type m_tuple_impl; - - template - friend class Tuple; - - friend bool operator==(const Tuple& lhs, const Tuple& rhs) - { - return lhs.m_tuple_impl == rhs.m_tuple_impl; - } - - friend bool operator!=(const Tuple& lhs, const Tuple& rhs) - { - return lhs.m_tuple_impl != rhs.m_tuple_impl; - } - - friend bool operator<(const Tuple& lhs, const Tuple& rhs) - { - return lhs.m_tuple_impl < rhs.m_tuple_impl; - } - - friend std::ostream& operator<<(std::ostream& o, const Tuple& t) - { - o << '('; - detail::print_cons(o, t.m_tuple_impl); - o << ')'; - return o; - } - }; - - namespace detail - { - // Call traits for make_tuple - // - // Conversions: - // T -> T - // T& -> T - // const T& -> T - // T[] -> const T* - // const T[] -> const T* - - template - struct make_tuple_traits - { - typedef T type; - }; - - template - struct make_tuple_traits - { - typedef T type; - }; - - // To produce compilation error intentionally by referencing an non-exsitent - // member of this class. - template - class generate_error {}; - - template - struct make_tuple_traits - { - typedef const T* type; - }; - - template - struct make_tuple_traits - { - typedef const T* type; - }; - - template - struct make_tuple_mapper - { - typedef Tuple::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type, - typename make_tuple_traits::type> type; - }; - - } // namespace detail - - // make_tuple function templates for arities 0~10 - - inline Tuple<> make_tuple() - { return Tuple<>(); } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6, const T7& t7) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6, const T7& t7, const T8& t8) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7, t8); - } - - template - inline typename detail::make_tuple_mapper::type - make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, - const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9) - { - typedef typename detail::make_tuple_mapper::type tuple_type; - return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9); - } - -} // namespace promise - -#endif // __TARS_PROMISE_TUPLE_H__ diff --git a/servant/promise/when_all.h b/servant/promise/when_all.h deleted file mode 100644 index 9efed10..0000000 --- a/servant/promise/when_all.h +++ /dev/null @@ -1,334 +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 __TARS_PROMISE_WHEN_ALL_H_ -#define __TARS_PROMISE_WHEN_ALL_H_ - -#include "promise.h" -#include -// #include "std::tuple.h" -#include "util/tc_monitor.h" -// #include "util/tc_shared_ptr.h" -// #include "util/detail/assert.h" -namespace tars -{ - namespace detail - { - // Callback implementation for parallel composition of futures. - template - struct ParallelAllCallbackBase - { - public: - typedef Promise PromiseAll; - - ParallelAllCallbackBase(const PromiseAll& promise_all, - size_t number_of_non_satisfied) - : m_number_of_non_satisfied(number_of_non_satisfied) - , m_promise_all(promise_all) - { - } - - virtual ~ParallelAllCallbackBase() - { - } - - size_t m_number_of_non_satisfied; - PromiseAll m_promise_all; - FutureAllValueType m_waiting_futures; - TC_ThreadLock m_monitor; - - private: - ParallelAllCallbackBase(const ParallelAllCallbackBase&); - void operator=(const ParallelAllCallbackBase&); - - }; - - // FutureAllValueType is supposed to be of type tars::std::tuple or - // std::vector. - // - // This implementation is for tars::std::tuple<>. - template - class ParallelAllCallback : private ParallelAllCallbackBase - { - public: - typedef typename ParallelAllCallbackBase::PromiseAll PromiseAll; - - ParallelAllCallback(const PromiseAll& promise_all) - : ParallelAllCallbackBase(promise_all, FutureAllValueType::LENGTH) - {} - - virtual ~ParallelAllCallback() {} - - template - void on_future(const typename FutureAllValueType::template element::type& future) - { - // Note: this future must be recorded no matter what exception is thrown. - TC_ThreadLock::Lock lock(this->m_monitor); - this->m_waiting_futures.template get() = future; - assert(this->m_number_of_non_satisfied > 0); - if (--this->m_number_of_non_satisfied == 0) - { - lock.release(); - try - { - this->m_promise_all.setValue(this->m_waiting_futures); - } - catch (...) - { - this->m_promise_all.setException(currentException()); - } - return; - } - } - - private: - ParallelAllCallback(const ParallelAllCallback&); - void operator=(const ParallelAllCallback&); - - }; - - // This implementation is specialized for std::vector >. - template - class ParallelAllCallback > : private ParallelAllCallbackBase > - { - public: - typedef typename ParallelAllCallbackBase >::PromiseAll PromiseAll; - - ParallelAllCallback(const PromiseAll& promise_all, - size_t number_of_non_satisfied) - : ParallelAllCallbackBase >(promise_all, number_of_non_satisfied) - { - this->m_waiting_futures.resize(number_of_non_satisfied); - } - - virtual ~ParallelAllCallback() {} - - void on_future(const FutureType& future, size_t idx) - { - // Note: this future must be recorded no matter what exception is thrown. - assert(idx < this->m_waiting_futures.size()); - - TC_ThreadLock::Lock lock(this->m_monitor); - this->m_waiting_futures[idx] = future; - assert(this->m_number_of_non_satisfied > 0); - if (--this->m_number_of_non_satisfied == 0) - { - lock.release(); - try - { - this->m_promise_all.setValue(this->m_waiting_futures); - } - catch (...) - { - this->m_promise_all.setException(tars::currentException()); - } - return; - } - } - - private: - ParallelAllCallback(const ParallelAllCallback&); - void operator=(const ParallelAllCallback&); - - }; - - // A helper class to notify ParallelAllCallback > which future is done. - template - class VectorParallelCallback - { - public: - VectorParallelCallback(const std::shared_ptr > > >& cb, size_t idx) - : m_parallel_callback(cb), - m_idx(idx) - {} - - void on_future(const Future& future) - { - m_parallel_callback->on_future(future, m_idx); - } - - private: - std::shared_ptr > > > m_parallel_callback; - size_t m_idx; - }; - - } // namespace detail - - /** - * Parallelly composite two or more futues. Only when all futures are satisfied is - * the composited future satisfied. - */ - template - Future, Future > > - whenAll(const Future& future1, const Future& future2) - { - typedef std::tuple, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3) - { - typedef std::tuple, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4) - { - typedef std::tuple, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5) - { - typedef std::tuple, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future, Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5, const Future& future6) - { - typedef std::tuple, Future, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future, Future, Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5, const Future& future6, const Future& future7) - { - typedef std::tuple, Future, Future, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future, Future, Future, Future, Future, Future, Future, Future > > - whenAll(const Future& future1, const Future& future2, const Future& future3, const Future& future4, - const Future& future5, const Future& future6, const Future& future7, const Future& future8) - { - typedef std::tuple, Future, Future, Future, Future, Future, Future, Future > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - std::shared_ptr future_callback(new WhenAllCallback(promise_all)); - 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(); - } - - template - Future > > - whenAll(InputIterator first, InputIterator last) - { - typedef std::vector > FutureAllValueType; - typedef Promise PromiseAll; - typedef detail::ParallelAllCallback WhenAllCallback; - - PromiseAll promise_all; - if (first == last) - { - promise_all.setValue(FutureAllValueType()); - } - else - { - std::shared_ptr when_all_callback(new WhenAllCallback(promise_all, std::distance(first, last))); - size_t i; - - for (i = 0; first != last; ++first, ++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(); - } - -} // namespace promise - -#endif // __TARS_PROMISE_WHEN_ALL_H__ diff --git a/tools/tars2cpp/main.cpp b/tools/tars2cpp/main.cpp index 73e05d3..101a780 100644 --- a/tools/tars2cpp/main.cpp +++ b/tools/tars2cpp/main.cpp @@ -29,7 +29,7 @@ void usage() cout << " --unjson not json interface" << endl; cout << " --os only create struct(not create interface) " << endl; cout << " --include=\"dir1;dir2;dir3\" set search path of tars protocol" << endl; - cout << " --unknown create unkown field" << endl; + // cout << " --unknown create unkown field" << endl; cout << " --tarsMaster create get registry info interface" << endl; cout << " --currentPriority use current path first." << endl; cout << " tars2cpp support type: bool byte short int long float double vector map" << endl; @@ -123,7 +123,7 @@ int main(int argc, char* argv[]) g_parse->setHeader(option.getValue("header")); g_parse->setCurrentPriority(option.hasParam("currentPriority")); - t2c.setUnknownField(option.hasParam("unknown")); + // t2c.setUnknownField(option.hasParam("unknown")); for(size_t i = 0; i < vTars.size(); i++) { diff --git a/tools/tars2cpp/tars2cpp.cpp b/tools/tars2cpp/tars2cpp.cpp index ec9e469..572d1c0 100644 --- a/tools/tars2cpp/tars2cpp.cpp +++ b/tools/tars2cpp/tars2cpp.cpp @@ -31,7 +31,7 @@ Tars2Cpp::Tars2Cpp() , _onlyStruct(false) , _bJsonSupport(true) , _namespace("tars") -, _unknownField(false) +// , _unknownField(false) , _tarsMaster(false) { @@ -230,18 +230,18 @@ string Tars2Cpp::readFrom(const TypeIdPtr& pPtr, bool bIsRequire) const -string Tars2Cpp::readUnknown(const TypeIdPtr& pPtr) const -{ - ostringstream s; - s << TAB << "_is.readUnknown(sUnknownField, " << pPtr->getTag() << ");" << endl; - return s.str(); -} -string Tars2Cpp::writeUnknown() const -{ - ostringstream s; - s << TAB << "_os.writeUnknown(sUnknownField);" << endl; - return s.str(); -} +// string Tars2Cpp::readUnknown(const TypeIdPtr& pPtr) const +// { +// ostringstream s; +// s << TAB << "_is.readUnknown(sUnknownField, " << pPtr->getTag() << ");" << endl; +// return s.str(); +// } +// string Tars2Cpp::writeUnknown() const +// { +// ostringstream s; +// s << TAB << "_os.writeUnknown(sUnknownField);" << endl; +// return s.str(); +// } string Tars2Cpp::display(const TypeIdPtr& pPtr) const { @@ -568,14 +568,14 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con } } - if (b) - { - if (_unknownField) - { - s << ",sUnknownField(\"\")"; - } - s << endl; - } + // if (b) + // { + // if (_unknownField) + // { + // s << ",sUnknownField(\"\")"; + // } + // s << endl; + // } s << TAB << "{" << endl; INC_TAB; @@ -656,10 +656,10 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con { s << writeTo(member[j]); } - if (_unknownField) - { - s << writeUnknown(); - } + // if (_unknownField) + // { + // s << writeUnknown(); + // } DEL_TAB; s << TAB << "}" << endl; @@ -673,10 +673,10 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con { s << readFrom(member[j]); } - if (_unknownField) - { - s << readUnknown(member[member.size() - 1]); - } + // if (_unknownField) + // { + // s << readUnknown(member[member.size() - 1]); + // } DEL_TAB; s << TAB << "}" << endl; @@ -771,10 +771,10 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con s << TAB << tostr(member[j]->getTypePtr()) << " " << member[j]->getId() << toStrSuffix(member[j]) << ";" << endl; } - if (_unknownField) - { - s << TAB << "std::string sUnknownField;" << endl; - } + // if (_unknownField) + // { + // s << TAB << "std::string sUnknownField;" << endl; + // } DEL_TAB; s << TAB << "};" << endl; @@ -1376,6 +1376,7 @@ string Tars2Cpp::generateServantDispatch(const OperationPtr& pPtr, const string& return s.str(); } +/* string Tars2Cpp::promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire) const { ostringstream s; @@ -1442,6 +1443,7 @@ bool Tars2Cpp::isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const } return false; } +*/ string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) const { ostringstream s; @@ -1502,7 +1504,7 @@ string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) cons DEL_TAB; s << TAB << "}" << endl; s << TAB << endl; - +/* //promise异步的函数声明 string sStruct = pPtr->getId(); @@ -1559,7 +1561,7 @@ string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) cons DEL_TAB; s << TAB << "}" << endl; s << endl; - +*/ //协程并行异步的函数声明 s << TAB << "void coro_" << pPtr->getId() << "("; s << cn << "CoroPrxCallbackPtr callback,"; @@ -1811,6 +1813,7 @@ struct SortOperation { } }; +/* string Tars2Cpp::generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const { ostringstream s; @@ -1972,6 +1975,7 @@ string Tars2Cpp::generateDispatchPromiseAsync(const OperationPtr &pPtr, const st return s.str(); } +*/ /******************************InterfacePtr***************************************/ string Tars2Cpp::generateH(const InterfacePtr &pPtr, const NamespacePtr &nPtr) const { @@ -2077,9 +2081,9 @@ string Tars2Cpp::generateH(const InterfacePtr &pPtr, const NamespacePtr &nPtr) c s << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "PrxCallback> " << pPtr->getId() << "PrxCallbackPtr;" << endl; s << endl; - +/* //生成promise异步回调Proxy - s << TAB << "/* callback of promise async proxy for client */" << endl; + s << TAB << "//callback of promise async proxy for client" << endl; s << TAB << "class " << pPtr->getId() << "PrxCallbackPromise: public tars::ServantProxyCallback" << endl; s << TAB << "{" << endl; s << TAB << "public:" << endl; @@ -2156,7 +2160,7 @@ string Tars2Cpp::generateH(const InterfacePtr &pPtr, const NamespacePtr &nPtr) c s << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "PrxCallbackPromise> " << pPtr->getId() << "PrxCallbackPromisePtr;" << endl; s << endl; - +*/ //生成协程异步回调类,用于并发请求 s << TAB << "/* callback of coroutine async proxy for client */" << endl; s << TAB << "class " << pPtr->getId() << "CoroPrxCallback: public " << pPtr->getId() << "PrxCallback" << endl; @@ -2542,7 +2546,7 @@ void Tars2Cpp::generateH(const ContextPtr &pPtr) const { s << "#include \"servant/ServantProxy.h\"" << endl; s << "#include \"servant/Servant.h\"" << endl; - s << "#include \"promise/promise.h\"" << endl; + // s << "#include \"promise/promise.h\"" << endl; break; } } diff --git a/tools/tars2cpp/tars2cpp.h b/tools/tars2cpp/tars2cpp.h index b9191f8..bd49380 100644 --- a/tools/tars2cpp/tars2cpp.h +++ b/tools/tars2cpp/tars2cpp.h @@ -64,7 +64,7 @@ public: /** * setUnknownField for UnknownField support */ - void setUnknownField(bool bUnkownField) { _unknownField = bUnkownField; } + // void setUnknownField(bool bUnkownField) { _unknownField = bUnkownField; } /** * added for master info @@ -113,7 +113,7 @@ protected: * * @return string */ - string promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire = true) const; + // string promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire = true) const; /** * @@ -121,14 +121,14 @@ protected: * * @return string */ - string readUnknown(const TypeIdPtr &pPtr) const; + // string readUnknown(const TypeIdPtr &pPtr) const; /** * * @param pPtr * * @return string */ - string writeUnknown()const; + // string writeUnknown()const; /** * * @param pPtr @@ -354,7 +354,7 @@ protected: * * @return string */ - string generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const; +// string generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const; /** * 生成操作函数调用分发的源码 @@ -363,7 +363,7 @@ protected: * * @return string */ - string generateDispatchPromiseAsync(const OperationPtr &pPtr, const string &cn) const; +// string generateDispatchPromiseAsync(const OperationPtr &pPtr, const string &cn) const; /** * 生成操作的servant的头文件源码 @@ -440,7 +440,7 @@ protected: string generateInitValue(const TypeIdPtr &pPtr) const; - bool isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const; +// bool isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const; private: std::string _baseDir; @@ -453,7 +453,7 @@ private: std::string _namespace ; - bool _unknownField; + // bool _unknownField; bool _tarsMaster; }; diff --git a/util/src/tc_socket.cpp b/util/src/tc_socket.cpp index a1fbdb8..90319cb 100755 --- a/util/src/tc_socket.cpp +++ b/util/src/tc_socket.cpp @@ -788,7 +788,7 @@ clean: TAF_THROW_EXCEPTION_SYSCODE(TC_Socket_Exception, "[TC_Socket::createPipe] error"); } -#if TARGET_PLATFORM_LINUX||TARGET_PLATFORM_IOS +#if TARGET_PLATFORM_LINUX vector TC_Socket::getLocalHosts(int domain) { vector result; @@ -858,6 +858,35 @@ vector TC_Socket::getLocalHosts(int domain) return result; } +#elif TARGET_PLATFORM_IOS +vector TC_Socket::getLocalHosts(int domain) +{ + vector hosts; + char local[255] = { 0 }; + gethostname(local, sizeof(local)); + hostent* ph = gethostbyname(local); + if (ph == NULL) + { + return hosts; + } + + in_addr addr; + if (ph->h_addrtype == AF_INET) + { + int i = 0; + while (ph->h_addr_list[i] != 0) + { + addr.s_addr = *(u_long*)ph->h_addr_list[i++]; + hosts.emplace_back(inet_ntoa(addr)); + } + } + else + { + // unsupport AF_INET6 ... + return hosts; + } + return hosts; +} #endif #if TARGET_PLATFORM_WINDOWS