mirror of
https://gitee.com/TarsCloud/TarsCpp.git
synced 2024-12-22 22:16:38 +08:00
http2 compiler succ, sync has bug
This commit is contained in:
parent
0350c6cdf8
commit
0d6a4eb76b
@ -4,17 +4,38 @@ project(tars_cpp)
|
||||
|
||||
set(TARS_VERSION "2.0.0")
|
||||
add_definitions(-DTARS_VERSION="${TARS_VERSION}")
|
||||
set(TARS_MYSQL 1)
|
||||
set(TARS_SSL 0)
|
||||
set(TARS_HTTP2 0)
|
||||
set(TARS_PROTOBUF 0)
|
||||
set(TARS_ZLIB 0)
|
||||
|
||||
add_definitions(-DTARS_MYSQL=${TARS_MYSQL})
|
||||
add_definitions(-DTARS_SSL=${TARS_SSL})
|
||||
add_definitions(-DTARS_HTTP2=${TARS_HTTP2})
|
||||
add_definitions(-DTARS_PROTOBUF=${TARS_PROTOBUF})
|
||||
add_definitions(-DTARS_ZLIB=${TARS_ZLIB})
|
||||
option(TARS_MYSQL "option for mysql" ON)
|
||||
option(TARS_SSL "option for ssl" OFF)
|
||||
option(TARS_HTTP2 "option for http2" OFF)
|
||||
option(TARS_PROTOBUF "option for protocol" OFF)
|
||||
option(TARS_ZLIB "option for zip" OFF)
|
||||
|
||||
#set(TARS_MYSQL 1)
|
||||
#set(TARS_SSL 0)
|
||||
#set(TARS_HTTP2 0)
|
||||
#set(TARS_PROTOBUF 0)
|
||||
#set(TARS_ZLIB 0)
|
||||
|
||||
if(TARS_MYSQL)
|
||||
add_definitions(-DTARS_MYSQL=1)
|
||||
endif()
|
||||
|
||||
if(TARS_SSL)
|
||||
add_definitions(-DTARS_SSL=1)
|
||||
endif()
|
||||
|
||||
if(TARS_HTTP2)
|
||||
add_definitions(-DTARS_HTTP2=1)
|
||||
endif()
|
||||
|
||||
if(TARS_PROTOBUF)
|
||||
add_definitions(-DTARS_PROTOBUF=1)
|
||||
endif()
|
||||
|
||||
if(TARS_ZLIB)
|
||||
add_definitions(-DTARS_ZLIB=1)
|
||||
endif()
|
||||
|
||||
# list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules/")
|
||||
set(CMAKE_VERBOSE_MAKEFILE off)
|
||||
@ -56,15 +77,15 @@ endif()
|
||||
|
||||
#-------------------------------------------------------------
|
||||
#set(APP_LIBRARIES)
|
||||
set(OPEN_MYSQL "OFF")
|
||||
set(OPEN_NGHTTP2 "OFF")
|
||||
set(OPEN_SSL "OFF")
|
||||
set(OPEN_ZLIB "OFF")
|
||||
set(OPEN_PROTOBUF "OFF")
|
||||
#set(OPEN_MYSQL "OFF")
|
||||
#set(OPEN_NGHTTP2 "OFF")
|
||||
#set(OPEN_SSL "OFF")
|
||||
#set(OPEN_ZLIB "OFF")
|
||||
#set(OPEN_PROTOBUF "OFF")
|
||||
|
||||
set(THIRDPARTY_PATH "${CMAKE_BINARY_DIR}/src")
|
||||
if(TARS_MYSQL)
|
||||
set(OPEN_MYSQL "ON")
|
||||
# set(OPEN_MYSQL "ON")
|
||||
set(MYSQL_DIR_INC "${THIRDPARTY_PATH}/mysql-lib/include")
|
||||
set(MYSQL_DIR_LIB "${THIRDPARTY_PATH}/mysql-lib/libmysql")
|
||||
include_directories(${MYSQL_DIR_INC})
|
||||
@ -72,7 +93,7 @@ if(TARS_MYSQL)
|
||||
endif()
|
||||
|
||||
if(TARS_PROTOBUF)
|
||||
set(OPEN_PROTOBUF "ON")
|
||||
# set(OPEN_PROTOBUF "ON")
|
||||
set(PROTOBUF_DIR_INC "${THIRDPARTY_PATH}/protobuf-lib/src")
|
||||
set(PROTOBUF_DIR_LIB "${THIRDPARTY_PATH}/protobuf-lib")
|
||||
include_directories(${PROTOBUF_DIR_INC})
|
||||
@ -80,7 +101,7 @@ if(TARS_PROTOBUF)
|
||||
endif()
|
||||
|
||||
if(TARS_ZLIB)
|
||||
set(OPEN_ZLIB "ON")
|
||||
# set(OPEN_ZLIB "ON")
|
||||
set(ZLIB_DIR_INC "${THIRDPARTY_PATH}/z-lib")
|
||||
set(ZLIB_DIR_LIB "${THIRDPARTY_PATH}/z-lib")
|
||||
include_directories(${ZLIB_DIR_INC})
|
||||
@ -88,7 +109,7 @@ if(TARS_ZLIB)
|
||||
endif()
|
||||
|
||||
if(TARS_HTTP2)
|
||||
set(OPEN_NGHTTP2 "ON")
|
||||
# set(OPEN_NGHTTP2 "ON")
|
||||
set(NGHTTP2_DIR_INC "${THIRDPARTY_PATH}/nghttp2-lib/lib/includes/")
|
||||
set(NGHTTP2_DIR_LIB "${THIRDPARTY_PATH}/nghttp2-lib/lib")
|
||||
include_directories(${NGHTTP2_DIR_INC})
|
||||
@ -96,7 +117,7 @@ if(TARS_HTTP2)
|
||||
endif()
|
||||
|
||||
if(TARS_SSL)
|
||||
set(OPEN_SSL "ON")
|
||||
# set(OPEN_SSL "ON")
|
||||
set(SSL_DIR_INC "${THIRDPARTY_PATH}/openssl-lib/include/")
|
||||
set(SSL_DIR_LIB "${THIRDPARTY_PATH}/openssl-lib")
|
||||
include_directories(${SSL_DIR_INC})
|
||||
@ -106,7 +127,7 @@ endif()
|
||||
#-------------------------------------------------------------
|
||||
|
||||
set(LIB_MYSQL)
|
||||
set(LIB_NGHTTP2)
|
||||
set(LIB_HTTP2)
|
||||
set(LIB_SSL)
|
||||
set(LIB_CRYPTO)
|
||||
set(LIB_ZLIB)
|
||||
@ -117,7 +138,7 @@ if(TARS_MYSQL)
|
||||
set(LIB_MYSQL "libmysql")
|
||||
endif()
|
||||
if(TARS_HTTP2)
|
||||
set(LIB_NGHTTP2 "libnghttp2_static")
|
||||
set(LIB_HTTP2 "libnghttp2_static")
|
||||
endif()
|
||||
if(TARS_SSL)
|
||||
set(LIB_SSL "libssl")
|
||||
@ -136,7 +157,7 @@ if(TARS_MYSQL)
|
||||
set(LIB_MYSQL "mysqlclient")
|
||||
endif()
|
||||
if(TARS_HTTP2)
|
||||
set(LIB_NGHTTP2 "nghttp2_static")
|
||||
set(LIB_HTTP2 "nghttp2_static")
|
||||
endif()
|
||||
|
||||
if(TARS_SSL)
|
||||
@ -162,12 +183,12 @@ ENDIF()
|
||||
#link_libraries(${LIB_ZLIB})
|
||||
#endif()
|
||||
#
|
||||
#if(TARS_HTTP2)
|
||||
# list(APPEND APP_LIBRARIES ${LIB_PROTOBUF})
|
||||
#endif()
|
||||
if(TARS_HTTP2)
|
||||
link_libraries(${LIB_HTTP2})
|
||||
endif()
|
||||
#
|
||||
#if(TARS_PROTOBUF)
|
||||
# list(APPEND APP_LIBRARIES ${LIB_NGHTTP2})
|
||||
# list(APPEND APP_LIBRARIES ${LIB_HTTP2})
|
||||
#endif()
|
||||
|
||||
if(TARS_SSL)
|
||||
@ -230,7 +251,7 @@ ExternalProject_Add(${LIB_ZLIB}
|
||||
endif()
|
||||
|
||||
if(TARS_HTTP2)
|
||||
ExternalProject_Add(${LIB_NGHTTP2}
|
||||
ExternalProject_Add(${LIB_HTTP2}
|
||||
URL http://cdn.tarsyun.com/src/nghttp2-1.40.0.tar.gz
|
||||
PREFIX ${CMAKE_BINARY_DIR}
|
||||
INSTALL_DIR ${CMAKE_SOURCE_DIR}
|
||||
@ -263,7 +284,7 @@ ExternalProject_Add(${LIB_SSL}
|
||||
)
|
||||
endif()
|
||||
|
||||
add_custom_target(thirdparty DEPENDS ${LIB_MYSQL} ${LIB_NGHTTP2} ${LIB_SSL})
|
||||
add_custom_target(thirdparty DEPENDS ${LIB_MYSQL} ${LIB_HTTP2} ${LIB_SSL})
|
||||
|
||||
#-------------------------------------------------------------
|
||||
IF (APPLE)
|
||||
@ -320,11 +341,11 @@ message("INSTALL_PREFIX: ${INSTALL_PREFIX}")
|
||||
#-------------------------------------------------------------
|
||||
|
||||
message("----------------------------------------------------")
|
||||
message("OPEN_MYSQL: ${OPEN_MYSQL}")
|
||||
message("OPEN_NGHTTP2: ${OPEN_NGHTTP2}")
|
||||
message("OPEN_SSL: ${OPEN_SSL}")
|
||||
message("OPEN_ZLIB: ${OPEN_ZLIB}")
|
||||
message("OPEN_PROTOBUF: ${OPEN_PROTOBUF}")
|
||||
message("TARS_MYSQL: ${TARS_MYSQL}")
|
||||
message("TARS_HTTP2: ${TARS_HTTP2}")
|
||||
message("TARS_SSL: ${TARS_SSL}")
|
||||
message("TARS_ZLIB: ${TARS_ZLIB}")
|
||||
message("TARS_PROTOBUF: ${TARS_PROTOBUF}")
|
||||
|
||||
#-------------------------------------------------------------
|
||||
|
||||
|
@ -57,7 +57,7 @@ void syncRpc2(int c)
|
||||
|
||||
try
|
||||
{
|
||||
param.servant2Prx->http_call("GET", "/", header, "helloworld", rheader, rbody);
|
||||
param.servant2Prx->http_call("POST", "/", header, "helloworld", rheader, rbody);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
@ -70,6 +70,43 @@ void syncRpc2(int c)
|
||||
cout << "syncRpc2 total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
|
||||
}
|
||||
|
||||
|
||||
struct TestHttpCallback : public HttpCallback
|
||||
{
|
||||
TestHttpCallback(int64_t t, int i, int c) : start(t), cur(i), count(c)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual int onHttpResponse(const std::map<std::string, std::string>& requestHeaders ,
|
||||
const std::map<std::string, std::string>& responseHeaders ,
|
||||
const std::vector<char>& rspBody)
|
||||
{
|
||||
callback_count++;
|
||||
|
||||
if(cur == count-1)
|
||||
{
|
||||
int64_t cost = TC_Common::now2us() - start;
|
||||
cout << "onHttpResponse count:" << count << ", " << cost << " us, avg:" << 1.*cost/count << "us" << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
virtual int onHttpResponseException(const std::map<std::string, std::string>& requestHeaders,
|
||||
int expCode)
|
||||
{
|
||||
cout << "onHttpResponseException expCode:" << expCode << endl;
|
||||
|
||||
callback_count++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int64_t start;
|
||||
int cur;
|
||||
int count;
|
||||
};
|
||||
|
||||
void asyncRpc2(int c)
|
||||
{
|
||||
int64_t t = TC_Common::now2us();
|
||||
@ -112,7 +149,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 4)
|
||||
{
|
||||
cout << "Usage:" << argv[0] << "--count=1000 --call=[synchttp2|asynchttp2] --thread=1" << endl;
|
||||
cout << "Usage:" << argv[0] << "--count=1000 --call=[sync|async] --thread=1" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -149,11 +186,11 @@ int main(int argc, char *argv[])
|
||||
|
||||
std::function<void(int)> func;
|
||||
|
||||
if (param.call == "synchttp2")
|
||||
if (param.call == "sync")
|
||||
{
|
||||
func = syncRpc2;
|
||||
}
|
||||
else if(param.call == "asynchttp2")
|
||||
else if(param.call == "async")
|
||||
{
|
||||
func = asyncRpc2;
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
build_tars_server("HttpServer" "")
|
||||
build_tars_server("Http2Server" "")
|
@ -65,7 +65,9 @@ int Http2Imp::doRequest(TarsCurrentPtr current, vector<char> &buffer)
|
||||
|
||||
for(size_t i = 0; i < vtReqid.size(); i++)
|
||||
{
|
||||
session->doResponse(vtReqid[i], rsp, buffer);
|
||||
vector<char> data;
|
||||
session->doResponse(vtReqid[i], rsp, data);
|
||||
buffer.insert(buffer.end(), data.begin(), data.end());
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -49,17 +49,6 @@
|
||||
#远程LogServer[可选]
|
||||
# log = tars.tarslog.LogObj
|
||||
|
||||
#配置绑定端口
|
||||
<HttpAdapter>
|
||||
endpoint = tcp -h 0.0.0.0 -p 8081 -t 10000
|
||||
allow =
|
||||
maxconns = 4096
|
||||
threads = 5
|
||||
servant = TestApp.HttpServer.HttpObj
|
||||
queuecap = 1000000
|
||||
protocol = not-tars
|
||||
</HttpAdapter>
|
||||
|
||||
<Http2Adapter>
|
||||
endpoint = tcp -h 0.0.0.0 -p 8082 -t 10000
|
||||
allow =
|
||||
|
@ -124,7 +124,7 @@ void syncRpc(int c)
|
||||
|
||||
try
|
||||
{
|
||||
param.servantPrx->http1_call("GET", "/", header, "helloworld", rheader, rbody);
|
||||
param.servantPrx->http_call("GET", "/", header, "helloworld", rheader, rbody);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
|
@ -1,471 +0,0 @@
|
||||
// **********************************************************************
|
||||
// This file was generated by a TARS parser!
|
||||
// TARS version 2.0.0.
|
||||
// **********************************************************************
|
||||
|
||||
#ifndef __SSL_H_
|
||||
#define __SSL_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, true);
|
||||
}
|
||||
|
||||
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, true);
|
||||
}
|
||||
|
||||
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
|
@ -25,7 +25,7 @@
|
||||
|
||||
#if TARS_HTTP2
|
||||
#include "util/tc_http2.h"
|
||||
// #include "util/tc_http2clientmgr.h"
|
||||
#include "nghttp2/nghttp2.h"
|
||||
#endif
|
||||
|
||||
namespace tars
|
||||
|
@ -833,39 +833,39 @@ void ServantProxy::rpc_call_async(uint32_t iRequestId,
|
||||
|
||||
invoke(msg, bCoro);
|
||||
}
|
||||
//
|
||||
//
|
||||
//void ServantProxy::http1_call(const std::string& method,
|
||||
// const std::string& uri,
|
||||
// const std::map<std::string, std::string>& headers,
|
||||
// const std::string& body,
|
||||
// std::map<std::string, std::string>& rheaders,
|
||||
// std::string& rbody)
|
||||
//{
|
||||
// ReqMessage* msg = new ReqMessage();
|
||||
//
|
||||
// msg->init(ReqMessage::SYNC_CALL, NULL, "");
|
||||
//
|
||||
// msg->bFromRpc = true;
|
||||
// msg->request.sServantName = uri;
|
||||
// msg->request.sFuncName = method;
|
||||
// msg->request.iRequestId = this->tars_gen_requestid();
|
||||
//
|
||||
// // 使用下面两个字段保存头部和包体
|
||||
// msg->request.context = headers;
|
||||
//
|
||||
// msg->request.sBuffer.assign(body.begin(), body.end());
|
||||
//
|
||||
// invoke(msg);
|
||||
//
|
||||
// rheaders.swap(msg->response->status);
|
||||
// rbody.assign(msg->response->sBuffer.begin(), msg->response->sBuffer.end());
|
||||
//
|
||||
// delete msg;
|
||||
// msg = NULL;
|
||||
//}
|
||||
|
||||
|
||||
void ServantProxy::http1_call(const std::string& method,
|
||||
const std::string& uri,
|
||||
const std::map<std::string, std::string>& headers,
|
||||
const std::string& body,
|
||||
std::map<std::string, std::string>& rheaders,
|
||||
std::string& rbody)
|
||||
{
|
||||
ReqMessage* msg = new ReqMessage();
|
||||
|
||||
msg->init(ReqMessage::SYNC_CALL, NULL, "");
|
||||
|
||||
msg->bFromRpc = true;
|
||||
msg->request.sServantName = uri;
|
||||
msg->request.sFuncName = method;
|
||||
msg->request.iRequestId = this->tars_gen_requestid();
|
||||
|
||||
// 使用下面两个字段保存头部和包体
|
||||
msg->request.context = headers;
|
||||
|
||||
msg->request.sBuffer.assign(body.begin(), body.end());
|
||||
|
||||
invoke(msg);
|
||||
|
||||
rheaders.swap(msg->response->status);
|
||||
rbody.assign(msg->response->sBuffer.begin(), msg->response->sBuffer.end());
|
||||
|
||||
delete msg;
|
||||
msg = NULL;
|
||||
}
|
||||
|
||||
void ServantProxy::http2_call(const std::string& method,
|
||||
void ServantProxy::http_call(const std::string& method,
|
||||
const std::string& uri,
|
||||
const std::map<std::string, std::string>& headers,
|
||||
const std::string& body,
|
||||
@ -893,7 +893,7 @@ void ServantProxy::http2_call(const std::string& method,
|
||||
msg = NULL;
|
||||
}
|
||||
|
||||
void ServantProxy::http2_call_async(const std::map<std::string, std::string>& headers,
|
||||
void ServantProxy::http_call_async(const std::map<std::string, std::string>& headers,
|
||||
const std::string& body,
|
||||
const HttpCallbackPtr &cb)
|
||||
{
|
||||
|
@ -571,7 +571,7 @@ int TcpTransceiver::doResponse()
|
||||
{
|
||||
TC_NetWorkBuffer::PACKET_TYPE ret;
|
||||
|
||||
while(!rbuf->empty())
|
||||
do
|
||||
{
|
||||
shared_ptr<ResponsePacket> rsp = std::make_shared<ResponsePacket>();
|
||||
|
||||
@ -588,7 +588,7 @@ int TcpTransceiver::doResponse()
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}while(true);
|
||||
|
||||
//接收的数据小于buffer大小, 内核会再次通知你
|
||||
if(iRet < BUFFER_SIZE)
|
||||
|
@ -620,22 +620,22 @@ public:
|
||||
const char* buff, uint32_t len,
|
||||
const ServantProxyCallbackPtr& callback,
|
||||
bool bCoro = false);
|
||||
|
||||
|
||||
/**
|
||||
* http1同步远程调用
|
||||
*/
|
||||
void http1_call(const std::string& method,
|
||||
const std::string& uri,
|
||||
const std::map<std::string, std::string>& headers,
|
||||
const std::string& body,
|
||||
std::map<std::string, std::string>& rheaders,
|
||||
std::string& rbody);
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * http1同步远程调用
|
||||
// */
|
||||
// void http1_call(const std::string& method,
|
||||
// const std::string& uri,
|
||||
// const std::map<std::string, std::string>& headers,
|
||||
// const std::string& body,
|
||||
// std::map<std::string, std::string>& rheaders,
|
||||
// std::string& rbody);
|
||||
|
||||
/**
|
||||
* http2协议同步远程调用
|
||||
*/
|
||||
void http2_call(const std::string& method,
|
||||
void http_call(const std::string& method,
|
||||
const std::string& uri,
|
||||
const std::map<std::string, std::string>& headers,
|
||||
const std::string& body,
|
||||
@ -644,7 +644,7 @@ public:
|
||||
/**
|
||||
* http2协议异步远程调用
|
||||
*/
|
||||
void http2_call_async(const std::map<std::string, std::string>& headers,
|
||||
void http_call_async(const std::map<std::string, std::string>& headers,
|
||||
const std::string& body,
|
||||
const HttpCallbackPtr &cb);
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
#ifndef __TC_HTTP2_H__
|
||||
#define __TC_HTTP2_H__
|
||||
|
||||
#if TARS_HTTP2
|
||||
//#if TARS_HTTP2
|
||||
|
||||
#include "util/tc_http.h"
|
||||
#include "util/tc_spin_lock.h"
|
||||
#include "util/tc_network_buffer.h"
|
||||
#include "nghttp2/nghttp2.h"
|
||||
//#include "nghttp2/nghttp2.h"
|
||||
|
||||
typedef struct nghttp2_session nghttp2_session;
|
||||
|
||||
namespace tars
|
||||
{
|
||||
@ -188,6 +190,6 @@ private:
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
//#endif
|
||||
|
||||
#endif
|
||||
|
@ -20,6 +20,7 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include "util/tc_http2.h"
|
||||
#include "nghttp2/nghttp2.h"
|
||||
|
||||
namespace tars
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user