From 0d6a4eb76bab5745baa4cc8949e87dfe685d39b0 Mon Sep 17 00:00:00 2001 From: jarodruan Date: Sun, 16 Feb 2020 20:17:52 +0800 Subject: [PATCH] http2 compiler succ, sync has bug --- CMakeLists.txt | 89 ++-- examples/HttpDemo/Http2Client/main.cpp | 45 +- examples/HttpDemo/Http2Server/CMakeLists.txt | 2 +- examples/HttpDemo/Http2Server/Http2Imp.cpp | 4 +- examples/HttpDemo/Http2Server/config.conf | 11 - examples/HttpDemo/HttpClient/main.cpp | 2 +- examples/SSLDemo/Server/ssl.h | 471 ------------------- servant/libservant/AppProtocol.cpp | 2 +- servant/libservant/ServantProxy.cpp | 66 +-- servant/libservant/Transceiver.cpp | 4 +- servant/servant/ServantProxy.h | 26 +- util/include/util/tc_http2.h | 8 +- util/src/tc_http2.cpp | 1 + 13 files changed, 156 insertions(+), 575 deletions(-) delete mode 100644 examples/SSLDemo/Server/ssl.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2cccba0..d3ac6b0 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}") #------------------------------------------------------------- diff --git a/examples/HttpDemo/Http2Client/main.cpp b/examples/HttpDemo/Http2Client/main.cpp index 688f7d2..b249569 100644 --- a/examples/HttpDemo/Http2Client/main.cpp +++ b/examples/HttpDemo/Http2Client/main.cpp @@ -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& requestHeaders , + const std::map& responseHeaders , + const std::vector& 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& 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 func; - if (param.call == "synchttp2") + if (param.call == "sync") { func = syncRpc2; } - else if(param.call == "asynchttp2") + else if(param.call == "async") { func = asyncRpc2; } diff --git a/examples/HttpDemo/Http2Server/CMakeLists.txt b/examples/HttpDemo/Http2Server/CMakeLists.txt index 8e915cb..2332fd7 100644 --- a/examples/HttpDemo/Http2Server/CMakeLists.txt +++ b/examples/HttpDemo/Http2Server/CMakeLists.txt @@ -1 +1 @@ -build_tars_server("HttpServer" "") \ No newline at end of file +build_tars_server("Http2Server" "") \ No newline at end of file diff --git a/examples/HttpDemo/Http2Server/Http2Imp.cpp b/examples/HttpDemo/Http2Server/Http2Imp.cpp index e282f78..b108339 100644 --- a/examples/HttpDemo/Http2Server/Http2Imp.cpp +++ b/examples/HttpDemo/Http2Server/Http2Imp.cpp @@ -65,7 +65,9 @@ int Http2Imp::doRequest(TarsCurrentPtr current, vector &buffer) for(size_t i = 0; i < vtReqid.size(); i++) { - session->doResponse(vtReqid[i], rsp, buffer); + vector data; + session->doResponse(vtReqid[i], rsp, data); + buffer.insert(buffer.end(), data.begin(), data.end()); } } else diff --git a/examples/HttpDemo/Http2Server/config.conf b/examples/HttpDemo/Http2Server/config.conf index e71c136..82ba06f 100755 --- a/examples/HttpDemo/Http2Server/config.conf +++ b/examples/HttpDemo/Http2Server/config.conf @@ -49,17 +49,6 @@ #远程LogServer[可选] # log = tars.tarslog.LogObj - #配置绑定端口 - - 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 - - endpoint = tcp -h 0.0.0.0 -p 8082 -t 10000 allow = diff --git a/examples/HttpDemo/HttpClient/main.cpp b/examples/HttpDemo/HttpClient/main.cpp index 3aa8ec6..46258fa 100644 --- a/examples/HttpDemo/HttpClient/main.cpp +++ b/examples/HttpDemo/HttpClient/main.cpp @@ -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) { diff --git a/examples/SSLDemo/Server/ssl.h b/examples/SSLDemo/Server/ssl.h deleted file mode 100644 index 53b5b30..0000000 --- a/examples/SSLDemo/Server/ssl.h +++ /dev/null @@ -1,471 +0,0 @@ -// ********************************************************************** -// This file was generated by a TARS parser! -// TARS version 2.0.0. -// ********************************************************************** - -#ifndef __SSL_H_ -#define __SSL_H_ - -#include -#include -#include -#include "tup/Tars.h" -#include "tup/TarsJson.h" -using namespace std; -#include "servant/ServantProxy.h" -#include "servant/Servant.h" - - -namespace TestApp -{ - - /* callback of async proxy for client */ - class HelloPrxCallback: public tars::ServantProxyCallback - { - public: - virtual ~HelloPrxCallback(){} - virtual void callback_test(tars::Int32 ret) - { throw std::runtime_error("callback_test() override incorrect."); } - virtual void callback_test_exception(tars::Int32 ret) - { throw std::runtime_error("callback_test_exception() override incorrect."); } - - virtual void callback_testHello(tars::Int32 ret, const std::string& sRsp) - { throw std::runtime_error("callback_testHello() override incorrect."); } - virtual void callback_testHello_exception(tars::Int32 ret) - { throw std::runtime_error("callback_testHello_exception() override incorrect."); } - - public: - virtual const map & getResponseContext() const - { - CallbackThreadData * pCbtd = CallbackThreadData::getData(); - assert(pCbtd != NULL); - - if(!pCbtd->getContextValid()) - { - throw TC_Exception("cann't get response context"); - } - return pCbtd->getResponseContext(); - } - - public: - virtual int onDispatch(tars::ReqMessagePtr msg) - { - static ::std::string __Hello_all[]= - { - "test", - "testHello" - }; - pair r = equal_range(__Hello_all, __Hello_all+2, string(msg->request.sFuncName)); - if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; - switch(r.first - __Hello_all) - { - case 0: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_test_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - - CallbackThreadData * pCbtd = CallbackThreadData::getData(); - assert(pCbtd != NULL); - - pCbtd->setResponseContext(msg->response->context); - - callback_test(_ret); - - pCbtd->delResponseContext(); - - return tars::TARSSERVERSUCCESS; - - } - case 1: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_testHello_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - - std::string sRsp; - _is.read(sRsp, 2, true); - CallbackThreadData * pCbtd = CallbackThreadData::getData(); - assert(pCbtd != NULL); - - pCbtd->setResponseContext(msg->response->context); - - callback_testHello(_ret, sRsp); - - pCbtd->delResponseContext(); - - return tars::TARSSERVERSUCCESS; - - } - } - return tars::TARSSERVERNOFUNCERR; - } - - }; - typedef tars::TC_AutoPtr HelloPrxCallbackPtr; - - /* callback of coroutine async proxy for client */ - class HelloCoroPrxCallback: public HelloPrxCallback - { - public: - virtual ~HelloCoroPrxCallback(){} - public: - virtual const map & getResponseContext() const { return _mRspContext; } - - virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } - - public: - int onDispatch(tars::ReqMessagePtr msg) - { - static ::std::string __Hello_all[]= - { - "test", - "testHello" - }; - - pair r = equal_range(__Hello_all, __Hello_all+2, string(msg->request.sFuncName)); - if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; - switch(r.first - __Hello_all) - { - case 0: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_test_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - try - { - tars::Int32 _ret; - _is.read(_ret, 0, true); - - setResponseContext(msg->response->context); - - callback_test(_ret); - - } - catch(std::exception &ex) - { - callback_test_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - catch(...) - { - callback_test_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - - return tars::TARSSERVERSUCCESS; - - } - case 1: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_testHello_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - try - { - tars::Int32 _ret; - _is.read(_ret, 0, true); - - std::string sRsp; - _is.read(sRsp, 2, true); - setResponseContext(msg->response->context); - - callback_testHello(_ret, sRsp); - - } - catch(std::exception &ex) - { - callback_testHello_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - catch(...) - { - callback_testHello_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - - return tars::TARSSERVERSUCCESS; - - } - } - return tars::TARSSERVERNOFUNCERR; - } - - protected: - map _mRspContext; - }; - typedef tars::TC_AutoPtr HelloCoroPrxCallbackPtr; - - /* proxy for client */ - class HelloProxy : public tars::ServantProxy - { - public: - typedef map TARS_CONTEXT; - tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) - { - tars::TarsOutputStream _os; - std::map _mStatus; - shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); - if(pResponseContext) - { - pResponseContext->swap(rep->context); - } - - tars::TarsInputStream _is; - _is.setBuffer(rep->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - return _ret; - } - - void async_test(HelloPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); - } - - void coro_test(HelloCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback, true); - } - - tars::Int32 testHello(const std::string & sReq,std::string &sRsp,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) - { - tars::TarsOutputStream _os; - _os.write(sReq, 1); - _os.write(sRsp, 2); - std::map _mStatus; - shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testHello", _os, context, _mStatus); - if(pResponseContext) - { - pResponseContext->swap(rep->context); - } - - tars::TarsInputStream _is; - _is.setBuffer(rep->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - _is.read(sRsp, 2, true); - return _ret; - } - - void async_testHello(HelloPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - _os.write(sReq, 1); - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback); - } - - void coro_testHello(HelloCoroPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - _os.write(sReq, 1); - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback, 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 HelloPrx; - - /* servant for server */ - class Hello : public tars::Servant - { - public: - virtual ~Hello(){} - virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; - static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) - { - if (current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(current->getRequestVersion()); - tarsAttr.put("", _ret); - - vector sTupResponseBuffer; - tarsAttr.encode(sTupResponseBuffer); - current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - - current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); - } - } - - virtual tars::Int32 testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current) = 0; - static void async_response_testHello(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sRsp) - { - if (current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(current->getRequestVersion()); - tarsAttr.put("", _ret); - tarsAttr.put("sRsp", sRsp); - - vector sTupResponseBuffer; - tarsAttr.encode(sTupResponseBuffer); - current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - - _os.write(sRsp, 2); - - current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); - } - } - - public: - int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) - { - static ::std::string __TestApp__Hello_all[]= - { - "test", - "testHello" - }; - - pair r = equal_range(__TestApp__Hello_all, __TestApp__Hello_all+2, _current->getFuncName()); - if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; - switch(r.first - __TestApp__Hello_all) - { - case 0: - { - tars::TarsInputStream _is; - _is.setBuffer(_current->getRequestBuffer()); - if (_current->getRequestVersion() == TUPVERSION) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.decode(_current->getRequestBuffer()); - } - else - { - } - tars::Int32 _ret = test(_current); - if(_current->isResponse()) - { - if (_current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.put("", _ret); - tarsAttr.encode(_sResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - _os.swap(_sResponseBuffer); - } - } - return tars::TARSSERVERSUCCESS; - - } - case 1: - { - tars::TarsInputStream _is; - _is.setBuffer(_current->getRequestBuffer()); - std::string sReq; - std::string sRsp; - if (_current->getRequestVersion() == TUPVERSION) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.decode(_current->getRequestBuffer()); - tarsAttr.get("sReq", sReq); - tarsAttr.getByDefault("sRsp", sRsp, sRsp); - } - else - { - _is.read(sReq, 1, true); - _is.read(sRsp, 2, false); - } - tars::Int32 _ret = testHello(sReq,sRsp, _current); - if(_current->isResponse()) - { - if (_current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.put("", _ret); - tarsAttr.put("sRsp", sRsp); - tarsAttr.encode(_sResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - _os.write(sRsp, 2); - _os.swap(_sResponseBuffer); - } - } - return tars::TARSSERVERSUCCESS; - - } - } - return tars::TARSSERVERNOFUNCERR; - } - }; - - -} - - - -#endif diff --git a/servant/libservant/AppProtocol.cpp b/servant/libservant/AppProtocol.cpp index 7e244de..5e2bf35 100644 --- a/servant/libservant/AppProtocol.cpp +++ b/servant/libservant/AppProtocol.cpp @@ -25,7 +25,7 @@ #if TARS_HTTP2 #include "util/tc_http2.h" -// #include "util/tc_http2clientmgr.h" +#include "nghttp2/nghttp2.h" #endif namespace tars diff --git a/servant/libservant/ServantProxy.cpp b/servant/libservant/ServantProxy.cpp index 26de607..8652ca4 100644 --- a/servant/libservant/ServantProxy.cpp +++ b/servant/libservant/ServantProxy.cpp @@ -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& headers, +// const std::string& body, +// std::map& 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& headers, - const std::string& body, - std::map& 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& 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& headers, +void ServantProxy::http_call_async(const std::map& headers, const std::string& body, const HttpCallbackPtr &cb) { diff --git a/servant/libservant/Transceiver.cpp b/servant/libservant/Transceiver.cpp index 2c193d5..d469dd3 100755 --- a/servant/libservant/Transceiver.cpp +++ b/servant/libservant/Transceiver.cpp @@ -571,7 +571,7 @@ int TcpTransceiver::doResponse() { TC_NetWorkBuffer::PACKET_TYPE ret; - while(!rbuf->empty()) + do { shared_ptr rsp = std::make_shared(); @@ -588,7 +588,7 @@ int TcpTransceiver::doResponse() else { break; } - } + }while(true); //接收的数据小于buffer大小, 内核会再次通知你 if(iRet < BUFFER_SIZE) diff --git a/servant/servant/ServantProxy.h b/servant/servant/ServantProxy.h index 2826248..553bf4e 100644 --- a/servant/servant/ServantProxy.h +++ b/servant/servant/ServantProxy.h @@ -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& headers, - const std::string& body, - std::map& rheaders, - std::string& rbody); +// +// +// /** +// * http1同步远程调用 +// */ +// void http1_call(const std::string& method, +// const std::string& uri, +// const std::map& headers, +// const std::string& body, +// std::map& 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& headers, const std::string& body, @@ -644,7 +644,7 @@ public: /** * http2协议异步远程调用 */ - void http2_call_async(const std::map& headers, + void http_call_async(const std::map& headers, const std::string& body, const HttpCallbackPtr &cb); diff --git a/util/include/util/tc_http2.h b/util/include/util/tc_http2.h index 431a67f..2b20e5d 100644 --- a/util/include/util/tc_http2.h +++ b/util/include/util/tc_http2.h @@ -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 diff --git a/util/src/tc_http2.cpp b/util/src/tc_http2.cpp index cfbdcd1..98d0338 100644 --- a/util/src/tc_http2.cpp +++ b/util/src/tc_http2.cpp @@ -20,6 +20,7 @@ #include #include #include "util/tc_http2.h" +#include "nghttp2/nghttp2.h" namespace tars {