Skip to content

Commit

Permalink
<fix&perf>(sdk,boostssl): fix sdk service drop session when receive m…
Browse files Browse the repository at this point in the history
…sg in handshake phase, perf boostssl code in clang-tidy style. (FISCO-BCOS#4163)
  • Loading branch information
kyonRay authored Jan 10, 2024
1 parent 2b8e9cc commit b02da97
Show file tree
Hide file tree
Showing 17 changed files with 131 additions and 129 deletions.
4 changes: 2 additions & 2 deletions bcos-boostssl/bcos-boostssl/context/ContextBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ std::shared_ptr<boost::asio::ssl::context> ContextBuilder::buildSslContext(
std::shared_ptr<boost::asio::ssl::context> ContextBuilder::buildSslContext(
bool _server, const ContextConfig::SMCertConfig& _smCertConfig)
{
SSL_CTX* ctx = NULL;
SSL_CTX* ctx = nullptr;
if (_server)
{
const SSL_METHOD* meth = SSLv23_server_method();
Expand Down Expand Up @@ -230,7 +230,7 @@ std::shared_ptr<boost::asio::ssl::context> ContextBuilder::buildSslContextByCert
std::shared_ptr<boost::asio::ssl::context> ContextBuilder::buildSslContextByCertContent(
bool _server, const ContextConfig::SMCertConfig& _smCertConfig)
{
SSL_CTX* ctx = NULL;
SSL_CTX* ctx = nullptr;
if (_server)
{
const SSL_METHOD* meth = SSLv23_server_method();
Expand Down
7 changes: 4 additions & 3 deletions bcos-boostssl/bcos-boostssl/context/ContextConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/filesystem.hpp>
#include <boost/property_tree/ptree.hpp>
#include <memory>
#include <utility>

namespace bcos::boostssl::context
{
Expand Down Expand Up @@ -58,7 +59,7 @@ class ContextConfig
void setIsCertPath(bool _isCertPath) { m_isCertPath = _isCertPath; }

std::string sslType() const { return m_sslType; }
void setSslType(const std::string _sslType) { m_sslType = _sslType; }
void setSslType(std::string _sslType) { m_sslType = std::move(_sslType); }

const CertConfig& certConfig() const { return m_certConfig; }
void setCertConfig(const CertConfig& _certConfig) { m_certConfig = _certConfig; }
Expand All @@ -67,7 +68,7 @@ class ContextConfig
void setSmCertConfig(const SMCertConfig& _smCertConfig) { m_smCertConfig = _smCertConfig; }

std::string moduleName() { return m_moduleName; }
void setModuleName(std::string _moduleName) { m_moduleName = _moduleName; }
void setModuleName(std::string _moduleName) { m_moduleName = std::move(_moduleName); }


private:
Expand All @@ -81,4 +82,4 @@ class ContextConfig
std::string m_moduleName = "DEFAULT";
};

} // namespace bcos
} // namespace bcos::boostssl::context
8 changes: 5 additions & 3 deletions bcos-boostssl/bcos-boostssl/httpserver/HttpQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#pragma once
#include <bcos-boostssl/httpserver/Common.h>

#include <utility>

namespace bcos::boostssl::http
{
// The queue for http request pipeline
Expand All @@ -36,7 +38,7 @@ class Queue
public:
explicit Queue(std::size_t _limit = 16) : m_limit(_limit) { m_allResp.reserve(m_limit); }

void setSender(std::function<void(HttpResponsePtr)> _sender) { m_sender = _sender; }
void setSender(std::function<void(HttpResponsePtr)> _sender) { m_sender = std::move(_sender); }
std::function<void(HttpResponsePtr)> sender() const { return m_sender; }

std::size_t limit() const { return m_limit; }
Expand All @@ -62,12 +64,12 @@ class Queue
// enqueue and waiting called by the HTTP handler to send a response.
void enqueue(HttpResponsePtr _msg)
{
m_allResp.push_back(_msg);
m_allResp.push_back(std::move(_msg));
// there was no previous work, start this one
if (m_allResp.size() == 1)
{
m_sender(m_allResp.front());
}
}
};
} // namespace bcos
} // namespace bcos::boostssl::http
6 changes: 3 additions & 3 deletions bcos-boostssl/bcos-boostssl/httpserver/HttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <bcos-boostssl/httpserver/HttpServer.h>
#include <bcos-utilities/ThreadPool.h>
#include <memory>
#include <utility>

using namespace bcos;
using namespace bcos::boostssl;
Expand Down Expand Up @@ -228,12 +229,11 @@ HttpServer::Ptr HttpServerFactory::buildHttpServer(const std::string& _listenIP,
uint16_t _listenPort, std::shared_ptr<boost::asio::io_context> _ioc,
std::shared_ptr<boost::asio::ssl::context> _ctx, std::string _moduleName)
{
std::string m_moduleName = _moduleName;
// create httpserver and launch a listening port
auto server = std::make_shared<HttpServer>(_listenIP, _listenPort, _moduleName);
auto server = std::make_shared<HttpServer>(_listenIP, _listenPort, std::move(_moduleName));
auto acceptor = std::make_shared<boost::asio::ip::tcp::acceptor>((*_ioc));
auto httpStreamFactory = std::make_shared<HttpStreamFactory>();
server->setCtx(_ctx);
server->setCtx(std::move(_ctx));
server->setAcceptor(acceptor);
server->setHttpStreamFactory(httpStreamFactory);

Expand Down
26 changes: 16 additions & 10 deletions bcos-boostssl/bcos-boostssl/httpserver/HttpServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <bcos-utilities/IOServicePool.h>
#include <exception>
#include <thread>
#include <utility>
namespace bcos::boostssl::http
{
// The http server impl
Expand All @@ -32,8 +33,10 @@ class HttpServer : public std::enable_shared_from_this<HttpServer>
using Ptr = std::shared_ptr<HttpServer>;

public:
HttpServer(const std::string& _listenIP, uint16_t _listenPort, std::string _moduleName)
: m_listenIP(_listenIP), m_listenPort(_listenPort), m_moduleName(_moduleName)
HttpServer(std::string _listenIP, uint16_t _listenPort, std::string _moduleName)
: m_listenIP(std::move(_listenIP)),
m_listenPort(_listenPort),
m_moduleName(std::move(_moduleName))
{}

~HttpServer() { stop(); }
Expand All @@ -54,27 +57,30 @@ class HttpServer : public std::enable_shared_from_this<HttpServer>

public:
HttpReqHandler httpReqHandler() const { return m_httpReqHandler; }
void setHttpReqHandler(HttpReqHandler _httpReqHandler) { m_httpReqHandler = _httpReqHandler; }
void setHttpReqHandler(HttpReqHandler _httpReqHandler)
{
m_httpReqHandler = std::move(_httpReqHandler);
}

std::shared_ptr<boost::asio::ip::tcp::acceptor> acceptor() const { return m_acceptor; }
void setAcceptor(std::shared_ptr<boost::asio::ip::tcp::acceptor> _acceptor)
{
m_acceptor = _acceptor;
m_acceptor = std::move(_acceptor);
}

std::shared_ptr<boost::asio::ssl::context> ctx() const { return m_ctx; }
void setCtx(std::shared_ptr<boost::asio::ssl::context> _ctx) { m_ctx = _ctx; }
void setCtx(std::shared_ptr<boost::asio::ssl::context> _ctx) { m_ctx = std::move(_ctx); }

WsUpgradeHandler wsUpgradeHandler() const { return m_wsUpgradeHandler; }
void setWsUpgradeHandler(WsUpgradeHandler _wsUpgradeHandler)
{
m_wsUpgradeHandler = _wsUpgradeHandler;
m_wsUpgradeHandler = std::move(_wsUpgradeHandler);
}

HttpStreamFactory::Ptr httpStreamFactory() const { return m_httpStreamFactory; }
void setHttpStreamFactory(HttpStreamFactory::Ptr _httpStreamFactory)
{
m_httpStreamFactory = _httpStreamFactory;
m_httpStreamFactory = std::move(_httpStreamFactory);
}

bool disableSsl() const { return m_disableSsl; }
Expand All @@ -84,13 +90,13 @@ class HttpServer : public std::enable_shared_from_this<HttpServer>

void setIOServicePool(bcos::IOServicePool::Ptr _ioservicePool)
{
m_ioservicePool = _ioservicePool;
m_ioservicePool = std::move(_ioservicePool);
}

private:
std::string m_listenIP;
uint16_t m_listenPort;
bool m_disableSsl;
bool m_disableSsl = false;
std::string m_moduleName;

HttpReqHandler m_httpReqHandler;
Expand Down Expand Up @@ -123,4 +129,4 @@ class HttpServerFactory : public std::enable_shared_from_this<HttpServerFactory>
std::shared_ptr<boost::asio::ssl::context> _ctx, std::string _moduleName);
};

} // namespace bcos
} // namespace bcos::boostssl::http
27 changes: 12 additions & 15 deletions bcos-boostssl/bcos-boostssl/httpserver/HttpSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@
#include <mutex>
#include <utility>

namespace bcos
{
namespace boostssl
{
namespace http
namespace bcos::boostssl::http
{
// The http session for connection
class HttpSession : public std::enable_shared_from_this<HttpSession>
Expand All @@ -45,7 +41,7 @@ class HttpSession : public std::enable_shared_from_this<HttpSession>
using Ptr = std::shared_ptr<HttpSession>;

public:
HttpSession(std::string _moduleName) : m_moduleName(_moduleName)
explicit HttpSession(std::string _moduleName) : m_moduleName(std::move(_moduleName))
{
HTTP_SESSION(DEBUG) << LOG_KV("[NEWOBJ][HTTPSESSION]", this);
}
Expand Down Expand Up @@ -251,24 +247,27 @@ class HttpSession : public std::enable_shared_from_this<HttpSession>
}

HttpReqHandler httpReqHandler() const { return m_httpReqHandler; }
void setRequestHandler(HttpReqHandler _httpReqHandler) { m_httpReqHandler = _httpReqHandler; }
void setRequestHandler(HttpReqHandler _httpReqHandler)
{
m_httpReqHandler = std::move(_httpReqHandler);
}

WsUpgradeHandler wsUpgradeHandler() const { return m_wsUpgradeHandler; }
void setWsUpgradeHandler(WsUpgradeHandler _wsUpgradeHandler)
{
m_wsUpgradeHandler = _wsUpgradeHandler;
m_wsUpgradeHandler = std::move(_wsUpgradeHandler);
}
std::shared_ptr<Queue> queue() { return m_queue; }
void setQueue(std::shared_ptr<Queue> _queue) { m_queue = _queue; }
void setQueue(std::shared_ptr<Queue> _queue) { m_queue = std::move(_queue); }

HttpStream::Ptr httpStream() { return m_httpStream; }
void setHttpStream(HttpStream::Ptr _httpStream) { m_httpStream = _httpStream; }
void setHttpStream(HttpStream::Ptr _httpStream) { m_httpStream = std::move(_httpStream); }

std::shared_ptr<std::string> nodeId() { return m_nodeId; }
void setNodeId(std::shared_ptr<std::string> _nodeId) { m_nodeId = _nodeId; }
void setNodeId(std::shared_ptr<std::string> _nodeId) { m_nodeId = std::move(_nodeId); }

std::string moduleName() { return m_moduleName; }
void setModuleName(std::string _moduleName) { m_moduleName = _moduleName; }
void setModuleName(std::string _moduleName) { m_moduleName = std::move(_moduleName); }


private:
Expand All @@ -286,6 +285,4 @@ class HttpSession : public std::enable_shared_from_this<HttpSession>
std::string m_moduleName = "DEFAULT";
};

} // namespace http
} // namespace boostssl
} // namespace bcos
} // namespace bcos::boostssl::http
2 changes: 1 addition & 1 deletion bcos-boostssl/bcos-boostssl/websocket/WsConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ using namespace bcos::boostssl::context;

// TODO: how to set timeout for connect to wsServer ???
void WsConnector::connectToWsServer(const std::string& _host, uint16_t _port, bool _disableSsl,
std::function<void(boost::beast::error_code, const std::string& _extErrorMsg,
std::function<void(boost::beast::error_code, const std::string&,
std::shared_ptr<WsStreamDelegate>, std::shared_ptr<std::string>)>
_callback)
{
Expand Down
34 changes: 15 additions & 19 deletions bcos-boostssl/bcos-boostssl/websocket/WsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,13 @@ void WsService::reportConnectedNodes()
std::string WsService::genConnectError(
const std::string& _error, const std::string& endpoint, bool end)
{
std::string msg = _error;
msg += ":/";
msg += endpoint;
std::stringstream msg;
msg << _error << ":/" << endpoint;
if (!end)
{
msg += ", ";
msg << ", ";
}
return msg;
return msg.str();
}

void WsService::syncConnectToEndpoints(EndPointsPtr _peers)
Expand Down Expand Up @@ -423,15 +422,15 @@ std::shared_ptr<WsSession> WsService::newSession(
auto wsService = self.lock();
if (wsService)
{
wsService->onConnect(_error, _session);
wsService->onConnect(std::move(_error), std::move(_session));
}
});
session->setDisconnectHandler(
[self](Error::Ptr _error, std::shared_ptr<ws::WsSession> _session) {
auto wsService = self.lock();
if (wsService)
{
wsService->onDisconnect(_error, _session);
wsService->onDisconnect(std::move(_error), std::move(_session));
}
});
session->setRecvMessageHandler(
Expand Down Expand Up @@ -511,7 +510,6 @@ WsSessions WsService::sessions()

return sessions;
}

/**
* @brief: session connect
* @param _error:
Expand Down Expand Up @@ -609,14 +607,13 @@ void WsService::asyncSendMessageByEndPoint(const std::string& _endPoint,
return;
}

session->asyncSendMessage(_msg, _options, _respFunc);
session->asyncSendMessage(std::move(_msg), _options, _respFunc);
}

void WsService::asyncSendMessage(
std::shared_ptr<boostssl::MessageFace> _msg, Options _options, RespCallBack _respCallBack)
{
auto seq = _msg->seq();
return asyncSendMessage(sessions(), _msg, _options, _respCallBack);
return asyncSendMessage(sessions(), std::move(_msg), _options, std::move(_respCallBack));
}

void WsService::asyncSendMessage(const WsSessions& _ss, std::shared_ptr<boostssl::MessageFace> _msg,
Expand Down Expand Up @@ -670,9 +667,8 @@ void WsService::asyncSendMessage(const WsSessions& _ss, std::shared_ptr<boostssl
auto moduleName = session->moduleName();
// Note: should not pass session to the lamda operator[], this will lead to memory leak
session->asyncSendMessage(msg, options,
[self, endPoint, moduleName, callback = respFunc](Error::Ptr _error,
std::shared_ptr<boostssl::MessageFace> _msg,
std::shared_ptr<WsSession> _session) {
[self, endPoint, moduleName, callback = respFunc](
auto&& _error, auto&& _msg, auto&& _session) {
if (_error && _error->errorCode() != 0)
{
BOOST_SSL_LOG(WARNING)
Expand All @@ -694,12 +690,12 @@ void WsService::asyncSendMessage(const WsSessions& _ss, std::shared_ptr<boostssl

auto retry = std::make_shared<Retry>();
retry->ss = _ss;
retry->msg = _msg;
retry->msg = std::move(_msg);

retry->options = _options;
retry->respFunc = _respFunc;
retry->respFunc = std::move(_respFunc);

if (_respFunc)
if (retry->respFunc)
{
retry->trySendMessageWithCB();
}
Expand Down Expand Up @@ -737,12 +733,12 @@ void WsService::asyncSendMessage(const std::set<std::string>& _endPoints,
}
}

return asyncSendMessage(ss, _msg, _options, _respFunc);
return asyncSendMessage(ss, std::move(_msg), _options, std::move(_respFunc));
}

void WsService::broadcastMessage(std::shared_ptr<boostssl::MessageFace> _msg)
{
broadcastMessage(sessions(), _msg);
broadcastMessage(sessions(), std::move(_msg));
}

void WsService::broadcastMessage(
Expand Down
Loading

0 comments on commit b02da97

Please sign in to comment.