compiler examples, add auto test scripts

This commit is contained in:
jarodruan 2020-01-29 20:52:34 +08:00
parent 17df5bba83
commit 2401420e78
52 changed files with 5765 additions and 3131 deletions

View File

@ -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------------")

11
examples/CMakeLists.txt Normal file
View File

@ -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")

View File

@ -0,0 +1,648 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __ASERVANT_H_
#define __ASERVANT_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<AServantPrxCallback> AServantPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class AServantCoroPrxCallback: public AServantPrxCallback
{
public:
virtual ~AServantCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __AServant_all[]=
{
"test",
"testInt",
"testStr"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<AServantCoroPrxCallback> AServantCoroPrxCallbackPtr;
/* proxy for client */
class AServantProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(AServantPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(AServantCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(iIn, 1);
_os.write(iOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testInt", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(iIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback);
}
void coro_testInt(AServantCoroPrxCallbackPtr callback,tars::Int32 iIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(iIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback);
}
tars::Int32 testStr(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testStr", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback);
}
void coro_testStr(AServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<AServantProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("iOut", iOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__AServant_all[]=
{
"test",
"testInt",
"testStr"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
tars::Int32 iIn;
tars::Int32 iOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("iOut", iOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(iOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 2:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -0,0 +1,648 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __ASERVANT_H_
#define __ASERVANT_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<AServantPrxCallback> AServantPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class AServantCoroPrxCallback: public AServantPrxCallback
{
public:
virtual ~AServantCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __AServant_all[]=
{
"test",
"testInt",
"testStr"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<AServantCoroPrxCallback> AServantCoroPrxCallbackPtr;
/* proxy for client */
class AServantProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(AServantPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(AServantCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testInt(tars::Int32 iIn,tars::Int32 &iOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(iIn, 1);
_os.write(iOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testInt", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(iIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback);
}
void coro_testInt(AServantCoroPrxCallbackPtr callback,tars::Int32 iIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(iIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testInt", _os, context, _mStatus, callback);
}
tars::Int32 testStr(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testStr", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback);
}
void coro_testStr(AServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<AServantProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("iOut", iOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__AServant_all[]=
{
"test",
"testInt",
"testStr"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
tars::Int32 iIn;
tars::Int32 iOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("iOut", iOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(iOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 2:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -0,0 +1,648 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __BSERVANT_H_
#define __BSERVANT_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<BServantPrxCallback> BServantPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class BServantCoroPrxCallback: public BServantPrxCallback
{
public:
virtual ~BServantCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<BServantCoroPrxCallback> BServantCoroPrxCallbackPtr;
/* proxy for client */
class BServantProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(BServantPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(BServantCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback);
}
void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<BServantProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 2:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -0,0 +1,648 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __BSERVANT_H_
#define __BSERVANT_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<BServantPrxCallback> BServantPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class BServantCoroPrxCallback: public BServantPrxCallback
{
public:
virtual ~BServantCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<BServantCoroPrxCallback> BServantCoroPrxCallbackPtr;
/* proxy for client */
class BServantProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(BServantPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(BServantCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback);
}
void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<BServantProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 2:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -0,0 +1,648 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __BSERVANT_H_
#define __BSERVANT_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<BServantPrxCallback> BServantPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class BServantCoroPrxCallback: public BServantPrxCallback
{
public:
virtual ~BServantCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<BServantCoroPrxCallback> BServantCoroPrxCallbackPtr;
/* proxy for client */
class BServantProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(BServantPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(BServantCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback);
}
void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<BServantProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 2:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -0,0 +1,648 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __BSERVANT_H_
#define __BSERVANT_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<BServantPrxCallback> BServantPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class BServantCoroPrxCallback: public BServantPrxCallback
{
public:
virtual ~BServantCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<BServantCoroPrxCallback> BServantCoroPrxCallbackPtr;
/* proxy for client */
class BServantProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(BServantPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(BServantCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testCoroParallel(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
void coro_testCoroParallel(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroParallel", _os, context, _mStatus, callback);
}
tars::Int32 testCoroSerial(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testCoroSerial", _os, context, _mStatus, callback);
}
void coro_testCoroSerial(BServantCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<BServantProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__BServant_all[]=
{
"test",
"testCoroParallel",
"testCoroSerial"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 2:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -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);
};
};

View File

@ -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 &current)
: _current(current)
{}
BServantCallback(TarsCurrentPtr &current, const tars::Promise<std::string> &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<std::string> _promise;
};
//////////////////////////////////////////////////////
class CServantCallback : public CServantPrxCallback
{
public:
CServantCallback(TarsCurrentPtr &current)
: _current(current)
{}
CServantCallback(TarsCurrentPtr &current, const tars::Promise<std::string> &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<std::string> _promise;
};
//////////////////////////////////////////////////////
tars::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
{
tars::Promise<std::string> promise;
Test::BServantPrxCallbackPtr cb = new BServantCallback(current, promise);
prx->async_queryResult(cb, sIn);
return promise.getFuture();
}
//////////////////////////////////////////////////////
tars::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
{
tars::Promise<std::string> promise;
Test::CServantPrxCallbackPtr cb = new CServantCallback(current, promise);
prx->async_queryResult(cb, sIn);
return promise.getFuture();
}
//////////////////////////////////////////////////////
tars::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future<std::string>& 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<std::string> promise;
promise.setValue(sException);
return promise.getFuture();
}
//////////////////////////////////////////////////////
int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future<std::string>& 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<std::tuple<tars::Future<std::string>, tars::Future<std::string> > >& allFuture)
{
int ret = 0;
std::string sResult("");
try
{
std::tuple<tars::Future<std::string>, tars::Future<std::string> > 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<BServantPrx>("Test.BServer.BServantObj");
_pPrxC = Application::getCommunicator()->stringToProxy<CServantPrx>("Test.CServer.CServantObj");
}
//////////////////////////////////////////////////////
void AServantImp::destroy()
{
}
// class Test1
// {
// public:
// template <typename R>
// void then(const std::function<R(const Future&)>& callback);
// {
// cout <<"then" << endl;
// }
// }
//////////////////////////////////////////////////////
tars::Int32 AServantImp::queryResultSerial(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current)
{
current->setResponse(false);
tars::Future<std::string> 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<std::string>(b1);
return 0;
}
//////////////////////////////////////////////////////
tars::Int32 AServantImp::queryResultParallel(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current)
{
current->setResponse(false);
tars::Future<std::string> f1 = sendBReq(_pPrxB, sIn, current);
tars::Future<std::string> f2 = sendCReq(_pPrxC, sIn, current);
tars::Future<std::tuple<tars::Future<std::string>, tars::Future<std::string> > > f_all = tars::whenAll(f1, f2);
f_all.then(std::bind(&handleBCRspAndReturnClient, current));
return 0;
}

View File

@ -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<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current);
tars::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future<std::string>& future);
tars::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current);
int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future<std::string>& future);
///////////////////////////////////
int handleBCRspAndReturnClient(TarsCurrentPtr current, const tars::Future<std::tuple<tars::Future<std::string>, tars::Future<std::string> > >& 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

View File

@ -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<AServantImp>(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;
}
/////////////////////////////////////////////////////////////////

View File

@ -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);
};
};

View File

@ -1 +0,0 @@
build_tars_server("PromiseDemoAServer")

View File

@ -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);
};
};

View File

@ -1 +0,0 @@
build_tars_server("PromiseDemoBServer")

View File

@ -1,11 +0,0 @@
#-----------------------------------------------------------------------
APP := Test
TARGET := BServer
CONFIG :=
STRIP_FLAG:= N
INCLUDE +=
#-----------------------------------------------------------------------
include /usr/local/tars/cpp/makefile/makefile.tars
#-----------------------------------------------------------------------

View File

@ -1,6 +0,0 @@
add_subdirectory(AServer)
add_subdirectory(BServer)
add_subdirectory(CServer)
add_subdirectory(Client)

View File

@ -1 +0,0 @@
build_tars_server("PromiseDemoCServer")

View File

@ -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;
}

View File

@ -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 <iostream>
#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

View File

@ -1,11 +0,0 @@
#-----------------------------------------------------------------------
APP := Test
TARGET := CServer
CONFIG :=
STRIP_FLAG:= N
INCLUDE +=
#-----------------------------------------------------------------------
include /usr/local/tars/cpp/makefile/makefile.tars
#-----------------------------------------------------------------------

View File

@ -1 +0,0 @@
build_tars_server("PromiseDemoClient")

View File

@ -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 <iostream>
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; i<iExecuteNum; i++)
{
sOut = "";
try
{
int ret = -1;
if(iFlag == 0)
{
ret = prx->queryResultSerial(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<tars::Int32>(string(argv[2]));
TC_ThreadPool tp;
tp.init(threads);
tp.start();
tars::Int32 times = TC_Common::strto<tars::Int32>(string(argv[3]));
tars::Int32 callMode = TC_Common::strto<tars::Int32>(string(argv[4]));
for(int i = 0; i<threads; i++)
{
auto fw = std::bind(&Test1::queryResult, &test1, callMode, times);
tp.exec(fw);
cout << "********************" <<endl;
}
tp.waitForAllDone();
}catch(exception &e)
{
cout<<e.what()<<endl;
}
catch(...)
{
}
return 0;
}

View File

@ -1,6 +0,0 @@
该工程是Tars promise编程示例的代码
目录名称 |功能
-----------------|----------------
AServer | promise编程的示例程序用promsie方式去并行和串行访问BServer和CServer

View File

@ -29,7 +29,7 @@ int main(int argc,char ** argv)
try try
{ {
HelloPrx prx; HelloPrx prx;
comm.stringToProxy("TestApp.HelloServer.HelloObj@tcp -h 10.120.129.226 -p 20001" , prx); comm.stringToProxy("TestApp.HelloServer.HelloObj@tcp -h 127.0.0.1 -p 8999" , prx);
try try
{ {

View File

@ -0,0 +1,471 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __HELLO_H_
#define __HELLO_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<HelloPrxCallback> HelloPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class HelloCoroPrxCallback: public HelloPrxCallback
{
public:
virtual ~HelloCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __Hello_all[]=
{
"test",
"testHello"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<HelloCoroPrxCallback> HelloCoroPrxCallbackPtr;
/* proxy for client */
class HelloProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(HelloPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(HelloCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testHello(const std::string & sReq,std::string &sRsp,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sReq, 1);
_os.write(sRsp, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testHello", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sReq, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback);
}
void coro_testHello(HelloCoroPrxCallbackPtr callback,const std::string &sReq,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sReq, 1);
std::map<string, string> _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<HelloProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sRsp", sRsp);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sRsp, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __TestApp__Hello_all[]=
{
"test",
"testHello"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sReq;
std::string sRsp;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sRsp", sRsp);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sRsp, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -14,12 +14,13 @@
* specific language governing permissions and limitations under the License. * 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);
}; };
}; };

View File

@ -14,32 +14,29 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#include "BServantImp.h" #include "HelloImp.h"
#include "BServer.h"
#include "servant/Application.h" #include "servant/Application.h"
#include "servant/Communicator.h"
using namespace std; using namespace std;
using namespace tars;
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
void BServantImp::initialize() void HelloImp::initialize()
{ {
//initialize servant here: //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:"<<sReq<<endl);
sRsp = sReq;
return 0; return 0;
} }

View File

@ -14,27 +14,40 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#ifndef _BServantImp_H_ #ifndef _HelloImp_H_
#define _BServantImp_H_ #define _HelloImp_H_
#include "servant/Application.h" #include "servant/Application.h"
#include "CServant.h" #include "Hello.h"
#include "promise/promise.h"
using namespace Test; /**
*
class CServantImp : public Test::CServant *
*/
class HelloImp : public TestApp::Hello
{ {
public: public:
/**
*
*/
virtual ~HelloImp() {}
virtual ~CServantImp() {} /**
*
*/
virtual void initialize(); virtual void initialize();
/**
*
*/
virtual void destroy(); virtual void destroy();
tars::Int32 queryResult(const std::string& sIn, std::string &sOut, tars::TarsCurrentPtr current); /**
*
*/
virtual int test(tars::TarsCurrentPtr current) { return 0;};
virtual int testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current);
}; };
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
#endif #endif

View File

@ -14,30 +14,32 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#include "CServer.h" #include "HelloServer.h"
#include "CServantImp.h" #include "HelloImp.h"
using namespace std; using namespace std;
CServer g_app; HelloServer g_app;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
void CServer::initialize() void
HelloServer::initialize()
{ {
//initialize application here: //initialize application here:
//... //...
addServant<CServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".CServantObj"); addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".HelloObj");
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
void CServer::destroyApp() void
HelloServer::destroyApp()
{ {
//destroy application here: //destroy application here:
//... //...
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) int
main(int argc, char* argv[])
{ {
try try
{ {

View File

@ -14,8 +14,8 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#ifndef _BServer_H_ #ifndef _HelloServer_H_
#define _BServer_H_ #define _HelloServer_H_
#include <iostream> #include <iostream>
#include "servant/Application.h" #include "servant/Application.h"
@ -25,13 +25,13 @@ using namespace tars;
/** /**
* *
**/ **/
class BServer : public Application class HelloServer : public Application
{ {
public: public:
/** /**
* *
**/ **/
virtual ~BServer() {}; virtual ~HelloServer() {};
/** /**
* *
@ -42,11 +42,9 @@ public:
* *
**/ **/
virtual void destroyApp(); virtual void destroyApp();
protected:
bool cmdprofile(const string& command, const string& params, string& result);
}; };
extern BServer g_app; extern HelloServer g_app;
//////////////////////////////////////////// ////////////////////////////////////////////
#endif #endif

View File

@ -0,0 +1,70 @@
<tars>
<application>
<client>
#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
</client>
<server>
#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
<HelloAdapter>
#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
</HelloAdapter>
</server>
</application>
</tars>

View File

@ -1,12 +1,16 @@
#----------------------------------------------------------------------- #-----------------------------------------------------------------------
APP := Test
TARGET := AServer APP := TestApp
TARGET := HelloServer
CONFIG := CONFIG :=
STRIP_FLAG:= N STRIP_FLAG:= N
INCLUDE += INCLUDE +=
LIB +=
#----------------------------------------------------------------------- #-----------------------------------------------------------------------
include /usr/local/tars/cpp/makefile/makefile.tars include /usr/local/tars/cpp/makefile/makefile.tars
#----------------------------------------------------------------------- #-----------------------------------------------------------------------

View File

@ -0,0 +1,471 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __PROXY_H_
#define __PROXY_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<ProxyPrxCallback> ProxyPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class ProxyCoroPrxCallback: public ProxyPrxCallback
{
public:
virtual ~ProxyCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __Proxy_all[]=
{
"test",
"testProxy"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<ProxyCoroPrxCallback> ProxyCoroPrxCallbackPtr;
/* proxy for client */
class ProxyProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(ProxyPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(ProxyCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testProxy(const std::string & sReq,std::string &sRsp,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sReq, 1);
_os.write(sRsp, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testProxy", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sReq, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testProxy", _os, context, _mStatus, callback);
}
void coro_testProxy(ProxyCoroPrxCallbackPtr callback,const std::string &sReq,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sReq, 1);
std::map<string, string> _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<ProxyProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sRsp", sRsp);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sRsp, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __TestApp__Proxy_all[]=
{
"test",
"testProxy"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sReq;
std::string sRsp;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sRsp", sRsp);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sRsp, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -14,12 +14,13 @@
* specific language governing permissions and limitations under the License. * 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);
}; };
}; };

View File

@ -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 &current)
: _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<HelloPrx>("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;
}

View File

@ -14,26 +14,49 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#ifndef _BServantImp_H_ #ifndef __PROXY_IMP_H_
#define _BServantImp_H_ #define __PROXY_IMP_H_
#include "servant/Application.h" #include "servant/Application.h"
#include "BServant.h" #include "Hello.h"
#include "promise/promise.h" #include "Proxy.h"
using namespace Test; using namespace TestApp;
class BServantImp : public Test::BServant /**
*
*
*/
class ProxyImp : public TestApp::Proxy
{ {
public: public:
/**
*
*/
virtual ~ProxyImp() {}
virtual ~BServantImp() {} /**
*
*/
virtual void initialize(); virtual void initialize();
/**
*
*/
virtual void destroy(); 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;
}; };
///////////////////////////////////////////////////// /////////////////////////////////////////////////////

View File

@ -14,30 +14,30 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#include "BServer.h" #include "ProxyServer.h"
#include "BServantImp.h" #include "ProxyImp.h"
using namespace std; using namespace std;
BServer g_app; ProxyServer g_app;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
void BServer::initialize() void ProxyServer::initialize()
{ {
//initialize application here: //initialize application here:
//... addServant<ProxyImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".ProxyObj");
addServant<BServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".BServantObj");
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
void BServer::destroyApp() void ProxyServer::destroyApp()
{ {
//destroy application here: //destroy application here:
//... //...
} }
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]) int
main(int argc, char* argv[])
{ {
try try
{ {

View File

@ -14,8 +14,8 @@
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
#ifndef _BServer_H_ #ifndef __PROXY_SERVER_H_
#define _BServer_H_ #define __PROXY_SERVER_H_
#include <iostream> #include <iostream>
#include "servant/Application.h" #include "servant/Application.h"
@ -25,13 +25,13 @@ using namespace tars;
/** /**
* *
**/ **/
class AServer : public Application class ProxyServer : public Application
{ {
public: public:
/** /**
* *
**/ **/
virtual ~AServer() {}; virtual ~ProxyServer() {};
/** /**
* *
@ -43,10 +43,10 @@ public:
**/ **/
virtual void destroyApp(); virtual void destroyApp();
protected: protected:
bool cmdprofile(const string& command, const string& params, string& result);
}; };
extern AServer g_app; extern ProxyServer g_app;
//////////////////////////////////////////// ////////////////////////////////////////////
#endif #endif

View File

@ -1,13 +1,17 @@
#----------------------------------------------------------------------- #-----------------------------------------------------------------------
APP := Test
TARGET := myClientPromise APP := TestApp
TARGET := ProxyServer
CONFIG := CONFIG :=
STRIP_FLAG:= N 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 include /usr/local/tars/cpp/makefile/makefile.tars
#----------------------------------------------------------------------- #-----------------------------------------------------------------------

View File

@ -0,0 +1,471 @@
// **********************************************************************
// This file was generated by a TARS parser!
// TARS version 1.4.0.
// **********************************************************************
#ifndef __STRESS_H_
#define __STRESS_H_
#include <map>
#include <string>
#include <vector>
#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<std::string, std::string> & 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<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<StressPrxCallback> StressPrxCallbackPtr;
/* callback of coroutine async proxy for client */
class StressCoroPrxCallback: public StressPrxCallback
{
public:
virtual ~StressCoroPrxCallback(){}
public:
virtual const map<std::string, std::string> & getResponseContext() const { return _mRspContext; }
virtual void setResponseContext(const map<std::string, std::string> &mContext) { _mRspContext = mContext; }
public:
int onDispatch(tars::ReqMessagePtr msg)
{
static ::std::string __Stress_all[]=
{
"test",
"testStr"
};
pair<string*, string*> 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<tars::BufferReader> _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<tars::BufferReader> _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<std::string, std::string> _mRspContext;
};
typedef tars::TC_AutoPtr<StressCoroPrxCallback> StressCoroPrxCallbackPtr;
/* proxy for client */
class StressProxy : public tars::ServantProxy
{
public:
typedef map<string, string> TARS_CONTEXT;
tars::Int32 test(const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(rep->sBuffer);
tars::Int32 _ret;
_is.read(_ret, 0, true);
return _ret;
}
void async_test(StressPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
void coro_test(StressCoroPrxCallbackPtr callback,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback);
}
tars::Int32 testStr(const std::string & sIn,std::string &sOut,const map<string, string> &context = TARS_CONTEXT(),map<string, string> * pResponseContext = NULL)
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
_os.write(sOut, 2);
std::map<string, string> _mStatus;
shared_ptr<tars::ResponsePacket> rep = tars_invoke(tars::TARSNORMAL,"testStr", _os, context, _mStatus);
if(pResponseContext)
{
pResponseContext->swap(rep->context);
}
tars::TarsInputStream<tars::BufferReader> _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<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _mStatus;
tars_invoke_async(tars::TARSNORMAL,"testStr", _os, context, _mStatus, callback);
}
void coro_testStr(StressCoroPrxCallbackPtr callback,const std::string &sIn,const map<string, string>& context = TARS_CONTEXT())
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(sIn, 1);
std::map<string, string> _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<StressProxy> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
vector<char> sTupResponseBuffer;
tarsAttr.encode(sTupResponseBuffer);
current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer());
}
}
public:
int onDispatch(tars::TarsCurrentPtr _current, vector<char> &_sResponseBuffer)
{
static ::std::string __Test__Stress_all[]=
{
"test",
"testStr"
};
pair<string*, string*> 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<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.decode(_current->getRequestBuffer());
}
else
{
}
tars::Int32 _ret = test(_current);
if(_current->isResponse())
{
if (_current->getRequestVersion() == TUPVERSION )
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
case 1:
{
tars::TarsInputStream<tars::BufferReader> _is;
_is.setBuffer(_current->getRequestBuffer());
std::string sIn;
std::string sOut;
if (_current->getRequestVersion() == TUPVERSION)
{
UniAttribute<tars::BufferWriterVector, tars::BufferReader> 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<tars::BufferWriterVector, tars::BufferReader> tarsAttr;
tarsAttr.setVersion(_current->getRequestVersion());
tarsAttr.put("", _ret);
tarsAttr.put("sOut", sOut);
tarsAttr.encode(_sResponseBuffer);
}
else
{
tars::TarsOutputStream<tars::BufferWriterVector> _os;
_os.write(_ret, 0);
_os.write(sOut, 2);
_os.swap(_sResponseBuffer);
}
}
return tars::TARSSERVERSUCCESS;
}
}
return tars::TARSSERVERNOFUNCERR;
}
};
}
#endif

View File

@ -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

View File

@ -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 <stdexcept>
#include <new>
#include <typeinfo>
#include <string>
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<detail::ExceptionBase> 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<std::bad_alloc>::e;
return ExceptionPtr(new detail::ExceptionBaseImpl(string(e.what(), strlen(e.what())) ));
}
catch (...)
{
//return detail::ExceptionPtrStaticExceptionObject<CurrentExceptionUnknownException>::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 <typename E>
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__

View File

@ -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 <pthread.h>
#include <list>
#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 <typename T> class Future;
template <typename T> struct is_future_type : std::false_type {};
template <typename T> struct is_future_type<Future<T> > : std::true_type {};
template <typename T> class Promise;
namespace detail
{
template <typename T>
struct resolved_type
{
typedef T type;
};
template <typename T>
struct resolved_type<Future<T> >
{
typedef T type;
};
template <typename T>
struct FutureTraits
{
// typedef TC_ScopedPtr<T> storage_type;
typedef std::unique_ptr<T> 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 <typename T>
struct FutureTraits<T&>
{
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<void>
{
typedef void rvalue_source_type;
typedef void move_dest_type;
};
template <typename T>
class FutureObjectInterface
{
public:
typedef typename FutureTraits<T>::rvalue_source_type rvalue_source_type;
typedef typename FutureTraits<T>::move_dest_type move_dest_type;
typedef typename FutureTraits<T>::dest_reference_type dest_reference_type;
typedef std::function<void(const std::shared_ptr<FutureObjectInterface>&)> 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<void>
{
public:
typedef FutureTraits<void>::move_dest_type move_dest_type;
typedef std::function<void(const std::shared_ptr<FutureObjectInterface>&)> 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 <typename T>
class PromptFutureObject : public FutureObjectInterface<T>,
public std::enable_shared_from_this<PromptFutureObject<T> >
{
public:
typedef typename FutureObjectInterface<T>::move_dest_type move_dest_type;
typedef typename FutureObjectInterface<T>::rvalue_source_type rvalue_source_type;
typedef typename FutureObjectInterface<T>::dest_reference_type dest_reference_type;
typedef typename FutureObjectInterface<T>::CallbackType CallbackType;
PromptFutureObject(rvalue_source_type v)
{
FutureTraits<T>::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<T>::assign(v, m_value); return true; }
virtual void registerCallback(const CallbackType& callback)
{
assert(callback);
try
{
callback(this->shared_from_this());
}
catch (...)
{
}
}
private:
typename FutureTraits<T>::storage_type m_value;
ExceptionPtr m_exception_ptr;
};
template <>
class PromptFutureObject<void> : public FutureObjectInterface<void>,
public std::enable_shared_from_this<PromptFutureObject<void> >
{
public:
typedef FutureObjectInterface<void>::move_dest_type move_dest_type;
typedef FutureObjectInterface<void>::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 <typename T>
class FutureObject : public FutureObjectInterface<T>,
public std::enable_shared_from_this<FutureObject<T> >,
private FutureObjectBase
{
public:
typedef typename FutureObjectInterface<T>::move_dest_type move_dest_type;
typedef typename FutureObjectInterface<T>::rvalue_source_type rvalue_source_type;
typedef typename FutureObjectInterface<T>::CallbackType CallbackType;
typedef typename FutureObjectInterface<T>::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<T>::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<CallbackType> callbacks;
{
TC_ThreadLock::Lock lock(m_monitor);
callbacks.swap(m_pending_callbacks);
}
for (typename std::list<CallbackType>::const_iterator it = callbacks.begin(),
end = callbacks.end();
it != end; ++it)
{
try
{
(*it)(this->shared_from_this());
}
catch (...)
{
}
}
}
typename FutureTraits<T>::storage_type m_value;
std::list<CallbackType> m_pending_callbacks;
};
template <>
class FutureObject<void> : public FutureObjectInterface<void>,
public std::enable_shared_from_this<FutureObject<void> >,
private FutureObjectBase
{
public:
using typename FutureObjectInterface<void>::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<CallbackType> callbacks;
{
TC_ThreadLock::Lock lock(m_monitor);
callbacks.swap(m_pending_callbacks);
}
for (std::list<CallbackType>::const_iterator it = callbacks.begin(),
end = callbacks.end();
it != end; ++it)
{
(*it)(this->shared_from_this());
}
}
std::list<CallbackType> m_pending_callbacks;
};
template <typename R> class ForwardValue;
template <typename R, typename T>
class SequentialCallback
{
private:
typedef typename detail::resolved_type<R>::type value_type;
typedef std::shared_ptr<detail::FutureObjectInterface<T> > FuturePtr;
public:
SequentialCallback(const std::function<R(const Future<T>&)>& callback,
const Promise<value_type>& promise)
: m_callback(callback)
, m_promise(promise)
{}
template <typename U>
typename std::enable_if<std::is_void<U>::value >::type run(const FuturePtr& future)
{
try
{
m_callback(future);
m_promise.set();
}
catch (...)
{
m_promise.setException(currentException());
}
}
template <typename U>
typename std::enable_if<!std::is_void<U>::value && !is_future_type<U>::value>::type
run(const FuturePtr& future)
{
try
{
m_promise.setValue(m_callback(future));
}
catch (...)
{
m_promise.setException(currentException());
}
}
template <typename U>
typename std::enable_if<is_future_type<U>::value >::type run(const FuturePtr& future)
{
try
{
m_callback(future).then(TC_Bind(&ForwardValue<value_type>::template run<value_type>,
tc_owned(new ForwardValue<value_type>(m_promise))));
}
catch (...)
{
m_promise.setException(currentException());
}
}
private:
std::function<R(const Future<T>&)> m_callback;
Promise<value_type> m_promise;
};
template <typename R>
class ForwardValue
{
public:
ForwardValue(const Promise<R>& promise)
: m_promise(promise)
{}
template <typename V>
typename std::enable_if<std::is_void<V>::value >::type run(const Future<V>& future)
{
try
{
future.get();
m_promise.set();
}
catch (...)
{
m_promise.setException(currentException());
}
}
template <typename V>
typename std::enable_if<!std::is_void<V>::value >::type run(const Future<V>& future)
{
try
{
m_promise.setValue(future.get());
}
catch (...)
{
m_promise.setException(currentException());
}
}
private:
Promise<R> m_promise;
};
template <typename T>
class FutureBase
{
private:
typedef typename detail::FutureTraits<T>::move_dest_type move_dest_type;
typedef typename detail::FutureTraits<T>::rvalue_source_type rvalue_source_type;
public:
typedef bool (FutureBase::*unspecified_bool_type)() const;
FutureBase() {}
FutureBase(const ExceptionPtr& e)
: m_future(std::shared_ptr<detail::PromptFutureObject<T> >(new detail::PromptFutureObject<T>(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<detail::FutureObjectInterface<T> > FuturePtr;
FutureBase(const FuturePtr& future)
: m_future(future)
{}
FuturePtr m_future;
};
} // namespace detail
template <typename T>
class Future : public detail::FutureBase<T>
{
public:
Future() {}
explicit Future(typename detail::FutureTraits<T>::rvalue_source_type t)
: detail::FutureBase<T>(std::shared_ptr<detail::PromptFutureObject<T> >(new detail::PromptFutureObject<T>(t)))
{}
Future(const ExceptionPtr& e)
: detail::FutureBase<T>(e)
{}
virtual ~Future() {}
template <typename R>
Future<R> then(const std::function<R(const Future<T>&)>& callback) const
{
// typedef typename detail::resolved_type<R>::type value_type;
if (!this->m_future)
{
throwException(FutureUninitializedException(__FILE__, __LINE__));
}
Promise<R> promise;
shared_ptr<detail::SequentialCallback<R, T>> ptr(new detail::SequentialCallback<R, T>(callback, promise));
this->m_future->registerCallback(std::bind(&detail::SequentialCallback<R, T>::template run<R>, ptr));
return promise.getFuture();
}
// template <typename R>
// Future<typename detail::resolved_type<R>::type> then(const std::function<R(const Future<T>&)>& callback) const
// {
// typedef typename detail::resolved_type<R>::type value_type;
// if (!this->m_future)
// {
// throwException(FutureUninitializedException(__FILE__, __LINE__));
// }
// Promise<value_type> promise;
// this->m_future->registerCallback(TC_Bind(&detail::SequentialCallback<R, T>::template run<R>,
// tc_owned(new detail::SequentialCallback<R, T>(callback, promise))));
// return promise.getFuture();
// }
private:
Future(const typename detail::FutureBase<T>::FuturePtr& future)
: detail::FutureBase<T>(future)
{}
template <typename R, typename U> friend class detail::SequentialCallback;
friend class Promise<T>;
};
template <>
class Future<void> : public detail::FutureBase<void>
{
public:
Future() {}
Future(const ExceptionPtr& e)
: detail::FutureBase<void>(e)
{}
virtual ~Future() {}
template <typename R>
Future<typename detail::resolved_type<R>::type> then(const std::function<R(const Future<void>&)>& callback) const
{
typedef typename detail::resolved_type<R>::type value_type;
if (!this->m_future)
{
throwException(FutureUninitializedException(__FILE__, __LINE__));
}
Promise<value_type> promise;
this->m_future->registerCallback(TC_Bind(&detail::SequentialCallback<R, void>::template run<R>,
tc_owned(new detail::SequentialCallback<R, void>(callback, promise))));
return promise.getFuture();
}
private:
Future(const detail::FutureBase<void>::FuturePtr& future)
: detail::FutureBase<void>(future)
{}
friend Future<void> makeFuture();
template <typename R, typename U> friend class detail::SequentialCallback;
friend class Promise<void>;
};
template <typename T>
// typename std::disable_if<std::is_void<T>, Future<T> >::type
typename std::enable_if<!std::is_void<T>::value, Future<T> >::type
makeFuture(typename detail::FutureTraits<T>::rvalue_source_type t)
{
return Future<T>(t);
}
inline Future<void> makeFuture()
{
return Future<void>(std::shared_ptr<detail::PromptFutureObject<void> >(new detail::PromptFutureObject<void>()));
}
template <typename T>
class Promise
{
public:
Promise()
: m_future(std::shared_ptr<detail::FutureObject<T> >(new detail::FutureObject<T>()))
{}
void setValue(typename detail::FutureTraits<T>::rvalue_source_type v)
{
assert(m_future);
m_future->setValue(v);
}
void setException(const ExceptionPtr& e)
{
m_future->setException(e);
}
Future<T> getFuture() const
{
return Future<T>(m_future);
}
private:
std::shared_ptr<detail::FutureObjectInterface<T> > m_future;
};
template <>
class Promise<void>
{
private:
std::shared_ptr<detail::FutureObjectInterface<void> > m_future;
public:
Promise()
: m_future(std::shared_ptr<detail::FutureObject<void> >(new detail::FutureObject<void>()))
{}
void set()
{
assert(m_future);
m_future->set();
}
void setException(const ExceptionPtr& e)
{
m_future->setException(e);
}
Future<void> getFuture() const
{
return Future<void>(m_future);
}
};
} // namespace promise
#endif // __TARS_PROMISE_H_

View File

@ -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 <string.h>
#include <exception>
#include <string>
#include <sstream>
#include <memory>
// #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<std::string> _what;
mutable std::shared_ptr<std::string> _data;
mutable char _sep;
template <typename T>
void injectErrorInfo(const T& t) const;
template <typename Exception, typename T>
friend typename std::enable_if<std::is_base_of<Exception, PromiseException>::value, const Exception&>::type
operator<<(const Exception& e, const T& t)
{
e.injectErrorInfo(t);
return e;
}
};
template <typename T>
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__

View File

@ -1,591 +0,0 @@
#ifndef __TARS_PROMISE_TUPLE_H_
#define __TARS_PROMISE_TUPLE_H_
// #include "util/detail/tc_assert.h"
#include <ostream>
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 <typename HT, typename TT>
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 <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9>
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 <typename HT2, typename TT2>
Cons(const Cons<HT2, TT2>& o)
: head(o.head)
, tail(o.tail)
{}
template <typename HT2, typename TT2>
Cons& operator=(const Cons<HT2, TT2>& 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 <typename HT>
struct Cons<HT, null_type>
{
typedef HT head_type;
typedef null_type tail_type;
Cons()
: head()
{}
template <typename T0>
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 <typename HT2>
Cons& operator=(const Cons<HT2, null_type>& 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 <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9>
struct map_tuple_to_cons
{
typedef Cons<T0, typename map_tuple_to_cons<T1, T2, T3, T4, T5, T6, T7, T8, T9, null_type>::type> type;
};
template <>
struct map_tuple_to_cons<null_type, null_type, null_type, null_type, null_type,
null_type, null_type, null_type, null_type, null_type>
{
typedef null_type type;
};
// Gets the N-th tail of a tuple.
template <int N, typename Tuple>
struct drop_front
{
typedef typename drop_front<N-1, Tuple>::type::tail_type type;
static const type& get_tail(const Tuple& tuple)
{
return drop_front<N-1, Tuple>::get_tail(tuple).tail;
}
};
template <typename Tuple>
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 <int N, typename Tuple>
struct element
{
typedef typename drop_front<N, Tuple>::type::head_type type;
};
// Gets the length of a tuple.
template <typename Tuple>
struct length
{
static const int value = length<typename Tuple::tail_type>::value + 1;
};
template <>
struct length<null_type>
{
static const int value = 0;
};
// Helper classes to print Cons.
template <typename HT, typename TT>
struct print_cons_helper
{
static std::ostream& print(std::ostream& o, const Cons<HT, TT>& cons)
{
o << cons.head;
o << ", ";
print_cons_helper<typename TT::head_type, typename TT::tail_type>::print(o, cons.tail);
return o;
}
};
template <typename HT>
struct print_cons_helper<HT, null_type>
{
static std::ostream& print(std::ostream& o, const Cons<HT, null_type>& cons)
{
o << cons.head;
return o;
}
};
template <typename HT, typename TT>
inline std::ostream& print_cons(std::ostream& o, const Cons<HT, TT>& cons)
{
return print_cons_helper<HT, TT>::print(o, cons);
}
inline std::ostream& print_cons(std::ostream& o, const null_type&)
{
return o;
}
} // namespace detail
template <typename T0 = detail::null_type, typename T1 = detail::null_type, typename T2 = detail::null_type,
typename T3 = detail::null_type, typename T4 = detail::null_type, typename T5 = detail::null_type,
typename T6 = detail::null_type, typename T7 = detail::null_type, typename T8 = detail::null_type,
typename T9 = detail::null_type>
class Tuple
{
private:
typedef typename detail::map_tuple_to_cons<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type tuple_impl_type;
public:
// Returns the type of the N-th element of this tuple.
template <int N>
struct element
{
typedef typename detail::element<N, tuple_impl_type>::type type;
};
template <typename T>
struct access_traits
{
typedef const T& parameter_type;
typedef const T& const_type;
typedef T& non_const_type;
};
template <typename T>
struct access_traits<T&>
{
typedef T& parameter_type;
typedef T& const_type;
typedef T& non_const_type;
};
// Number of elements of this tuple.
enum
{
LENGTH = detail::length<tuple_impl_type>::value
};
Tuple() {}
Tuple(typename access_traits<T0>::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<T0>::parameter_type t0,
typename access_traits<T1>::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<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::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<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::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<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::parameter_type t3,
typename access_traits<T4>::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<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::parameter_type t3,
typename access_traits<T4>::parameter_type t4,
typename access_traits<T5>::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<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::parameter_type t3,
typename access_traits<T4>::parameter_type t4,
typename access_traits<T5>::parameter_type t5,
typename access_traits<T6>::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<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::parameter_type t3,
typename access_traits<T4>::parameter_type t4,
typename access_traits<T5>::parameter_type t5,
typename access_traits<T6>::parameter_type t6,
typename access_traits<T7>::parameter_type t7)
: m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, detail::make_null(), detail::make_null())
{}
Tuple(typename access_traits<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::parameter_type t3,
typename access_traits<T4>::parameter_type t4,
typename access_traits<T5>::parameter_type t5,
typename access_traits<T6>::parameter_type t6,
typename access_traits<T7>::parameter_type t7,
typename access_traits<T8>::parameter_type t8)
: m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, t8, detail::make_null())
{}
Tuple(typename access_traits<T0>::parameter_type t0,
typename access_traits<T1>::parameter_type t1,
typename access_traits<T2>::parameter_type t2,
typename access_traits<T3>::parameter_type t3,
typename access_traits<T4>::parameter_type t4,
typename access_traits<T5>::parameter_type t5,
typename access_traits<T6>::parameter_type t6,
typename access_traits<T7>::parameter_type t7,
typename access_traits<T8>::parameter_type t8,
typename access_traits<T9>::parameter_type t9)
: m_tuple_impl(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9)
{}
template <typename TT0, typename TT1, typename TT2, typename TT3, typename TT4,
typename TT5, typename TT6, typename TT7, typename TT8, typename TT9>
Tuple(const Tuple<TT0, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9>& o)
: m_tuple_impl(o.m_tuple_impl)
{}
template <typename TT0, typename TT1, typename TT2, typename TT3, typename TT4,
typename TT5, typename TT6, typename TT7, typename TT8, typename TT9>
Tuple& operator=(const Tuple<TT0, TT1, TT2, TT3, TT4, TT5, TT6, TT7, TT8, TT9>& o)
{
m_tuple_impl = o.m_tuple_impl;
return *this;
}
template <int N>
typename access_traits<typename element<N>::type>::const_type
get() const
{
TC_STATIC_ASSERT(N < LENGTH, index_out_of_bounds);
return detail::drop_front<N, tuple_impl_type>::get_tail(m_tuple_impl).head;
}
template <int N>
typename access_traits<typename element<N>::type>::non_const_type
get()
{
TC_STATIC_ASSERT(N < LENGTH, index_out_of_bounds);
typedef detail::drop_front<N, tuple_impl_type> impl;
typedef typename impl::type cons_element;
return const_cast<cons_element&>(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 <typename TT0, typename TT1, typename TT2, typename TT3, typename TT4,
typename TT5, typename TT6, typename TT7, typename TT8, typename TT9>
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 <typename T>
struct make_tuple_traits
{
typedef T type;
};
template <typename T>
struct make_tuple_traits<T&>
{
typedef T type;
};
// To produce compilation error intentionally by referencing an non-exsitent
// member of this class.
template <typename T>
class generate_error {};
template <typename T, size_t n>
struct make_tuple_traits<T[n]>
{
typedef const T* type;
};
template <typename T>
struct make_tuple_traits<const T[]>
{
typedef const T* type;
};
template <typename T0 = null_type, typename T1 = null_type, typename T2 = null_type,
typename T3 = null_type, typename T4 = null_type, typename T5 = null_type,
typename T6 = null_type, typename T7 = null_type, typename T8 = null_type,
typename T9 = null_type>
struct make_tuple_mapper
{
typedef Tuple<typename make_tuple_traits<T0>::type,
typename make_tuple_traits<T1>::type,
typename make_tuple_traits<T2>::type,
typename make_tuple_traits<T3>::type,
typename make_tuple_traits<T4>::type,
typename make_tuple_traits<T5>::type,
typename make_tuple_traits<T6>::type,
typename make_tuple_traits<T7>::type,
typename make_tuple_traits<T8>::type,
typename make_tuple_traits<T9>::type> type;
};
} // namespace detail
// make_tuple function templates for arities 0~10
inline Tuple<> make_tuple()
{ return Tuple<>(); }
template <typename T0>
inline typename detail::make_tuple_mapper<T0>::type
make_tuple(const T0& t0)
{
typedef typename detail::make_tuple_mapper<T0>::type tuple_type;
return tuple_type(t0);
}
template <typename T0, typename T1>
inline typename detail::make_tuple_mapper<T0, T1>::type
make_tuple(const T0& t0, const T1& t1)
{
typedef typename detail::make_tuple_mapper<T0, T1>::type tuple_type;
return tuple_type(t0, t1);
}
template <typename T0, typename T1, typename T2>
inline typename detail::make_tuple_mapper<T0, T1, T2>::type
make_tuple(const T0& t0, const T1& t1, const T2& t2)
{
typedef typename detail::make_tuple_mapper<T0, T1, T2>::type tuple_type;
return tuple_type(t0, t1, t2);
}
template <typename T0, typename T1, typename T2, typename T3>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3>::type
make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3)
{
typedef typename detail::make_tuple_mapper<T0, T1, T2, T3>::type tuple_type;
return tuple_type(t0, t1, t2, t3);
}
template <typename T0, typename T1, typename T2, typename T3, typename T4>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4>::type
make_tuple(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4)
{
typedef typename detail::make_tuple_mapper<T0, T1, T2, T3, T4>::type tuple_type;
return tuple_type(t0, t1, t2, t3, t4);
}
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5>::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<T0, T1, T2, T3, T4, T5>::type tuple_type;
return tuple_type(t0, t1, t2, t3, t4, t5);
}
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6>::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<T0, T1, T2, T3, T4, T5, T6>::type tuple_type;
return tuple_type(t0, t1, t2, t3, t4, t5, t6);
}
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6, T7>::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<T0, T1, T2, T3, T4, T5, T6, T7>::type tuple_type;
return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7);
}
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6, T7, T8>::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<T0, T1, T2, T3, T4, T5, T6, T7, T8>::type tuple_type;
return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7, t8);
}
template <typename T0, typename T1, typename T2, typename T3, typename T4,
typename T5, typename T6, typename T7, typename T8, typename T9>
inline typename detail::make_tuple_mapper<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::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<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>::type tuple_type;
return tuple_type(t0, t1, t2, t3, t4, t5, t6, t7, t8, t9);
}
} // namespace promise
#endif // __TARS_PROMISE_TUPLE_H__

View File

@ -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 <vector>
// #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 <typename FutureAllValueType>
struct ParallelAllCallbackBase
{
public:
typedef Promise<FutureAllValueType> 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<tars::Future, ...> or
// std::vector<tars::Future>.
//
// This implementation is for tars::std::tuple<>.
template <typename FutureAllValueType>
class ParallelAllCallback : private ParallelAllCallbackBase<FutureAllValueType>
{
public:
typedef typename ParallelAllCallbackBase<FutureAllValueType>::PromiseAll PromiseAll;
ParallelAllCallback(const PromiseAll& promise_all)
: ParallelAllCallbackBase<FutureAllValueType>(promise_all, FutureAllValueType::LENGTH)
{}
virtual ~ParallelAllCallback() {}
template <int N>
void on_future(const typename FutureAllValueType::template element<N>::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<N>() = 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<tars::Future<> >.
template <typename FutureType>
class ParallelAllCallback<std::vector<FutureType> > : private ParallelAllCallbackBase<std::vector<FutureType> >
{
public:
typedef typename ParallelAllCallbackBase<std::vector<FutureType> >::PromiseAll PromiseAll;
ParallelAllCallback(const PromiseAll& promise_all,
size_t number_of_non_satisfied)
: ParallelAllCallbackBase<std::vector<FutureType> >(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<std::vector<> > which future is done.
template <typename T>
class VectorParallelCallback
{
public:
VectorParallelCallback(const std::shared_ptr<ParallelAllCallback<std::vector<Future<T> > > >& cb, size_t idx)
: m_parallel_callback(cb),
m_idx(idx)
{}
void on_future(const Future<T>& future)
{
m_parallel_callback->on_future(future, m_idx);
}
private:
std::shared_ptr<ParallelAllCallback<std::vector<Future<T> > > > 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 <typename T1, typename T2>
Future<std::tuple<Future<T1>, Future<T2> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2)
{
typedef std::tuple<Future<T1>, Future<T2> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T1, typename T2, typename T3>
Future<std::tuple<Future<T1>, Future<T2>, Future<T3> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2, const Future<T3>& future3)
{
typedef std::tuple<Future<T1>, Future<T2>, Future<T3> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T1, typename T2, typename T3, typename T4>
Future<std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2, const Future<T3>& future3, const Future<T4>& future4)
{
typedef std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T1, typename T2, typename T3, typename T4, typename T5>
Future<std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2, const Future<T3>& future3, const Future<T4>& future4,
const Future<T5>& future5)
{
typedef std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
Future<std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5>, Future<T6> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2, const Future<T3>& future3, const Future<T4>& future4,
const Future<T5>& future5, const Future<T6>& future6)
{
typedef std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5>, Future<T6> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
Future<std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5>, Future<T6>, Future<T7> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2, const Future<T3>& future3, const Future<T4>& future4,
const Future<T5>& future5, const Future<T6>& future6, const Future<T7>& future7)
{
typedef std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5>, Future<T6>, Future<T7> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
Future<std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5>, Future<T6>, Future<T7>, Future<T8> > >
whenAll(const Future<T1>& future1, const Future<T2>& future2, const Future<T3>& future3, const Future<T4>& future4,
const Future<T5>& future5, const Future<T6>& future6, const Future<T7>& future7, const Future<T8>& future8)
{
typedef std::tuple<Future<T1>, Future<T2>, Future<T3>, Future<T4>, Future<T5>, Future<T6>, Future<T7>, Future<T8> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> 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 <typename T, typename InputIterator>
Future<std::vector<Future<T> > >
whenAll(InputIterator first, InputIterator last)
{
typedef std::vector<Future<T> > FutureAllValueType;
typedef Promise<FutureAllValueType> PromiseAll;
typedef detail::ParallelAllCallback<FutureAllValueType> WhenAllCallback;
PromiseAll promise_all;
if (first == last)
{
promise_all.setValue(FutureAllValueType());
}
else
{
std::shared_ptr<WhenAllCallback> when_all_callback(new WhenAllCallback(promise_all, std::distance(first, last)));
size_t i;
for (i = 0; first != last; ++first, ++i)
{
std::shared_ptr<detail::VectorParallelCallback<T>> ptr(new detail::VectorParallelCallback<T>(when_all_callback, i));
first->then(std::bind(&detail::VectorParallelCallback<T>::on_future, ptr));
// tars::tc_owned(new detail::VectorParallelCallback<T>(when_all_callback, i))));
}
}
return promise_all.getFuture();
}
} // namespace promise
#endif // __TARS_PROMISE_WHEN_ALL_H__

View File

@ -29,7 +29,7 @@ void usage()
cout << " --unjson not json interface" << endl; cout << " --unjson not json interface" << endl;
cout << " --os only create struct(not create interface) " << endl; cout << " --os only create struct(not create interface) " << endl;
cout << " --include=\"dir1;dir2;dir3\" set search path of tars protocol" << 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 << " --tarsMaster create get registry info interface" << endl;
cout << " --currentPriority use current path first." << endl; cout << " --currentPriority use current path first." << endl;
cout << " tars2cpp support type: bool byte short int long float double vector map" << 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->setHeader(option.getValue("header"));
g_parse->setCurrentPriority(option.hasParam("currentPriority")); 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++) for(size_t i = 0; i < vTars.size(); i++)
{ {

View File

@ -31,7 +31,7 @@ Tars2Cpp::Tars2Cpp()
, _onlyStruct(false) , _onlyStruct(false)
, _bJsonSupport(true) , _bJsonSupport(true)
, _namespace("tars") , _namespace("tars")
, _unknownField(false) // , _unknownField(false)
, _tarsMaster(false) , _tarsMaster(false)
{ {
@ -230,18 +230,18 @@ string Tars2Cpp::readFrom(const TypeIdPtr& pPtr, bool bIsRequire) const
string Tars2Cpp::readUnknown(const TypeIdPtr& pPtr) const // string Tars2Cpp::readUnknown(const TypeIdPtr& pPtr) const
{ // {
ostringstream s; // ostringstream s;
s << TAB << "_is.readUnknown(sUnknownField, " << pPtr->getTag() << ");" << endl; // s << TAB << "_is.readUnknown(sUnknownField, " << pPtr->getTag() << ");" << endl;
return s.str(); // return s.str();
} // }
string Tars2Cpp::writeUnknown() const // string Tars2Cpp::writeUnknown() const
{ // {
ostringstream s; // ostringstream s;
s << TAB << "_os.writeUnknown(sUnknownField);" << endl; // s << TAB << "_os.writeUnknown(sUnknownField);" << endl;
return s.str(); // return s.str();
} // }
string Tars2Cpp::display(const TypeIdPtr& pPtr) const string Tars2Cpp::display(const TypeIdPtr& pPtr) const
{ {
@ -568,14 +568,14 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con
} }
} }
if (b) // if (b)
{ // {
if (_unknownField) // if (_unknownField)
{ // {
s << ",sUnknownField(\"\")"; // s << ",sUnknownField(\"\")";
} // }
s << endl; // s << endl;
} // }
s << TAB << "{" << endl; s << TAB << "{" << endl;
INC_TAB; INC_TAB;
@ -656,10 +656,10 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con
{ {
s << writeTo(member[j]); s << writeTo(member[j]);
} }
if (_unknownField) // if (_unknownField)
{ // {
s << writeUnknown(); // s << writeUnknown();
} // }
DEL_TAB; DEL_TAB;
s << TAB << "}" << endl; s << TAB << "}" << endl;
@ -673,10 +673,10 @@ string Tars2Cpp::generateH(const StructPtr& pPtr, const string& namespaceId) con
{ {
s << readFrom(member[j]); s << readFrom(member[j]);
} }
if (_unknownField) // if (_unknownField)
{ // {
s << readUnknown(member[member.size() - 1]); // s << readUnknown(member[member.size() - 1]);
} // }
DEL_TAB; DEL_TAB;
s << TAB << "}" << endl; 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; s << TAB << tostr(member[j]->getTypePtr()) << " " << member[j]->getId() << toStrSuffix(member[j]) << ";" << endl;
} }
if (_unknownField) // if (_unknownField)
{ // {
s << TAB << "std::string sUnknownField;" << endl; // s << TAB << "std::string sUnknownField;" << endl;
} // }
DEL_TAB; DEL_TAB;
s << TAB << "};" << endl; s << TAB << "};" << endl;
@ -1376,6 +1376,7 @@ string Tars2Cpp::generateServantDispatch(const OperationPtr& pPtr, const string&
return s.str(); return s.str();
} }
/*
string Tars2Cpp::promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire) const string Tars2Cpp::promiseReadFrom(const TypeIdPtr &pPtr, bool bIsRequire) const
{ {
ostringstream s; ostringstream s;
@ -1442,6 +1443,7 @@ bool Tars2Cpp::isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const
} }
return false; return false;
} }
*/
string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) const string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) const
{ {
ostringstream s; ostringstream s;
@ -1502,7 +1504,7 @@ string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) cons
DEL_TAB; DEL_TAB;
s << TAB << "}" << endl; s << TAB << "}" << endl;
s << TAB << endl; s << TAB << endl;
/*
//promise异步的函数声明 //promise异步的函数声明
string sStruct = pPtr->getId(); string sStruct = pPtr->getId();
@ -1559,7 +1561,7 @@ string Tars2Cpp::generateHAsync(const OperationPtr& pPtr, const string& cn) cons
DEL_TAB; DEL_TAB;
s << TAB << "}" << endl; s << TAB << "}" << endl;
s << endl; s << endl;
*/
//协程并行异步的函数声明 //协程并行异步的函数声明
s << TAB << "void coro_" << pPtr->getId() << "("; s << TAB << "void coro_" << pPtr->getId() << "(";
s << cn << "CoroPrxCallbackPtr callback,"; s << cn << "CoroPrxCallbackPtr callback,";
@ -1811,6 +1813,7 @@ struct SortOperation {
} }
}; };
/*
string Tars2Cpp::generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const string Tars2Cpp::generateHPromiseAsync(const InterfacePtr &pInter, const OperationPtr &pPtr) const
{ {
ostringstream s; ostringstream s;
@ -1972,6 +1975,7 @@ string Tars2Cpp::generateDispatchPromiseAsync(const OperationPtr &pPtr, const st
return s.str(); return s.str();
} }
*/
/******************************InterfacePtr***************************************/ /******************************InterfacePtr***************************************/
string Tars2Cpp::generateH(const InterfacePtr &pPtr, const NamespacePtr &nPtr) const 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 << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "PrxCallback> " << pPtr->getId() << "PrxCallbackPtr;" << endl;
s << endl; s << endl;
/*
//生成promise异步回调Proxy //生成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 << "class " << pPtr->getId() << "PrxCallbackPromise: public tars::ServantProxyCallback" << endl;
s << TAB << "{" << endl; s << TAB << "{" << endl;
s << TAB << "public:" << 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 << TAB << "typedef tars::TC_AutoPtr<" << pPtr->getId() << "PrxCallbackPromise> " << pPtr->getId() << "PrxCallbackPromisePtr;" << endl;
s << endl; s << endl;
*/
//生成协程异步回调类,用于并发请求 //生成协程异步回调类,用于并发请求
s << TAB << "/* callback of coroutine async proxy for client */" << endl; s << TAB << "/* callback of coroutine async proxy for client */" << endl;
s << TAB << "class " << pPtr->getId() << "CoroPrxCallback: public " << pPtr->getId() << "PrxCallback" << 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/ServantProxy.h\"" << endl;
s << "#include \"servant/Servant.h\"" << endl; s << "#include \"servant/Servant.h\"" << endl;
s << "#include \"promise/promise.h\"" << endl; // s << "#include \"promise/promise.h\"" << endl;
break; break;
} }
} }

View File

@ -64,7 +64,7 @@ public:
/** /**
* setUnknownField for UnknownField support * setUnknownField for UnknownField support
*/ */
void setUnknownField(bool bUnkownField) { _unknownField = bUnkownField; } // void setUnknownField(bool bUnkownField) { _unknownField = bUnkownField; }
/** /**
* added for master info * added for master info
@ -113,7 +113,7 @@ protected:
* *
* @return string * @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 * @return string
*/ */
string readUnknown(const TypeIdPtr &pPtr) const; // string readUnknown(const TypeIdPtr &pPtr) const;
/** /**
* *
* @param pPtr * @param pPtr
* *
* @return string * @return string
*/ */
string writeUnknown()const; // string writeUnknown()const;
/** /**
* *
* @param pPtr * @param pPtr
@ -354,7 +354,7 @@ protected:
* *
* @return string * @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 * @return string
*/ */
string generateDispatchPromiseAsync(const OperationPtr &pPtr, const string &cn) const; // string generateDispatchPromiseAsync(const OperationPtr &pPtr, const string &cn) const;
/** /**
* servant的头文件源码 * servant的头文件源码
@ -440,7 +440,7 @@ protected:
string generateInitValue(const TypeIdPtr &pPtr) const; string generateInitValue(const TypeIdPtr &pPtr) const;
bool isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const; // bool isPromiseDispatchInitValue(const TypeIdPtr &pPtr) const;
private: private:
std::string _baseDir; std::string _baseDir;
@ -453,7 +453,7 @@ private:
std::string _namespace ; std::string _namespace ;
bool _unknownField; // bool _unknownField;
bool _tarsMaster; bool _tarsMaster;
}; };

View File

@ -788,7 +788,7 @@ clean:
TAF_THROW_EXCEPTION_SYSCODE(TC_Socket_Exception, "[TC_Socket::createPipe] error"); TAF_THROW_EXCEPTION_SYSCODE(TC_Socket_Exception, "[TC_Socket::createPipe] error");
} }
#if TARGET_PLATFORM_LINUX||TARGET_PLATFORM_IOS #if TARGET_PLATFORM_LINUX
vector<string> TC_Socket::getLocalHosts(int domain) vector<string> TC_Socket::getLocalHosts(int domain)
{ {
vector<string> result; vector<string> result;
@ -858,6 +858,35 @@ vector<string> TC_Socket::getLocalHosts(int domain)
return result; return result;
} }
#elif TARGET_PLATFORM_IOS
vector<string> TC_Socket::getLocalHosts(int domain)
{
vector<string> 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 #endif
#if TARGET_PLATFORM_WINDOWS #if TARGET_PLATFORM_WINDOWS