add examples CMakeLists.txt

This commit is contained in:
jarodruan 2020-01-29 15:52:39 +08:00
parent fedd9f4a71
commit 17df5bba83
58 changed files with 501 additions and 865 deletions

View File

@ -114,11 +114,15 @@ IF(WIN32)
include_directories(${CMAKE_SOURCE_DIR}/util/src/epoll_windows)
ENDIF()
include("${CMAKE_SOURCE_DIR}/cmake/BuildTarsServer.cmake")
include_directories(${CMAKE_SOURCE_DIR}/util/include)
include_directories(${CMAKE_SOURCE_DIR}/servant)
include_directories(${CMAKE_SOURCE_DIR}/servant/protocol)
add_subdirectory(util)
add_subdirectory(tools)
# set(TARS2CPP "${tools_BINARY_DIR}/tars2cpp/tars2cpp")
add_subdirectory(servant)
add_subdirectory(examples)
#add_subdirectory(test_deprecated)

View File

@ -0,0 +1,57 @@
macro(build_tars_server MODULE)
include_directories(./)
aux_source_directory(. DIR_SRCS)
# message("MODULE: ${MODULE}, DIR_SRCS:${DIR_SRCS}")
FILE(GLOB TARS_LIST "${CMAKE_CURRENT_SOURCE_DIR}/*.tars")
# message("TARS_LIST:${TARS_LIST}")
set(TARS_LIST_DEPENDS)
if (TARS_LIST)
set(CLEAN_LIST)
foreach (TARS_SRC ${TARS_LIST})
get_filename_component(NAME_WE ${TARS_SRC} NAME_WE)
set(TARS_H ${NAME_WE}.h)
set(CUR_TARS_GEN ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H})
LIST(APPEND TARS_LIST_DEPENDS ${CUR_TARS_GEN})
# message("TARS_H:${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H}")
add_custom_command(OUTPUT ${CUR_TARS_GEN}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${TARS2CPP}
COMMAND ${TARS2CPP} ${TARS_SRC}
COMMENT "${TARS2CPP} ${TARS_SRC}")
list(APPEND CLEAN_LIST ${CMAKE_CURRENT_SOURCE_DIR}/${TARS_H})
endforeach ()
set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${CLEAN_LIST}")
set(TARS_TARGET "TARS_${MODULE}")
add_custom_target(${TARS_TARGET} ALL DEPENDS ${TARS_LIST_DEPENDS})
add_executable(${MODULE} ${DIR_SRCS})
add_dependencies(${MODULE} ${TARS_TARGET})
else(TARS_LIST)
add_executable(${MODULE} ${DIR_SRCS})
endif (TARS_LIST)
add_dependencies(${MODULE} tarsutil tarsservant)
target_link_libraries(${MODULE} tarsutil tarsservant)
endmacro()

View File

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

View File

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

View File

@ -0,0 +1,7 @@
add_subdirectory(AServer)
add_subdirectory(BServer)
add_subdirectory(client)
add_subdirectory(testCoro)
add_subdirectory(testParallelCoro)

View File

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

View File

@ -129,7 +129,7 @@ int main(int argc,char ** argv)
cout << "********************" <<endl;
}
tp.wait();
tp.waitForAllDone();
}
catch(exception &e)
{

View File

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

View File

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

View File

@ -57,133 +57,133 @@ public:
};
typedef tars::TC_AutoPtr<BServantCoroCallback> BServantCoroCallbackPtr;
//自定义协程类
class CoroutineClass : public TC_Thread
{
public:
/**
*
*/
CoroutineClass();
// //自定义协程类
// class CoroutineClass : public TC_Thread
// {
// public:
// /**
// * 构造函数
// */
// CoroutineClass();
/**
*
*/
virtual ~CoroutineClass();
// /**
// * 析构函数
// */
// virtual ~CoroutineClass();
/**
* 0-1
*/
int registerFunc(const vector< tars::TC_Callback<void ()> > &vFunc);
// /**
// * 返回0代表成功-1表示失败
// */
// int registerFunc(const vector< std::function<void ()> > &vFunc);
/**
* 线
*/
virtual void initialize() {}
// /**
// * 线程初始化
// */
// virtual void initialize() {}
/**
* 线
*/
virtual void run();
// /**
// * 线程处理方法
// */
// virtual void run();
/**
* 线
*/
void terminate();
// /**
// * 停止线程
// */
// void terminate();
protected:
/**
* 线,
*/
virtual void startCoro() {}
// protected:
// /**
// * 线程已经启动, 进入具体协程处理前调用
// */
// virtual void startCoro() {}
/**
* 线退
*/
virtual void stopCoro() {}
// /**
// * 线程马上要退出时调用
// */
// virtual void stopCoro() {}
/**
*
*/
virtual void handleCoro();
// /**
// * 具体的处理逻辑
// */
// virtual void handleCoro();
protected:
CoroutineScheduler *_coroSched;
uint32_t _iPoolSize;
size_t _iStackSize;
vector< tars::TC_Callback<void ()> > _vFunc;
};
// protected:
// CoroutineScheduler *_coroSched;
// uint32_t _iPoolSize;
// size_t _iStackSize;
// vector<std::function<void ()> > _vFunc;
// };
CoroutineClass::CoroutineClass()
: _coroSched(NULL)
, _iPoolSize(1024)
, _iStackSize(128*1024)
{
}
// CoroutineClass::CoroutineClass()
// : _coroSched(NULL)
// , _iPoolSize(1024)
// , _iStackSize(128*1024)
// {
// }
CoroutineClass::~CoroutineClass()
{
if(isAlive())
{
terminate();
// CoroutineClass::~CoroutineClass()
// {
// if(isAlive())
// {
// terminate();
getThreadControl().join();
}
}
// getThreadControl().join();
// }
// }
int CoroutineClass::registerFunc(const vector< tars::TC_Callback<void ()> > &vFunc)
{
if(vFunc.size() > _iPoolSize || vFunc.size() <= 0)
{
return -1;
}
// int CoroutineClass::registerFunc(const vector< std::function<void ()> > &vFunc)
// {
// if(vFunc.size() > _iPoolSize || vFunc.size() <= 0)
// {
// return -1;
// }
_vFunc = vFunc;
// _vFunc = vFunc;
return 0;
}
// return 0;
// }
void CoroutineClass::run()
{
initialize();
// void CoroutineClass::run()
// {
// initialize();
startCoro();
// startCoro();
handleCoro();
// handleCoro();
stopCoro();
}
// stopCoro();
// }
void CoroutineClass::terminate()
{
if(_coroSched)
{
_coroSched->terminate();
}
}
// void CoroutineClass::terminate()
// {
// if(_coroSched)
// {
// _coroSched->terminate();
// }
// }
void CoroutineClass::handleCoro()
{
_coroSched = new CoroutineScheduler();
// void CoroutineClass::handleCoro()
// {
// _coroSched = new CoroutineScheduler();
_coroSched->init(_iPoolSize, _iStackSize);
// _coroSched->init(_iPoolSize, _iStackSize);
ServantProxyThreadData * pSptd = ServantProxyThreadData::getData();
// ServantProxyThreadData * pSptd = ServantProxyThreadData::getData();
assert(pSptd != NULL);
// assert(pSptd != NULL);
pSptd->_sched = _coroSched;
// pSptd->_sched = _coroSched;
for(size_t i = 0; i < _vFunc.size(); ++i)
{
_coroSched->createCoroutine(_vFunc[i]);
}
// for(size_t i = 0; i < _vFunc.size(); ++i)
// {
// _coroSched->createCoroutine(_vFunc[i]);
// }
_coroSched->run();
// _coroSched->run();
delete _coroSched;
_coroSched = NULL;
}
// delete _coroSched;
// _coroSched = NULL;
// }
////////////////////////////////////////////
//继承框架的协程类

View File

@ -0,0 +1,4 @@
add_subdirectory(HttpClient)
add_subdirectory(HttpServer)

View File

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

View File

@ -161,7 +161,7 @@ int main(int argc,char ** argv)
tp.exec(fwrapper3);
cout << "********************" <<endl;
}
tp.wait();
tp.waitForAllDone();
}catch(exception &e)
{
cout<<e.what()<<endl;

View File

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

View File

@ -21,44 +21,42 @@ using namespace std;
HttpServer g_app;
/////////////////////////////////////////////////////////////////
struct HttpProtocol
{
/**
* http请求
* @param in
* @param out
*
* @return int
*/
static int parseHttp(string &in, string &out)
{
try
{
//判断请求是否是HTTP请求
bool b = TC_HttpRequest ::checkRequest(in.c_str(), in.length());
//完整的HTTP请求
if(b)
{
out = in;
in = "";
//TLOGDEBUG("out size: " << out.size() << endl);
return TC_EpollServer::PACKET_FULL;
}
else
{
return TC_EpollServer::PACKET_LESS;
}
}
catch(exception &ex)
{
return TC_EpollServer::PACKET_ERR;
}
// /////////////////////////////////////////////////////////////////
// struct HttpProtocol
// {
// /**
// * http协议解析
// * @param in
// * @param out
// *
// * @return int
// */
// static int parseHttp(TC_NetWorkBuffer &in, vector<char> &out)
// {
// try
// {
// bool b = in.checkRequest(in.length());
// if(b)
// {
// out = in;
// in.clearBuffers();
// //TLOGDEBUG("out size: " << out.size() << endl);
// return TC_EpollServer::PACKET_FULL;
// }
// else
// {
// return TC_EpollServer::PACKET_LESS;
// }
// }
// catch(exception &ex)
// {
// return TC_EpollServer::PACKET_ERR;
// }
return TC_EpollServer::PACKET_LESS; //表示收到的包不完全
}
// return TC_EpollServer::PACKET_LESS; //<2F><>ʾ<EFBFBD>յ<EFBFBD><D5B5>İ<EFBFBD><C4B0><EFBFBD><EFBFBD><EFBFBD>ȫ
// }
};
// };
void
HttpServer::initialize()
@ -67,7 +65,7 @@ HttpServer::initialize()
//...
addServant<HttpImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj");
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&HttpProtocol::parseHttp);
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&TC_NetWorkBuffer::parseHttp);
}
/////////////////////////////////////////////////////////////////
void

View File

@ -31,7 +31,7 @@ public:
: _current(current)
{}
BServantCallback(TarsCurrentPtr &current, const promise::Promise<std::string> &promise)
BServantCallback(TarsCurrentPtr &current, const tars::Promise<std::string> &promise)
: _current(current)
, _promise(promise)
{}
@ -60,7 +60,7 @@ private:
s += "|ret:";
s += TC_Common::tostr(ret);
_promise.setException(promise::copyException(s));
_promise.setException(tars::copyException(s));
TLOGDEBUG("ServerPrxCallback handExp:" << s << endl);
}
@ -68,7 +68,7 @@ private:
private:
TarsCurrentPtr _current;
promise::Promise<std::string> _promise;
tars::Promise<std::string> _promise;
};
//////////////////////////////////////////////////////
class CServantCallback : public CServantPrxCallback
@ -79,7 +79,7 @@ public:
: _current(current)
{}
CServantCallback(TarsCurrentPtr &current, const promise::Promise<std::string> &promise)
CServantCallback(TarsCurrentPtr &current, const tars::Promise<std::string> &promise)
: _current(current)
, _promise(promise)
{}
@ -108,7 +108,7 @@ private:
s += "|ret:";
s += TC_Common::tostr(ret);
_promise.setException(promise::copyException(s));
_promise.setException(tars::copyException(s));
TLOGDEBUG("ServerPrxCallback handExp:" << s << endl);
}
@ -116,12 +116,12 @@ private:
private:
TarsCurrentPtr _current;
promise::Promise<std::string> _promise;
tars::Promise<std::string> _promise;
};
//////////////////////////////////////////////////////
promise::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
tars::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
{
promise::Promise<std::string> promise;
tars::Promise<std::string> promise;
Test::BServantPrxCallbackPtr cb = new BServantCallback(current, promise);
@ -130,9 +130,9 @@ promise::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, t
return promise.getFuture();
}
//////////////////////////////////////////////////////
promise::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
tars::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current)
{
promise::Promise<std::string> promise;
tars::Promise<std::string> promise;
Test::CServantPrxCallbackPtr cb = new CServantCallback(current, promise);
@ -141,7 +141,7 @@ promise::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, t
return promise.getFuture();
}
//////////////////////////////////////////////////////
promise::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future<std::string>& future)
tars::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future<std::string>& future)
{
std::string sResult("");
std::string sException("");
@ -157,13 +157,13 @@ promise::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentP
sException = e.what();
}
promise::Promise<std::string> promise;
tars::Promise<std::string> promise;
promise.setValue(sException);
return promise.getFuture();
}
//////////////////////////////////////////////////////
int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<std::string>& future)
int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future<std::string>& future)
{
int ret = 0;
std::string sResult("");
@ -184,16 +184,16 @@ int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<std:
return 0;
}
//////////////////////////////////////////////////////
int handleBCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > >& allFuture)
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
{
promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > tupleFuture = allFuture.get();
std::tuple<tars::Future<std::string>, tars::Future<std::string> > tupleFuture = allFuture.get();
std::string sResult1 = tupleFuture.get<0>().get();
std::string sResult2 = tupleFuture.get<1>().get();
std::string sResult1 = std::get<0>(tupleFuture).get();
std::string sResult2 = std::get<1>(tupleFuture).get();
sResult = sResult1;
sResult += "|";
@ -223,15 +223,30 @@ void AServantImp::initialize()
void AServantImp::destroy()
{
}
// class Test1
// {
// public:
// template <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);
promise::Future<std::string> f = sendBReq(_pPrxB, sIn, current);
tars::Future<std::string> f = sendBReq(_pPrxB, sIn, current);
f.then(tars::TC_Bind(&handleBRspAndSendCReq, _pPrxC, current)).then(tars::TC_Bind(&handleCRspAndReturnClient, current));
auto b1 = std::bind(handleBRspAndSendCReq, _pPrxC, current);//, std::placeholders::_3);
auto b2 = std::bind(handleCRspAndReturnClient, current);//, std::placeholders::_2);
f.then(b1).then(b2);
// Test1 t;
// t.then<std::string>(b1);
return 0;
}
//////////////////////////////////////////////////////
@ -239,13 +254,13 @@ tars::Int32 AServantImp::queryResultParallel(const std::string& sIn, std::string
{
current->setResponse(false);
promise::Future<std::string> f1 = sendBReq(_pPrxB, sIn, current);
tars::Future<std::string> f1 = sendBReq(_pPrxB, sIn, current);
promise::Future<std::string> f2 = sendCReq(_pPrxC, sIn, current);
tars::Future<std::string> f2 = sendCReq(_pPrxC, sIn, current);
promise::Future<promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > > f_all = promise::whenAll(f1, f2);
tars::Future<std::tuple<tars::Future<std::string>, tars::Future<std::string> > > f_all = tars::whenAll(f1, f2);
f_all.then(tars::TC_Bind(&handleBCRspAndReturnClient, current));
f_all.then(std::bind(&handleBCRspAndReturnClient, current));
return 0;
}

View File

@ -29,16 +29,16 @@
using namespace Test;
///////////////////////////////////
promise::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current);
tars::Future<std::string> sendBReq(BServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current);
promise::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const promise::Future<std::string>& future);
tars::Future<std::string> handleBRspAndSendCReq(CServantPrx prx, TarsCurrentPtr current, const tars::Future<std::string>& future);
promise::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current);
tars::Future<std::string> sendCReq(CServantPrx prx, const std::string& sIn, tars::TarsCurrentPtr current);
int handleCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<std::string>& future);
int handleCRspAndReturnClient(TarsCurrentPtr current, const tars::Future<std::string>& future);
///////////////////////////////////
int handleBCRspAndReturnClient(TarsCurrentPtr current, const promise::Future<promise::Tuple<promise::Future<std::string>, promise::Future<std::string> > >& allFuture);
int handleBCRspAndReturnClient(TarsCurrentPtr current, const tars::Future<std::tuple<tars::Future<std::string>, tars::Future<std::string> > >& allFuture);
///////////////////////////////////
class AServantImp : public Test::AServant

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,4 @@
add_subdirectory(PushClient)
add_subdirectory(PushServer)

View File

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

View File

@ -5,71 +5,93 @@
/*
ResponsePacket
*/
static size_t pushResponse(const char* recvBuffer, size_t length, list<ResponsePacket>& done)
static TC_NetWorkBuffer::PACKET_TYPE pushResponse(TC_NetWorkBuffer &in, ResponsePacket& done)
{
size_t pos = 0;
while (pos < length)
{
unsigned int len = length - pos;
if(len < sizeof(unsigned int))
{
break;
}
size_t len = sizeof(tars::Int32);
unsigned int iHeaderLen = ntohl(*(unsigned int*)(recvBuffer + pos));
if (in.getBufferLength() < len)
{
return TC_NetWorkBuffer::PACKET_LESS;
}
//做一下保护,长度大于M
if (iHeaderLen > 100000 || iHeaderLen < sizeof(unsigned int))
{
throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
}
string header;
in.getHeader(len, header);
//包没有接收全
if (len < iHeaderLen)
{
break;
}
else
{
ResponsePacket rsp;
rsp.iRequestId = ntohl(*((unsigned int *)(recvBuffer + pos + sizeof(unsigned int))));
rsp.sBuffer.resize(iHeaderLen - 2*sizeof(unsigned int));
::memcpy(&rsp.sBuffer[0], recvBuffer + pos + 2*sizeof(unsigned int), iHeaderLen - 2*sizeof(unsigned int));
assert(header.size() == len);
pos += iHeaderLen;
tars::Int32 iHeaderLen = 0;
done.push_back(rsp);
}
}
::memcpy(&iHeaderLen, header.c_str(), sizeof(tars::Int32));
return pos;
iHeaderLen = ntohl(iHeaderLen);
//做一下保护,长度大于M
if (iHeaderLen > 100000 || iHeaderLen < sizeof(unsigned int))
{
throw TarsDecodeException("packet length too long or too short,len:" + TC_Common::tostr(iHeaderLen));
}
//包没有接收全
if (in.getBufferLength() < (uint32_t)iHeaderLen)
{
return TC_NetWorkBuffer::PACKET_LESS;
}
tars::Int32 iRequestId = 0;
string sRequestId;
in.getHeader(sizeof(iHeaderLen), sRequestId);
ResponsePacket rsp;
rsp.iRequestId = ntohl(*((unsigned int *)(sRequestId.c_str())));
in.getHeader(iHeaderLen - sizeof(iHeaderLen) - sizeof(iRequestId), rsp.sBuffer);
return TC_NetWorkBuffer::PACKET_FULL;
}
/*
+iRequestId+
*/
static void pushRequest(const RequestPacket& request, string& buff)
static void pushRequest(const RequestPacket& request, shared_ptr<TC_NetWorkBuffer::SendBuffer>& buff)
{
unsigned int net_bufflength = htonl(request.sBuffer.size()+8);
unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength);
// unsigned int net_bufflength = htonl(request.sBuffer.size()+8);
// unsigned char * bufflengthptr = (unsigned char*)(&net_bufflength);
buff = "";
for (int i = 0; i<4; ++i)
{
buff += *bufflengthptr++;
}
// buff = "";
// for (int i = 0; i<4; ++i)
// {
// buff += *bufflengthptr++;
// }
unsigned int netrequestId = htonl(request.iRequestId);
unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId);
// unsigned int netrequestId = htonl(request.iRequestId);
// unsigned char * netrequestIdptr = (unsigned char*)(&netrequestId);
for (int i = 0; i<4; ++i)
{
buff += *netrequestIdptr++;
}
// for (int i = 0; i<4; ++i)
// {
// buff += *netrequestIdptr++;
// }
string tmp;
tmp.assign((const char*)(&request.sBuffer[0]), request.sBuffer.size());
buff+=tmp;
// string tmp;
// tmp.assign((const char*)(&request.sBuffer[0]), request.sBuffer.size());
// buff+=tmp;
TarsOutputStream<BufferWriterVector> os;
tars::Int32 iHeaderLen = 0;
tars::Int32 iRequestId = request.iRequestId;
// 先预留8个字节长度
os.writeBuf((const char *)&iHeaderLen, sizeof(iHeaderLen));
os.writeBuf((const char *)&iRequestId, sizeof(iRequestId));
request.writeTo(os);
buff->swap(os.getByteBuffer());
// assert(buff->length() >= 4);
iHeaderLen = htonl((int)(buff->length()));
memcpy((void*)buff->buffer(), (const char *)&iHeaderLen, sizeof(iHeaderLen));
}
static void printResult(int iRequestId, const string &sResponseStr)
@ -87,15 +109,15 @@ int TestPushCallBack::onDispatch(ReqMessagePtr msg)
if(msg->request.sFuncName == "printResult")
{
string sRet;
cout << "sBuffer: " << msg->response.sBuffer.size() << endl;
sRet.assign(&(msg->response.sBuffer[0]), msg->response.sBuffer.size());
cout << "sBuffer: " << msg->response->sBuffer.size() << endl;
sRet.assign(&(msg->response->sBuffer[0]), msg->response->sBuffer.size());
printResult(msg->request.iRequestId, sRet);
return 0;
}
else if(msg->response.iRequestId == 0)
else if(msg->response->iRequestId == 0)
{
string sRet;
sRet.assign(&(msg->response.sBuffer[0]), msg->response.sBuffer.size());
sRet.assign(&(msg->response->sBuffer[0]), msg->response->sBuffer.size());
printPushInfo(sRet);
return 0;
}

View File

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

View File

@ -7,37 +7,6 @@ TestPushServer g_app;
/////////////////////////////////////////////////////////////////
static int parse(string &in, string &out)
{
if(in.length() < sizeof(unsigned int))
{
return TC_EpollServer::PACKET_LESS;
}
unsigned int iHeaderLen;
memcpy(&iHeaderLen, in.c_str(), sizeof(unsigned int));
iHeaderLen = ntohl(iHeaderLen);
if(iHeaderLen < (unsigned int)(sizeof(unsigned int))|| iHeaderLen > 1000000)
{
return TC_EpollServer::PACKET_ERR;
}
if((unsigned int)in.length() < iHeaderLen)
{
return TC_EpollServer::PACKET_LESS;
}
out = in.substr(0, iHeaderLen);
in = in.substr(iHeaderLen);
return TC_EpollServer::PACKET_FULL;
}
void
TestPushServer::initialize()
{
@ -46,7 +15,7 @@ TestPushServer::initialize()
addServant<TestPushServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".TestPushServantObj");
addServantProtocol("Test.TestPushServer.TestPushServantObj", parse);
addServantProtocol("Test.TestPushServer.TestPushServantObj", TC_NetWorkBuffer::parseBinary4<8, 1024*1024*10>);
pushThread.start();

View File

@ -0,0 +1,5 @@
include_directories(HelloServer/Server)
add_subdirectory(HelloServer)
add_subdirectory(ProxyServer)

View File

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

View File

@ -0,0 +1,5 @@
include_directories(Server)
add_subdirectory(Server)
add_subdirectory(Client)
add_subdirectory(AsyncClient)

View File

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

View File

@ -1,26 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
module TestApp
{
interface Hello
{
int test();
int testHello(string sReq, out string sRsp);
};
};

View File

@ -1,42 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "HelloImp.h"
#include "servant/Application.h"
using namespace std;
//////////////////////////////////////////////////////
void HelloImp::initialize()
{
//initialize servant here:
//...
}
//////////////////////////////////////////////////////
void HelloImp::destroy()
{
//destroy servant here:
//...
}
int HelloImp::testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current)
{
TLOGDEBUG("HelloImp::testHellosReq:"<<sReq<<endl);
sRsp = sReq;
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 _HelloImp_H_
#define _HelloImp_H_
#include "servant/Application.h"
#include "Hello.h"
/**
*
*
*/
class HelloImp : public TestApp::Hello
{
public:
/**
*
*/
virtual ~HelloImp() {}
/**
*
*/
virtual void initialize();
/**
*
*/
virtual void destroy();
/**
*
*/
virtual int test(tars::TarsCurrentPtr current) { return 0;};
virtual int testHello(const std::string &sReq, std::string &sRsp, tars::TarsCurrentPtr current);
};
/////////////////////////////////////////////////////
#endif

View File

@ -1,59 +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 "HelloServer.h"
#include "HelloImp.h"
using namespace std;
HelloServer g_app;
/////////////////////////////////////////////////////////////////
void
HelloServer::initialize()
{
//initialize application here:
//...
addServant<HelloImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".HelloObj");
}
/////////////////////////////////////////////////////////////////
void
HelloServer::destroyApp()
{
//destroy application here:
//...
}
/////////////////////////////////////////////////////////////////
int
main(int argc, char* argv[])
{
try
{
g_app.main(argc, argv);
g_app.waitForShutdown();
}
catch (std::exception& e)
{
cerr << "std::exception:" << e.what() << std::endl;
}
catch (...)
{
cerr << "unknown exception." << std::endl;
}
return -1;
}
/////////////////////////////////////////////////////////////////

View File

@ -1,50 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#ifndef _HelloServer_H_
#define _HelloServer_H_
#include <iostream>
#include "servant/Application.h"
using namespace tars;
/**
*
**/
class HelloServer : public Application
{
public:
/**
*
**/
virtual ~HelloServer() {};
/**
*
**/
virtual void initialize();
/**
*
**/
virtual void destroyApp();
};
extern HelloServer g_app;
////////////////////////////////////////////
#endif

View File

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

View File

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

View File

@ -0,0 +1,5 @@
include_directories(Server)
add_subdirectory(Server)
add_subdirectory(Client)

View File

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

View File

@ -1,26 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
module TestApp
{
interface Proxy
{
int test();
int testProxy(string sReq, out string sRsp);
};
};

View File

@ -1,83 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "ProxyImp.h"
#include "ProxyServer.h"
using namespace std;
using namespace tars;
class HelloCallback : public HelloPrxCallback
{
public:
HelloCallback(TarsCurrentPtr &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

@ -1,63 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#ifndef __PROXY_IMP_H_
#define __PROXY_IMP_H_
#include "servant/Application.h"
#include "Hello.h"
#include "Proxy.h"
using namespace TestApp;
/**
*
*
*/
class ProxyImp : public TestApp::Proxy
{
public:
/**
*
*/
virtual ~ProxyImp() {}
/**
*
*/
virtual void initialize();
/**
*
*/
virtual void destroy();
/**
*
*/
virtual tars::Int32 test(tars::TarsCurrentPtr current);
/**
*
*/
virtual tars::Int32 testProxy(const std::string& sReq, std::string &sRsp, tars::TarsCurrentPtr current);
private:
HelloPrx _prx;
};
/////////////////////////////////////////////////////
#endif

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 "ProxyServer.h"
#include "ProxyImp.h"
using namespace std;
ProxyServer g_app;
/////////////////////////////////////////////////////////////////
void ProxyServer::initialize()
{
//initialize application here:
addServant<ProxyImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".ProxyObj");
}
/////////////////////////////////////////////////////////////////
void ProxyServer::destroyApp()
{
//destroy application here:
//...
}
/////////////////////////////////////////////////////////////////
int
main(int argc, char* argv[])
{
try
{
g_app.main(argc, argv);
g_app.waitForShutdown();
}
catch (std::exception& e)
{
cerr << "std::exception:" << e.what() << std::endl;
}
catch (...)
{
cerr << "unknown exception." << std::endl;
}
return -1;
}
/////////////////////////////////////////////////////////////////

View File

@ -1,52 +0,0 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#ifndef __PROXY_SERVER_H_
#define __PROXY_SERVER_H_
#include <iostream>
#include "servant/Application.h"
using namespace tars;
/**
*
**/
class ProxyServer : public Application
{
public:
/**
*
**/
virtual ~ProxyServer() {};
/**
*
**/
virtual void initialize();
/**
*
**/
virtual void destroyApp();
protected:
};
extern ProxyServer g_app;
////////////////////////////////////////////
#endif

View File

@ -0,0 +1,3 @@
build_tars_server("ProxyServer")

View File

@ -1,17 +0,0 @@
#-----------------------------------------------------------------------
APP := TestApp
TARGET := ProxyServer
CONFIG :=
STRIP_FLAG:= N
TARS2CPP_FLAG:=
INCLUDE +=
LIB +=
#-----------------------------------------------------------------------
include /home/tarsproto/TestApp/HelloServer/HelloServer.mk
include /usr/local/tars/cpp/makefile/makefile.tars
#-----------------------------------------------------------------------

View File

@ -0,0 +1,5 @@
include_directories(TarsStressServer)
add_subdirectory(TarsStressServer)
add_subdirectory(TarsStressClient)

View File

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

View File

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

View File

@ -61,6 +61,8 @@ static BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
}
#endif
std::string ServerConfig::TarsPath; //服务路径
std::string ServerConfig::Application; //应用名称
std::string ServerConfig::ServerName; //服务名称,一个服务名称含一个或多个服务标识
std::string ServerConfig::LocalIp; //本机IP
@ -1238,7 +1240,7 @@ void Application::initializeServer()
if(!ServerConfig::Local.empty())
{
ServantHelperManager::getInstance()->addServant<AdminServant>("AdminObj");
ServantHelperManager::getInstance()->addServant<AdminServant>("AdminObj", this);
ServantHelperManager::getInstance()->setAdapterServant("AdminAdapter", "AdminObj");

View File

@ -47,6 +47,16 @@ string Servant::getName() const
return _name;
}
void Servant::setApplication(Application *application)
{
_application = application;
}
Application* Servant::getApplication() const
{
return _application;
}
void Servant::setHandle(TC_EpollServer::Handle* handle)
{
_handle = handle;

View File

@ -48,7 +48,7 @@ void ServantHelperManager::setAdapterServant(const string &sAdapter, const strin
bool ServantHelperManager::setDyeing(const string & sDyeingKey, const string & sDyeingServant,
const string & sDyeingInterface)
{
TC_LockT<TC_ThreadMutex> lock(_mutex);
TC_LockT<TC_SpinLock> lock(_mutex);
_dyeingKey = sDyeingKey;
_dyeingServant = sDyeingServant;
@ -61,7 +61,7 @@ bool ServantHelperManager::setDyeing(const string & sDyeingKey, const string & s
bool ServantHelperManager::isDyeingReq(const string & sKey, const string & sServant, const string & sInterface)
{
TC_LockT<TC_ThreadMutex> lock(_mutex);
TC_LockT<TC_SpinLock> lock(_mutex);
return ((_dyeingKey == sKey) && (_dyeingServant == sServant) &&
(_dyeingInterface == "" || _dyeingInterface == sInterface) );

View File

@ -746,21 +746,39 @@ public:
virtual ~Future() {}
template <typename R>
Future<typename detail::resolved_type<R>::type> then(const std::function<R(const Future&)>& callback) const
Future<R> then(const std::function<R(const Future<T>&)>& callback) const
{
typedef typename detail::resolved_type<R>::type value_type;
// 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))));
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)
@ -784,7 +802,7 @@ public:
virtual ~Future() {}
template <typename R>
Future<typename detail::resolved_type<R>::type> then(const std::function<R(const Future&)>& callback) const
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;

View File

@ -56,10 +56,10 @@ namespace tars
};
// FutureAllValueType is supposed to be of type promise::std::tuple<promise::Future, ...> or
// std::vector<promise::Future>.
// FutureAllValueType is supposed to be of type tars::std::tuple<tars::Future, ...> or
// std::vector<tars::Future>.
//
// This implementation is for promise::std::tuple<>.
// This implementation is for tars::std::tuple<>.
template <typename FutureAllValueType>
class ParallelAllCallback : private ParallelAllCallbackBase<FutureAllValueType>
{
@ -88,7 +88,7 @@ namespace tars
}
catch (...)
{
this->m_promise_all.setException(promise::currentException());
this->m_promise_all.setException(currentException());
}
return;
}
@ -100,7 +100,7 @@ namespace tars
};
// This implementation is specialized for std::vector<promise::Future<> >.
// This implementation is specialized for std::vector<tars::Future<> >.
template <typename FutureType>
class ParallelAllCallback<std::vector<FutureType> > : private ParallelAllCallbackBase<std::vector<FutureType> >
{
@ -133,7 +133,7 @@ namespace tars
}
catch (...)
{
this->m_promise_all.setException(promise::currentException());
this->m_promise_all.setException(tars::currentException());
}
return;
}
@ -181,8 +181,8 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
return promise_all.getFuture();
}
@ -196,9 +196,9 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback)));
return promise_all.getFuture();
}
@ -212,10 +212,10 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback)));
future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback)));
future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback)));
return promise_all.getFuture();
}
@ -230,11 +230,11 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback)));
future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback)));
future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback)));
future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback)));
future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback)));
return promise_all.getFuture();
}
@ -249,12 +249,12 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback)));
future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback)));
future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback)));
future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback)));
future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback)));
future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback)));
future6.then(std::bind(&WhenAllCallback::template on_future<5>, (future_callback)));
return promise_all.getFuture();
}
@ -269,13 +269,13 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback)));
future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback)));
future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback)));
future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback)));
future7.then(tars::TC_Bind(&WhenAllCallback::template on_future<6>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback)));
future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback)));
future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback)));
future6.then(std::bind(&WhenAllCallback::template on_future<5>, (future_callback)));
future7.then(std::bind(&WhenAllCallback::template on_future<6>, (future_callback)));
return promise_all.getFuture();
}
@ -290,14 +290,14 @@ namespace tars
PromiseAll promise_all;
std::shared_ptr<WhenAllCallback> future_callback(new WhenAllCallback(promise_all));
future1.then(tars::TC_Bind(&WhenAllCallback::template on_future<0>, tars::tc_shared(future_callback)));
future2.then(tars::TC_Bind(&WhenAllCallback::template on_future<1>, tars::tc_shared(future_callback)));
future3.then(tars::TC_Bind(&WhenAllCallback::template on_future<2>, tars::tc_shared(future_callback)));
future4.then(tars::TC_Bind(&WhenAllCallback::template on_future<3>, tars::tc_shared(future_callback)));
future5.then(tars::TC_Bind(&WhenAllCallback::template on_future<4>, tars::tc_shared(future_callback)));
future6.then(tars::TC_Bind(&WhenAllCallback::template on_future<5>, tars::tc_shared(future_callback)));
future7.then(tars::TC_Bind(&WhenAllCallback::template on_future<6>, tars::tc_shared(future_callback)));
future8.then(tars::TC_Bind(&WhenAllCallback::template on_future<7>, tars::tc_shared(future_callback)));
future1.then(std::bind(&WhenAllCallback::template on_future<0>, (future_callback)));
future2.then(std::bind(&WhenAllCallback::template on_future<1>, (future_callback)));
future3.then(std::bind(&WhenAllCallback::template on_future<2>, (future_callback)));
future4.then(std::bind(&WhenAllCallback::template on_future<3>, (future_callback)));
future5.then(std::bind(&WhenAllCallback::template on_future<4>, (future_callback)));
future6.then(std::bind(&WhenAllCallback::template on_future<5>, (future_callback)));
future7.then(std::bind(&WhenAllCallback::template on_future<6>, (future_callback)));
future8.then(std::bind(&WhenAllCallback::template on_future<7>, (future_callback)));
return promise_all.getFuture();
}
@ -321,8 +321,9 @@ namespace tars
for (i = 0; first != last; ++first, ++i)
{
first->then(tars::TC_Bind(&detail::VectorParallelCallback<T>::on_future,
tars::tc_owned(new detail::VectorParallelCallback<T>(when_all_callback, 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();

View File

@ -27,6 +27,7 @@ namespace tars
{
class BaseNotify;
class Application;
////////////////////////////////////////////////////////////////////
/**
*
@ -62,6 +63,18 @@ public:
*/
void setHandle(TC_EpollServer::Handle* handle);
/**
*
* @param application
*/
void setApplication(Application *application);
/**
*
* @return
*/
Application* getApplication() const;
/**
* Handle
* @return HandlePtr&
@ -166,6 +179,11 @@ protected:
*/
string _name;
/**
*
*/
Application *_application;
/**
* Handle
*/

View File

@ -41,10 +41,13 @@ typedef TC_AutoPtr<ServantHelperCreation> ServantHelperCreationPtr;
/**
* Servant
*/
template<class T>
struct ServantCreation : public ServantHelperCreation
{
ServantPtr create(const string &s) { T *p = new T; p->setName(s); return p; }
ServantCreation(Application *application) : _application(application){}
ServantPtr create(const string &s) { T *p = new T; p->setName(s); p->setApplication(_application); return p; }
Application *_application;
};
//////////////////////////////////////////////////////////////////////////////
@ -69,14 +72,14 @@ public:
* @param id
*/
template<typename T>
void addServant(const string &id,bool check = false)
void addServant(const string &id, Application *application, bool check = false)
{
if(check && _servant_adapter.end() == _servant_adapter.find(id))
{
cerr<<"[TARS]ServantHelperManager::addServant "<< id <<" not find adapter.(maybe not conf in the web)"<<endl;
throw runtime_error("[TARS]ServantHelperManager::addServant " + id + " not find adapter.(maybe not conf in the web)");
cerr<<"[TAF]ServantHelperManager::addServant "<< id <<" not find adapter.(maybe not conf in the web)"<<endl;
throw runtime_error("[TAF]ServantHelperManager::addServant " + id + " not find adapter.(maybe not conf in the web)");
}
_servant_creator[id] = new ServantCreation<T>();
_servant_creator[id] = new ServantCreation<T>(application);
}
/**
@ -99,14 +102,15 @@ public:
* @param sAdapter
* @return string
*/
string getAdapterServant(const string &sAdapter)
const string &getAdapterServant(const string &sAdapter) const
{
map<string, string>::iterator it = _adapter_servant.find(sAdapter);
static const string s = "(NO TARS PROTOCOL)";
auto it = _adapter_servant.find(sAdapter);
if(it != _adapter_servant.end())
{
return it->second;
}
return "(NO TARS PROTOCOL)";
return s;
}
/**
@ -114,21 +118,22 @@ public:
* @param sServant
* @return string
*/
string getServantAdapter(const string& sServant)
const string &getServantAdapter(const string& sServant) const
{
map<string, string>::iterator it = _servant_adapter.find(sServant);
static const string s = "";
auto it = _servant_adapter.find(sServant);
if(it != _servant_adapter.end())
{
return it->second;
}
return "";
return s;
}
/**
* Adapter/Servant对应表
* @return map<string, string>
*/
map<string, string> getAdapterServant() {return _adapter_servant;}
const map<string, string> & getAdapterServant() const {return _adapter_servant;}
/**
*
@ -177,7 +182,7 @@ protected:
/**
*
*/
TC_ThreadMutex _mutex;
TC_SpinLock _mutex;
/**
*

View File

@ -1,7 +1,5 @@
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
include_directories(${PROJECT_SOURCE_DIR}/include)
aux_source_directory(. DIR_SRCS)
# if (CMAKE_SIZEOF_VOID_P EQUAL 8)