mirror of
https://gitee.com/TarsCloud/TarsCpp.git
synced 2024-12-22 22:16:38 +08:00
linux compiler succ
This commit is contained in:
parent
bcb8884843
commit
7c359b7281
@ -20,6 +20,14 @@ if(WIN32)
|
||||
COMMAND ../servant/script/busybox.exe bash ../examples/scripts/run-http.bat
|
||||
COMMENT "call run http")
|
||||
|
||||
if(TARS_HTTP2)
|
||||
add_custom_target(run-http2
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
DEPENDS Http2Server Http2Client
|
||||
COMMAND ../servant/script/busybox.exe bash ../examples/scripts/run-http2.bat
|
||||
COMMENT "call run http2")
|
||||
endif()
|
||||
|
||||
add_custom_target(run-co
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
DEPENDS CoroutineDemoAServer CoroutineDemoBServer CoroutineDemoClient testCoro testParallelCoro
|
||||
@ -61,6 +69,14 @@ else(WIN32)
|
||||
COMMAND sh ../examples/scripts/run-http.sh
|
||||
COMMENT "call run http")
|
||||
|
||||
if(TARS_HTTP2)
|
||||
add_custom_target(run-http2
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
DEPENDS Http2Server Http2Client
|
||||
COMMAND sh ../examples/scripts/run-http2.sh
|
||||
COMMENT "call run http2")
|
||||
endif()
|
||||
|
||||
add_custom_target(run-co
|
||||
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||
DEPENDS CoroutineDemoAServer CoroutineDemoBServer CoroutineDemoClient testCoro testParallelCoro
|
||||
|
@ -2,3 +2,7 @@
|
||||
add_subdirectory(HttpClient)
|
||||
add_subdirectory(HttpServer)
|
||||
|
||||
if(TARS_HTTP2)
|
||||
add_subdirectory(Http2Client)
|
||||
add_subdirectory(Http2Server)
|
||||
endif()
|
1
examples/HttpDemo/Http2Client/CMakeLists.txt
Normal file
1
examples/HttpDemo/Http2Client/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
build_tars_server("Http2Client" "Http2Server")
|
201
examples/HttpDemo/Http2Client/main.cpp
Normal file
201
examples/HttpDemo/Http2Client/main.cpp
Normal file
@ -0,0 +1,201 @@
|
||||
/**
|
||||
* 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 <iostream>
|
||||
#include "util/tc_http.h"
|
||||
#include "util/tc_option.h"
|
||||
#include "util/tc_common.h"
|
||||
#include "util/tc_clientsocket.h"
|
||||
#include "util/tc_thread_pool.h"
|
||||
#include "util/tc_timeprovider.h"
|
||||
#include "servant/Application.h"
|
||||
using namespace std;
|
||||
using namespace tars;
|
||||
|
||||
Communicator* _comm;
|
||||
|
||||
static string http2Obj = "Test.HttpServer.http2Obj@tcp -h 127.0.0.1 -p 8082";
|
||||
|
||||
struct Param
|
||||
{
|
||||
int count;
|
||||
string call;
|
||||
int thread;
|
||||
|
||||
ServantPrx servant2Prx;
|
||||
};
|
||||
|
||||
Param param;
|
||||
std::atomic<int> callback_count(0);
|
||||
|
||||
void syncRpc2(int c)
|
||||
{
|
||||
int64_t t = TC_Common::now2us();
|
||||
|
||||
std::map<std::string, std::string> header;
|
||||
header[":authority"] = "domain.com";
|
||||
header[":scheme"] = "http";
|
||||
|
||||
std::map<std::string, std::string> rheader;
|
||||
//发起远程调用
|
||||
for (int i = 0; i < c; ++i)
|
||||
{
|
||||
string rbody;
|
||||
|
||||
try
|
||||
{
|
||||
param.servant2Prx->http_call("GET", "/", header, "helloworld", rheader, rbody);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "exception:" << e.what() << endl;
|
||||
}
|
||||
++callback_count;
|
||||
}
|
||||
|
||||
int64_t cost = TC_Common::now2us() - t;
|
||||
cout << "syncRpc2 total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
|
||||
}
|
||||
|
||||
void asyncRpc2(int c)
|
||||
{
|
||||
int64_t t = TC_Common::now2us();
|
||||
|
||||
std::map<std::string, std::string> header;
|
||||
header[":path"] = "/";
|
||||
header[":method"] = "GET";
|
||||
header[":authority"] = "domain.com";
|
||||
header[":scheme"] = "http";
|
||||
|
||||
//发起远程调用
|
||||
for (int i = 0; i < c; ++i)
|
||||
{
|
||||
HttpCallbackPtr p = new TestHttpCallback(t, i, c);
|
||||
|
||||
try
|
||||
{
|
||||
param.servant2Prx->http_call_async(header, "helloworld", p);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "exception:" << e.what() << endl;
|
||||
}
|
||||
|
||||
TC_Common::msleep(10);
|
||||
|
||||
// while(i-callback_count > 0 )
|
||||
// {
|
||||
// TC_Common::msleep(100);
|
||||
// }
|
||||
}
|
||||
|
||||
int64_t cost = TC_Common::now2us() - t;
|
||||
cout << "asyncRpc2 send:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc < 4)
|
||||
{
|
||||
cout << "Usage:" << argv[0] << "--count=1000 --call=[synchttp2|asynchttp2] --thread=1" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
TC_Option option;
|
||||
option.decode(argc, argv);
|
||||
|
||||
param.count = TC_Common::strto<int>(option.getValue("count"));
|
||||
if(param.count <= 0) param.count = 1000;
|
||||
param.call = option.getValue("call");
|
||||
if(param.call.empty()) param.call = "sync";
|
||||
param.thread = TC_Common::strto<int>(option.getValue("thread"));
|
||||
if(param.thread <= 0) param.thread = 1;
|
||||
|
||||
_comm = new Communicator();
|
||||
|
||||
// TarsRollLogger::getInstance()->logger()->setLogLevel(6);
|
||||
|
||||
_comm->setProperty("sendqueuelimit", "1000000");
|
||||
_comm->setProperty("asyncqueuecap", "1000000");
|
||||
|
||||
param.servant2Prx = _comm->stringToProxy<ServantPrx>(http2Obj);
|
||||
|
||||
param.servant2Prx->tars_connect_timeout(5000);
|
||||
param.servant2Prx->tars_async_timeout(60*1000);
|
||||
|
||||
ProxyProtocol proto;
|
||||
|
||||
proto.requestFunc = ProxyProtocol::http2Request;
|
||||
proto.responseFunc = ProxyProtocol::http2Response;
|
||||
param.servant2Prx->tars_set_protocol(proto);
|
||||
|
||||
int64_t start = TC_Common::now2us();
|
||||
|
||||
std::function<void(int)> func;
|
||||
|
||||
if (param.call == "synchttp2")
|
||||
{
|
||||
func = syncRpc2;
|
||||
}
|
||||
else if(param.call == "asynchttp2")
|
||||
{
|
||||
func = asyncRpc2;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "no func, exits" << endl;
|
||||
exit(0);
|
||||
}
|
||||
|
||||
vector<std::thread*> vt;
|
||||
for(int i = 0 ; i< param.thread; i++)
|
||||
{
|
||||
vt.push_back(new std::thread(func, param.count));
|
||||
}
|
||||
|
||||
std::thread print([&]{while(callback_count != param.count * param.thread) {
|
||||
cout << param.call << ": ----------finish count:" << callback_count << endl;
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
};});
|
||||
|
||||
for(size_t i = 0 ; i< vt.size(); i++)
|
||||
{
|
||||
vt[i]->join();
|
||||
delete vt[i];
|
||||
}
|
||||
|
||||
cout << "(pid:" << std::this_thread::get_id() << ")"
|
||||
<< "(count:" << param.count << ")"
|
||||
<< "(use ms:" << (TC_Common::now2us() - start)/1000 << ")"
|
||||
<< endl;
|
||||
|
||||
while(callback_count != param.count * param.thread) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
print.join();
|
||||
cout << "----------finish count:" << callback_count << endl;
|
||||
}
|
||||
catch(exception &ex)
|
||||
{
|
||||
cout << ex.what() << endl;
|
||||
}
|
||||
cout << "main return." << endl;
|
||||
|
||||
return 0;
|
||||
}
|
1
examples/HttpDemo/Http2Server/CMakeLists.txt
Normal file
1
examples/HttpDemo/Http2Server/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
build_tars_server("HttpServer" "")
|
@ -17,10 +17,14 @@
|
||||
#ifndef _Http2Imp_H_
|
||||
#define _Http2Imp_H_
|
||||
|
||||
#include <unordered_map>
|
||||
#include "servant/Application.h"
|
||||
#include "util/tc_spin_lock.h"
|
||||
#include "util/tc_http2.h"
|
||||
|
||||
using namespace tars;
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
50
examples/HttpDemo/Http2Server/HttpImp.cpp
Normal file
50
examples/HttpDemo/Http2Server/HttpImp.cpp
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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 "HttpImp.h"
|
||||
#include "servant/Application.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
void HttpImp::initialize()
|
||||
{
|
||||
//initialize servant here:
|
||||
//...
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////
|
||||
void HttpImp::destroy()
|
||||
{
|
||||
//destroy servant here:
|
||||
//...
|
||||
}
|
||||
|
||||
int HttpImp::doRequest(TarsCurrentPtr current, vector<char> &buffer)
|
||||
{
|
||||
TC_HttpRequest request;
|
||||
vector<char> v = current->getRequestBuffer();
|
||||
string sBuf;
|
||||
sBuf.assign(&v[0],v.size());
|
||||
|
||||
request.decode(sBuf);
|
||||
TC_HttpResponse rsp;
|
||||
string s="hello";
|
||||
rsp.setResponse(s.c_str(),s.size());
|
||||
rsp.encode(buffer);
|
||||
|
||||
return 0;
|
||||
}
|
51
examples/HttpDemo/Http2Server/HttpImp.h
Normal file
51
examples/HttpDemo/Http2Server/HttpImp.h
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
* 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 _HttpImp_H_
|
||||
#define _HttpImp_H_
|
||||
|
||||
#include "servant/Application.h"
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class HttpImp : public Servant
|
||||
{
|
||||
public:
|
||||
/**
|
||||
*
|
||||
*/
|
||||
virtual ~HttpImp() {}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
virtual void initialize();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
virtual void destroy();
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
int doRequest(TarsCurrentPtr current, vector<char> &buffer);
|
||||
|
||||
};
|
||||
/////////////////////////////////////////////////////
|
||||
#endif
|
80
examples/HttpDemo/Http2Server/HttpServer.cpp
Normal file
80
examples/HttpDemo/Http2Server/HttpServer.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
/**
|
||||
* 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 "HttpServer.h"
|
||||
#include "HttpImp.h"
|
||||
#include "Http2Imp.h"
|
||||
#include "util/tc_http2.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
HttpServer g_app;
|
||||
|
||||
TC_NetWorkBuffer::PACKET_TYPE parseHttp2(TC_NetWorkBuffer&in, vector<char> &out)
|
||||
{
|
||||
TC_Http2Server*session = (TC_Http2Server*)(in.getContextData());
|
||||
|
||||
if(session == NULL)
|
||||
{
|
||||
session = new TC_Http2Server();
|
||||
in.setContextData(session, [=]{delete session;});
|
||||
|
||||
TC_EpollServer::Connection *connection = (TC_EpollServer::Connection *)in.getConnection();
|
||||
Http2Imp::addHttp2(connection->getId(), session);
|
||||
}
|
||||
|
||||
return session->parse(in, out);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
HttpServer::initialize()
|
||||
{
|
||||
//initialize application here:
|
||||
//...
|
||||
|
||||
addServant<HttpImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj");
|
||||
addServant<Http2Imp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".Http2Obj");
|
||||
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&TC_NetWorkBuffer::parseHttp);
|
||||
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".Http2Obj", &parseHttp2);
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////
|
||||
void
|
||||
HttpServer::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;
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////
|
50
examples/HttpDemo/Http2Server/HttpServer.h
Normal file
50
examples/HttpDemo/Http2Server/HttpServer.h
Normal file
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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 _HttpServer_H_
|
||||
#define _HttpServer_H_
|
||||
|
||||
#include <iostream>
|
||||
#include "servant/Application.h"
|
||||
|
||||
using namespace tars;
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
class HttpServer : public Application
|
||||
{
|
||||
public:
|
||||
/**
|
||||
*
|
||||
**/
|
||||
virtual ~HttpServer() {};
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
virtual void initialize();
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
virtual void destroyApp();
|
||||
};
|
||||
|
||||
extern HttpServer g_app;
|
||||
|
||||
////////////////////////////////////////////
|
||||
#endif
|
74
examples/HttpDemo/Http2Server/config.conf
Executable file
74
examples/HttpDemo/Http2Server/config.conf
Executable file
@ -0,0 +1,74 @@
|
||||
<tars>
|
||||
<application>
|
||||
#proxy需要的配置
|
||||
<client>
|
||||
#地址
|
||||
locator = tars.tarsregistry.QueryObj@tcp -h 127.0.0.1 -p 17890
|
||||
#最大超时时间(毫秒)
|
||||
sync-invoke-timeout = 5000
|
||||
#刷新端口时间间隔(毫秒)
|
||||
refresh-endpoint-interval = 10000
|
||||
#模块间调用[可选]
|
||||
stat = tars.tarsstat.StatObj
|
||||
#发送队列长度
|
||||
sendqueuelimit = 100000
|
||||
#异步回调队列个数限制
|
||||
asyncqueuecap = 100000
|
||||
#网络异步回调线程个数
|
||||
asyncthread = 3
|
||||
#网络线程个数
|
||||
netthread = 3
|
||||
#合并回调线程和网络线程(以网络线程个数为准)
|
||||
mergenetasync = 0
|
||||
#模块名称
|
||||
modulename = Test.HttpServer
|
||||
</client>
|
||||
|
||||
#定义所有绑定的IP
|
||||
<server>
|
||||
closecout = 0
|
||||
#应用名称
|
||||
app = Test
|
||||
#服务名称
|
||||
server = HttpServer
|
||||
#服务的数据目录,可执行文件,配置文件等
|
||||
basepath = ./
|
||||
datapath = ./
|
||||
#日志路径
|
||||
logpath = ./
|
||||
mergenetimp = 0
|
||||
#本地管理套接字[可选]
|
||||
local = tcp -h 127.0.0.1 -p 15001 -t 10000
|
||||
|
||||
#本地node的ip:port:timeout[可选]
|
||||
# node = ServerObj@tcp -h 127.0.0.1 -p 2345 -t 10000
|
||||
#配置中心的地址[可选]
|
||||
# config = tars.tarsconfig.ConfigObj
|
||||
#配置中心的地址[可选]
|
||||
# notify = tars.tarsconfig.NotifyObj
|
||||
#远程LogServer[可选]
|
||||
# log = tars.tarslog.LogObj
|
||||
|
||||
#配置绑定端口
|
||||
<HttpAdapter>
|
||||
endpoint = tcp -h 0.0.0.0 -p 8081 -t 10000
|
||||
allow =
|
||||
maxconns = 4096
|
||||
threads = 5
|
||||
servant = Test.HttpServer.HttpObj
|
||||
queuecap = 1000000
|
||||
protocol = not-tars
|
||||
</HttpAdapter>
|
||||
|
||||
<Http2Adapter>
|
||||
endpoint = tcp -h 0.0.0.0 -p 8082 -t 10000
|
||||
allow =
|
||||
maxconns = 4096
|
||||
threads = 5
|
||||
servant = Test.HttpServer.Http2Obj
|
||||
queuecap = 1000000
|
||||
protocol = not-tars
|
||||
</Http2Adapter>
|
||||
</server>
|
||||
</application>
|
||||
</tars>
|
@ -32,7 +32,6 @@ using namespace tup;
|
||||
Communicator* _comm;
|
||||
|
||||
static string httpObj = "Test.HttpServer.httpObj@tcp -h 127.0.0.1 -p 8081";
|
||||
static string http2Obj = "Test.HttpServer.http2Obj@tcp -h 127.0.0.1 -p 8082";
|
||||
|
||||
struct Param
|
||||
{
|
||||
@ -41,7 +40,6 @@ struct Param
|
||||
int thread;
|
||||
|
||||
ServantPrx servantPrx;
|
||||
ServantPrx servant2Prx;
|
||||
};
|
||||
|
||||
Param param;
|
||||
@ -141,79 +139,13 @@ void syncRpc(int c)
|
||||
cout << "syncCall total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
|
||||
}
|
||||
|
||||
|
||||
void syncRpc2(int c)
|
||||
{
|
||||
int64_t t = TC_Common::now2us();
|
||||
|
||||
std::map<std::string, std::string> header;
|
||||
header[":authority"] = "domain.com";
|
||||
header[":scheme"] = "http";
|
||||
|
||||
std::map<std::string, std::string> rheader;
|
||||
//发起远程调用
|
||||
for (int i = 0; i < c; ++i)
|
||||
{
|
||||
string rbody;
|
||||
|
||||
try
|
||||
{
|
||||
param.servant2Prx->http_call("GET", "/", header, "helloworld", rheader, rbody);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "exception:" << e.what() << endl;
|
||||
}
|
||||
++callback_count;
|
||||
}
|
||||
|
||||
int64_t cost = TC_Common::now2us() - t;
|
||||
cout << "syncRpc2 total:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
|
||||
}
|
||||
|
||||
void asyncRpc2(int c)
|
||||
{
|
||||
int64_t t = TC_Common::now2us();
|
||||
|
||||
std::map<std::string, std::string> header;
|
||||
header[":path"] = "/";
|
||||
header[":method"] = "GET";
|
||||
header[":authority"] = "domain.com";
|
||||
header[":scheme"] = "http";
|
||||
|
||||
//发起远程调用
|
||||
for (int i = 0; i < c; ++i)
|
||||
{
|
||||
HttpCallbackPtr p = new TestHttpCallback(t, i, c);
|
||||
|
||||
try
|
||||
{
|
||||
param.servant2Prx->http_call_async(header, "helloworld", p);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "exception:" << e.what() << endl;
|
||||
}
|
||||
|
||||
TC_Common::msleep(10);
|
||||
|
||||
// while(i-callback_count > 0 )
|
||||
// {
|
||||
// TC_Common::msleep(100);
|
||||
// }
|
||||
}
|
||||
|
||||
int64_t cost = TC_Common::now2us() - t;
|
||||
cout << "asyncRpc2 send:" << cost << "us, avg:" << 1.*cost/c << "us" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
try
|
||||
{
|
||||
if (argc < 4)
|
||||
{
|
||||
cout << "Usage:" << argv[0] << "--count=1000 --call=[basehttp|synchttp|synchttp2|asynchttp2] --thread=1" << endl;
|
||||
cout << "Usage:" << argv[0] << "--count=1000 --call=[basehttp|synchttp] --thread=1" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -236,23 +168,15 @@ int main(int argc, char *argv[])
|
||||
_comm->setProperty("asyncqueuecap", "1000000");
|
||||
|
||||
param.servantPrx = _comm->stringToProxy<ServantPrx>(httpObj);
|
||||
param.servant2Prx = _comm->stringToProxy<ServantPrx>(http2Obj);
|
||||
|
||||
param.servantPrx->tars_connect_timeout(5000);
|
||||
param.servantPrx->tars_async_timeout(60*1000);
|
||||
|
||||
param.servant2Prx->tars_connect_timeout(5000);
|
||||
param.servant2Prx->tars_async_timeout(60*1000);
|
||||
|
||||
ProxyProtocol proto;
|
||||
proto.requestFunc = ProxyProtocol::http1Request;
|
||||
proto.responseFunc = ProxyProtocol::http1Response;
|
||||
param.servantPrx->tars_set_protocol(proto);
|
||||
|
||||
proto.requestFunc = ProxyProtocol::http2Request;
|
||||
proto.responseFunc = ProxyProtocol::http2Response;
|
||||
param.servant2Prx->tars_set_protocol(proto);
|
||||
|
||||
int64_t start = TC_Common::now2us();
|
||||
|
||||
std::function<void(int)> func;
|
||||
@ -269,14 +193,6 @@ int main(int argc, char *argv[])
|
||||
// {
|
||||
// func = asyncRpc;
|
||||
// }
|
||||
else if (param.call == "synchttp2")
|
||||
{
|
||||
func = syncRpc2;
|
||||
}
|
||||
else if(param.call == "asynchttp2")
|
||||
{
|
||||
func = asyncRpc2;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "no func, exits" << endl;
|
||||
|
@ -1,16 +0,0 @@
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
|
||||
APP := Test
|
||||
TARGET := HttpClient
|
||||
CONFIG :=
|
||||
STRIP_FLAG:= N
|
||||
|
||||
INCLUDE +=
|
||||
LIB +=
|
||||
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
|
||||
include /usr/local/tars/cpp/makefile/makefile.tars
|
||||
#-----------------------------------------------------------------------
|
@ -16,29 +16,11 @@
|
||||
|
||||
#include "HttpServer.h"
|
||||
#include "HttpImp.h"
|
||||
#include "Http2Imp.h"
|
||||
#include "util/tc_http2.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
HttpServer g_app;
|
||||
|
||||
TC_NetWorkBuffer::PACKET_TYPE parseHttp2(TC_NetWorkBuffer&in, vector<char> &out)
|
||||
{
|
||||
TC_Http2Server*session = (TC_Http2Server*)(in.getContextData());
|
||||
|
||||
if(session == NULL)
|
||||
{
|
||||
session = new TC_Http2Server();
|
||||
in.setContextData(session, [=]{delete session;});
|
||||
|
||||
TC_EpollServer::Connection *connection = (TC_EpollServer::Connection *)in.getConnection();
|
||||
Http2Imp::addHttp2(connection->getId(), session);
|
||||
}
|
||||
|
||||
return session->parse(in, out);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
HttpServer::initialize()
|
||||
@ -47,9 +29,7 @@ HttpServer::initialize()
|
||||
//...
|
||||
|
||||
addServant<HttpImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj");
|
||||
addServant<Http2Imp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".Http2Obj");
|
||||
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".HttpObj",&TC_NetWorkBuffer::parseHttp);
|
||||
addServantProtocol(ServerConfig::Application + "." + ServerConfig::ServerName + ".Http2Obj", &parseHttp2);
|
||||
}
|
||||
/////////////////////////////////////////////////////////////////
|
||||
void
|
||||
|
@ -60,15 +60,6 @@
|
||||
protocol = not-tars
|
||||
</HttpAdapter>
|
||||
|
||||
<Http2Adapter>
|
||||
endpoint = tcp -h 0.0.0.0 -p 8082 -t 10000
|
||||
allow =
|
||||
maxconns = 4096
|
||||
threads = 5
|
||||
servant = Test.HttpServer.Http2Obj
|
||||
queuecap = 1000000
|
||||
protocol = not-tars
|
||||
</Http2Adapter>
|
||||
</server>
|
||||
</application>
|
||||
</tars>
|
||||
|
@ -1,14 +0,0 @@
|
||||
|
||||
#-----------------------------------------------------------------------
|
||||
|
||||
APP := TestApp
|
||||
TARGET := HttpServer
|
||||
CONFIG :=
|
||||
STRIP_FLAG:= N
|
||||
|
||||
INCLUDE +=
|
||||
LIB +=
|
||||
|
||||
#-----------------------------------------------------------------------i
|
||||
|
||||
include /usr/local/tars/cpp/makefile/makefile.tars
|
@ -13,8 +13,7 @@ sleep 3
|
||||
echo "client: .\\bin\\Release\\HttpClient.exe"
|
||||
|
||||
.\\bin\\Release\\HttpClient.exe --count=10000 --thread=2 --call=basehttp
|
||||
.\\bin\\Release\\HttpClient.exe --count=10000 --thread=2 --call=synchttp2
|
||||
.\\bin\\Release\\HttpClient.exe --count=10000 --thread=2 --call=asynchttp2
|
||||
.\\bin\\Release\\HttpClient.exe --count=10000 --thread=2 --call=synchttp
|
||||
|
||||
sleep 1
|
||||
|
||||
|
@ -14,8 +14,7 @@ sleep 1
|
||||
echo "client: ./bin/HttpClient"
|
||||
|
||||
./bin/HttpClient --count=10000 --thread=2 --call=basehttp
|
||||
./bin/HttpClient --count=10000 --thread=2 --call=synchttp2
|
||||
./bin/HttpClient --count=10000 --thread=2 --call=asynchttp2
|
||||
./bin/HttpClient --count=10000 --thread=2 --call=synchttp
|
||||
|
||||
sleep 1
|
||||
|
||||
|
22
examples/scripts/run-http2.bat
Normal file
22
examples/scripts/run-http2.bat
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
echo "run-http2.bat"
|
||||
|
||||
killall -9 Http2Server.exe
|
||||
sleep 1
|
||||
|
||||
echo "start server: .\\bin\\Release\\Http2Server.exe --config=..\\examples\\HttpDemo\\Http2Server\\config.conf &"
|
||||
|
||||
.\\bin\\Release\\Http2Server.exe --config=..\\examples\\HttpDemo\\Http2Server\\config.conf &
|
||||
|
||||
sleep 3
|
||||
|
||||
echo "client: .\\bin\\Release\\Http2Client.exe"
|
||||
|
||||
.\\bin\\Release\\Http2Client.exe --count=10000 --thread=2 --call=synchttp2
|
||||
.\\bin\\Release\\Http2Client.exe --count=10000 --thread=2 --call=asynchttp2
|
||||
|
||||
sleep 1
|
||||
|
||||
killall -9 Http2Server.exe
|
||||
|
||||
|
@ -62,47 +62,6 @@ vector<char> ProxyProtocol::tarsRequest(RequestPacket& request, Transceiver *)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////
|
||||
#if TARS_HTTP2
|
||||
|
||||
#define MAKE_NV(NAME, VALUE, VALUELEN) \
|
||||
{ \
|
||||
(uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, VALUELEN, \
|
||||
NGHTTP2_NV_FLAG_NONE \
|
||||
}
|
||||
|
||||
#define MAKE_NV2(NAME, VALUE) \
|
||||
{ \
|
||||
(uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1, \
|
||||
NGHTTP2_NV_FLAG_NONE \
|
||||
}
|
||||
|
||||
#define MAKE_STRING_NV(NAME, VALUE) {(uint8_t*)(NAME.data()), (uint8_t*)(VALUE.data()), NAME.size(), VALUE.size(), NGHTTP2_NV_FLAG_NONE};
|
||||
|
||||
// nghttp2读取请求包体,准备发送
|
||||
static ssize_t reqbody_read_callback(nghttp2_session *session, int32_t stream_id,
|
||||
uint8_t *buf, size_t length,
|
||||
uint32_t *data_flags,
|
||||
nghttp2_data_source *source,
|
||||
void *user_data)
|
||||
{
|
||||
std::vector<char>* body = (std::vector<char>* )source->ptr;
|
||||
// cout << "reqbody_read_callback:" << body->size() << endl;
|
||||
|
||||
if (body->empty())
|
||||
{
|
||||
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t len = length > body->size() ? body->size() : length;
|
||||
memcpy(buf, body->data(), len);
|
||||
|
||||
vector<char>::iterator end = body->begin();
|
||||
std::advance(end, len);
|
||||
body->erase(body->begin(), end);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
vector<char> ProxyProtocol::http1Request(tars::RequestPacket& request, Transceiver *trans)
|
||||
{
|
||||
@ -164,6 +123,48 @@ TC_NetWorkBuffer::PACKET_TYPE ProxyProtocol::http1Response(TC_NetWorkBuffer &in,
|
||||
return TC_NetWorkBuffer::PACKET_LESS;
|
||||
}
|
||||
|
||||
#if TARS_HTTP2
|
||||
|
||||
#define MAKE_NV(NAME, VALUE, VALUELEN) \
|
||||
{ \
|
||||
(uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, VALUELEN, \
|
||||
NGHTTP2_NV_FLAG_NONE \
|
||||
}
|
||||
|
||||
#define MAKE_NV2(NAME, VALUE) \
|
||||
{ \
|
||||
(uint8_t *)NAME, (uint8_t *)VALUE, sizeof(NAME) - 1, sizeof(VALUE) - 1, \
|
||||
NGHTTP2_NV_FLAG_NONE \
|
||||
}
|
||||
|
||||
#define MAKE_STRING_NV(NAME, VALUE) {(uint8_t*)(NAME.data()), (uint8_t*)(VALUE.data()), NAME.size(), VALUE.size(), NGHTTP2_NV_FLAG_NONE};
|
||||
|
||||
// nghttp2读取请求包体,准备发送
|
||||
static ssize_t reqbody_read_callback(nghttp2_session *session, int32_t stream_id,
|
||||
uint8_t *buf, size_t length,
|
||||
uint32_t *data_flags,
|
||||
nghttp2_data_source *source,
|
||||
void *user_data)
|
||||
{
|
||||
std::vector<char>* body = (std::vector<char>* )source->ptr;
|
||||
// cout << "reqbody_read_callback:" << body->size() << endl;
|
||||
|
||||
if (body->empty())
|
||||
{
|
||||
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ssize_t len = length > body->size() ? body->size() : length;
|
||||
memcpy(buf, body->data(), len);
|
||||
|
||||
vector<char>::iterator end = body->begin();
|
||||
std::advance(end, len);
|
||||
body->erase(body->begin(), end);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
// ENCODE function, called by network thread
|
||||
vector<char> ProxyProtocol::http2Request(RequestPacket& request, Transceiver *trans)
|
||||
{
|
||||
|
@ -138,10 +138,11 @@ public:
|
||||
*/
|
||||
ProxyProtocol() : requestFunc(streamRequest) {}
|
||||
|
||||
#if TARS_HTTP2
|
||||
static vector<char> http1Request(tars::RequestPacket& request, Transceiver *);
|
||||
static TC_NetWorkBuffer::PACKET_TYPE http1Response(TC_NetWorkBuffer &in, ResponsePacket& done);
|
||||
|
||||
#if TARS_HTTP2
|
||||
|
||||
// ENCODE function, called by network thread
|
||||
static vector<char> http2Request(tars::RequestPacket& request, Transceiver *);
|
||||
|
||||
|
@ -23,7 +23,9 @@
|
||||
#include <vector>
|
||||
#include <stdlib.h>
|
||||
|
||||
class MYSQL;
|
||||
struct st_mysql;
|
||||
|
||||
typedef struct st_mysql MYSQL;
|
||||
|
||||
namespace tars
|
||||
{
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
#include "util/tc_mysql.h"
|
||||
#include "mysql.h"
|
||||
#include "errmsg.h"
|
||||
#include <sstream>
|
||||
#include <string.h>
|
||||
|
@ -306,6 +306,7 @@ TC_NetWorkBuffer::PACKET_TYPE TC_NetWorkBuffer::checkHttp()
|
||||
|
||||
TC_NetWorkBuffer::PACKET_TYPE TC_NetWorkBuffer::parseHttp(TC_NetWorkBuffer&in, vector<char> &out)
|
||||
{
|
||||
cout << "parseHttp" << endl;
|
||||
TC_NetWorkBuffer::PACKET_TYPE b = in.checkHttp();
|
||||
|
||||
if (b == PACKET_FULL)
|
||||
|
Loading…
Reference in New Issue
Block a user