http2 in linux succ

This commit is contained in:
ruanshudong 2020-02-18 14:55:40 +08:00
parent d435becdd1
commit 539fb61b76
6 changed files with 38 additions and 74 deletions

View File

@ -263,6 +263,7 @@ ExternalProject_Add(${LIB_HTTP2}
BUILD_COMMAND make
INSTALL_COMMAND ${CMAKE_COMMAND} -E echo "install"
URL_MD5 5df375bbd532fcaa7cd4044b54b1188d
# URL_MD5 9521689460b4a57912acd3f88f301a3a
)
endif()

View File

@ -48,6 +48,7 @@ void syncRpc2(int c)
std::map<std::string, std::string> header;
header[":authority"] = "domain.com";
header[":scheme"] = "http";
// header[":method"] = "POST";
std::map<std::string, std::string> rheader;
//发起远程调用

View File

@ -128,7 +128,7 @@ vector<char> ProxyProtocol::http2Request(RequestPacket& request, Transceiver *tr
trans->getSendBuffer()->setContextData(session, [=]{delete session;});
session->settings();
session->settings(3000);
}
request.iRequestId = session->submit(request.sFuncName, request.sServantName, request.context, request.sBuffer);
@ -147,14 +147,6 @@ vector<char> ProxyProtocol::http2Request(RequestPacket& request, Transceiver *tr
TC_NetWorkBuffer::PACKET_TYPE ProxyProtocol::http2Response(TC_NetWorkBuffer &in, ResponsePacket& rsp)
{
TC_Http2Client* session = (TC_Http2Client*)((Transceiver*)(in.getConnection()))->getSendBuffer()->getContextData();
if(session == NULL)
{
session = new TC_Http2Client();
((Transceiver*)(in.getConnection()))->getSendBuffer()->setContextData(session, [=]{delete session;});
session->settings(3000);
}
pair<int, shared_ptr<TC_HttpResponse>> out;
TC_NetWorkBuffer::PACKET_TYPE flag = session->parseResponse(in, out);

View File

@ -214,18 +214,19 @@ int TC_ConsistentHashNew::getIndex(int32_t hashcode, unsigned int & iIndex)
size_t iCode = (hashcode & 0xFFFFFFFFL);
int low = 0;
size_t high = _vHashList.size();
int high = (int)_vHashList.size();
if(iCode <= _vHashList[0].iHashCode || iCode > _vHashList[high-1].iHashCode)
if(iCode <= (size_t)_vHashList[0].iHashCode || iCode > (size_t)_vHashList[high-1].iHashCode)
{
iIndex = _vHashList[0].iIndex;
return 0;
}
while (low < high - 1)
{
int mid = (low + high) / 2;
if (_vHashList[mid].iHashCode > iCode)
if ((size_t)_vHashList[mid].iHashCode > iCode)
{
high = mid;
}

View File

@ -414,29 +414,29 @@ string TC_File::load2str(const string &sFullFileName)
{
// #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
// ifstream ifs(sFullFileName.c_str());
ifstream ifs(sFullFileName.c_str());
// return string(istreambuf_iterator<char>(ifs), istreambuf_iterator<char>());
return string(istreambuf_iterator<char>(ifs), istreambuf_iterator<char>());
// #elif TARGET_PLATFORM_WINDOWS
std::ifstream::pos_type iLen = TC_File::getFileSize(sFullFileName);
FILE* fp = TC_Port::fopen(sFullFileName.c_str(), "rb");
// if (fopen_s(&fp, sFullFileName.c_str(), "rb") == 0)
if(fp != NULL)
{
char* sBuffer = new char[iLen];
size_t l = fread(sBuffer, 1, iLen, fp);
fclose(fp);
if (l != iLen)
{
delete[] sBuffer;
return "";
}
std::string s(sBuffer, iLen);
delete[] sBuffer;
return s;
}
return "";
// std::ifstream::pos_type iLen = TC_File::getFileSize(sFullFileName);
// FILE* fp = TC_Port::fopen(sFullFileName.c_str(), "rb");
// // if (fopen_s(&fp, sFullFileName.c_str(), "rb") == 0)
// if(fp != NULL)
// {
// char* sBuffer = new char[iLen];
// size_t l = fread(sBuffer, 1, iLen, fp);
// fclose(fp);
// if (l != iLen)
// {
// delete[] sBuffer;
// return "";
// }
// std::string s(sBuffer, iLen);
// delete[] sBuffer;
// return s;
// }
// return "";
// #endif
}
@ -461,7 +461,7 @@ bool TC_File::load2str(const string &sFullFileName, vector<char> &data)
// if (fopen_s(&fp, sFullFileName.c_str(), "rb") == 0)
{
data.resize(iLen);
size_t l = fread(data.data(), 1, iLen, fp);
int l = fread(data.data(), 1, iLen, fp);
fclose(fp);
if (l != iLen)
{

View File

@ -43,7 +43,7 @@ int TC_Http2::settings(unsigned int maxCurrentStreams)
{
nghttp2_settings_entry iv[2] = {
{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS, maxCurrentStreams},
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 100 * 1024 * 1024},
{NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, 1024 * 1024},
};
/* 24 bytes magic string also will be sent*/
@ -364,19 +364,6 @@ int TC_Http2Server::encodeResponse(const shared_ptr<TC_Http2Server::Http2Context
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
//
//void TC_Http2Client::Http2Response::swap(Http2Response& other)
//{
// if (this == &other)
// return;
//
// std::swap(streamId, other.streamId);
// headers.swap(other.headers);
// body.swap(other.body);
// std::swap(state, other.state);
//}
///////////////////////////////////////////////////////////////////////////////////////
namespace client
@ -384,7 +371,7 @@ namespace client
static ssize_t send_callback(nghttp2_session* session, const uint8_t* data, size_t length, int flags, void* user_data)
{
TC_Http2Client* nghttp2 = (TC_Http2Client* )user_data;
nghttp2->buffer().insert(nghttp2->buffer().end(), (const char*)data, (const char*)data + length);
nghttp2->insertBuff((const char*)data, length);
return length;
}
@ -397,9 +384,6 @@ static int on_begin_headers_callback(nghttp2_session* session, const nghttp2_fra
{
if (frame->headers.cat == NGHTTP2_HCAT_RESPONSE)
{
// TC_HttpResponse rsp;
// rsp.streamId = frame->hd.stream_id;
// rsp.state = TC_Http2Client::ResponseNone;
nghttp2->responses()[frame->hd.stream_id] = std::make_shared<TC_HttpResponse>();
}
}
@ -449,7 +433,6 @@ static int on_frame_recv_callback(nghttp2_session* session, const nghttp2_frame*
if (frame->hd.flags & NGHTTP2_FLAG_END_HEADERS)
{
;
// it->second.state = TC_Http2Client::ResponseHeadersDone;
}
return 0;
@ -484,8 +467,6 @@ static int on_stream_close_callback(nghttp2_session* session, int32_t stream_id,
return NGHTTP2_ERR_CALLBACK_FAILURE;
}
// it->second.state = TC_Http2Client::ResponseBodyDone;
nghttp2->doneResponses()[stream_id] = it->second;
nghttp2->responses().erase(it);
@ -494,19 +475,7 @@ static int on_stream_close_callback(nghttp2_session* session, int32_t stream_id,
}
#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};
#define MAKE_STRING_NV(NAME, VALUE) {(uint8_t*)(NAME.c_str()), (uint8_t*)(VALUE.c_str()), NAME.size(), VALUE.size(), NGHTTP2_NV_FLAG_NONE};
TC_Http2Client::TC_Http2Client()
{
@ -532,14 +501,14 @@ int TC_Http2Client::submit(const string &method, const string &path, const map<s
{
std::vector<nghttp2_nv> nva;
// const std::string method(":method");
nghttp2_nv nv1 = MAKE_STRING_NV(string(":method"), method);
const std::string smethod(":method");
nghttp2_nv nv1 = MAKE_STRING_NV(smethod, method);
if (!method.empty()) {
nva.push_back(nv1);
}
// const std::string path(":path");
nghttp2_nv nv2 = MAKE_STRING_NV(string(":path"), path);
const std::string spath(":path");
nghttp2_nv nv2 = MAKE_STRING_NV(spath, path);
if (!path.empty())
nva.push_back(nv2);
@ -572,9 +541,9 @@ int TC_Http2Client::submit(const string &method, const string &path, const map<s
return _err;
}
int sid = _err;
int _err = nghttp2_session_send(_session);
int sid = _err;
_err = nghttp2_session_send(_session);
if (_err != 0) {
return _err;
}