diff --git a/bcos-boostssl/bcos-boostssl/context/ContextBuilder.cpp b/bcos-boostssl/bcos-boostssl/context/ContextBuilder.cpp index ef9e17cadc..986519db05 100644 --- a/bcos-boostssl/bcos-boostssl/context/ContextBuilder.cpp +++ b/bcos-boostssl/bcos-boostssl/context/ContextBuilder.cpp @@ -119,7 +119,7 @@ std::shared_ptr ContextBuilder::buildSslContext( std::shared_ptr 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(); @@ -230,7 +230,7 @@ std::shared_ptr ContextBuilder::buildSslContextByCert std::shared_ptr 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(); diff --git a/bcos-boostssl/bcos-boostssl/context/ContextConfig.h b/bcos-boostssl/bcos-boostssl/context/ContextConfig.h index a2e4bd676c..e75427fef5 100644 --- a/bcos-boostssl/bcos-boostssl/context/ContextConfig.h +++ b/bcos-boostssl/bcos-boostssl/context/ContextConfig.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace bcos::boostssl::context { @@ -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; } @@ -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: @@ -81,4 +82,4 @@ class ContextConfig std::string m_moduleName = "DEFAULT"; }; -} // namespace bcos \ No newline at end of file +} // namespace bcos::boostssl::context \ No newline at end of file diff --git a/bcos-boostssl/bcos-boostssl/httpserver/HttpQueue.h b/bcos-boostssl/bcos-boostssl/httpserver/HttpQueue.h index 5b4b911762..5a26e8a687 100644 --- a/bcos-boostssl/bcos-boostssl/httpserver/HttpQueue.h +++ b/bcos-boostssl/bcos-boostssl/httpserver/HttpQueue.h @@ -20,6 +20,8 @@ #pragma once #include +#include + namespace bcos::boostssl::http { // The queue for http request pipeline @@ -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 _sender) { m_sender = _sender; } + void setSender(std::function _sender) { m_sender = std::move(_sender); } std::function sender() const { return m_sender; } std::size_t limit() const { return m_limit; } @@ -62,7 +64,7 @@ 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) { @@ -70,4 +72,4 @@ class Queue } } }; -} // namespace bcos +} // namespace bcos::boostssl::http diff --git a/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.cpp b/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.cpp index 40c6641053..fb0a2fc896 100644 --- a/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.cpp +++ b/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.cpp @@ -22,6 +22,7 @@ #include #include #include +#include using namespace bcos; using namespace bcos::boostssl; @@ -228,12 +229,11 @@ HttpServer::Ptr HttpServerFactory::buildHttpServer(const std::string& _listenIP, uint16_t _listenPort, std::shared_ptr _ioc, std::shared_ptr _ctx, std::string _moduleName) { - std::string m_moduleName = _moduleName; // create httpserver and launch a listening port - auto server = std::make_shared(_listenIP, _listenPort, _moduleName); + auto server = std::make_shared(_listenIP, _listenPort, std::move(_moduleName)); auto acceptor = std::make_shared((*_ioc)); auto httpStreamFactory = std::make_shared(); - server->setCtx(_ctx); + server->setCtx(std::move(_ctx)); server->setAcceptor(acceptor); server->setHttpStreamFactory(httpStreamFactory); diff --git a/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.h b/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.h index 41f13f19ae..7078582a3e 100644 --- a/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.h +++ b/bcos-boostssl/bcos-boostssl/httpserver/HttpServer.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace bcos::boostssl::http { // The http server impl @@ -32,8 +33,10 @@ class HttpServer : public std::enable_shared_from_this using Ptr = std::shared_ptr; 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(); } @@ -54,27 +57,30 @@ class HttpServer : public std::enable_shared_from_this 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 acceptor() const { return m_acceptor; } void setAcceptor(std::shared_ptr _acceptor) { - m_acceptor = _acceptor; + m_acceptor = std::move(_acceptor); } std::shared_ptr ctx() const { return m_ctx; } - void setCtx(std::shared_ptr _ctx) { m_ctx = _ctx; } + void setCtx(std::shared_ptr _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; } @@ -84,13 +90,13 @@ class HttpServer : public std::enable_shared_from_this 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; @@ -123,4 +129,4 @@ class HttpServerFactory : public std::enable_shared_from_this std::shared_ptr _ctx, std::string _moduleName); }; -} // namespace bcos +} // namespace bcos::boostssl::http diff --git a/bcos-boostssl/bcos-boostssl/httpserver/HttpSession.h b/bcos-boostssl/bcos-boostssl/httpserver/HttpSession.h index 59c7b8e6c1..182cf415ce 100644 --- a/bcos-boostssl/bcos-boostssl/httpserver/HttpSession.h +++ b/bcos-boostssl/bcos-boostssl/httpserver/HttpSession.h @@ -32,11 +32,7 @@ #include #include -namespace bcos -{ -namespace boostssl -{ -namespace http +namespace bcos::boostssl::http { // The http session for connection class HttpSession : public std::enable_shared_from_this @@ -45,7 +41,7 @@ class HttpSession : public std::enable_shared_from_this using Ptr = std::shared_ptr; 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); } @@ -251,24 +247,27 @@ class HttpSession : public std::enable_shared_from_this } 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() { return m_queue; } - void setQueue(std::shared_ptr _queue) { m_queue = _queue; } + void setQueue(std::shared_ptr _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 nodeId() { return m_nodeId; } - void setNodeId(std::shared_ptr _nodeId) { m_nodeId = _nodeId; } + void setNodeId(std::shared_ptr _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: @@ -286,6 +285,4 @@ class HttpSession : public std::enable_shared_from_this std::string m_moduleName = "DEFAULT"; }; -} // namespace http -} // namespace boostssl -} // namespace bcos \ No newline at end of file +} // namespace bcos::boostssl::http diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsConnector.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsConnector.cpp index a1bbfce300..1959cdd0dc 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsConnector.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsConnector.cpp @@ -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, std::shared_ptr)> _callback) { diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp index 94850b287a..35e96b437d 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsService.cpp @@ -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) @@ -423,7 +422,7 @@ std::shared_ptr WsService::newSession( auto wsService = self.lock(); if (wsService) { - wsService->onConnect(_error, _session); + wsService->onConnect(std::move(_error), std::move(_session)); } }); session->setDisconnectHandler( @@ -431,7 +430,7 @@ std::shared_ptr WsService::newSession( auto wsService = self.lock(); if (wsService) { - wsService->onDisconnect(_error, _session); + wsService->onDisconnect(std::move(_error), std::move(_session)); } }); session->setRecvMessageHandler( @@ -511,7 +510,6 @@ WsSessions WsService::sessions() return sessions; } - /** * @brief: session connect * @param _error: @@ -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 _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 _msg, @@ -670,9 +667,8 @@ void WsService::asyncSendMessage(const WsSessions& _ss, std::shared_ptrmoduleName(); // 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 _msg, - std::shared_ptr _session) { + [self, endPoint, moduleName, callback = respFunc]( + auto&& _error, auto&& _msg, auto&& _session) { if (_error && _error->errorCode() != 0) { BOOST_SSL_LOG(WARNING) @@ -694,12 +690,12 @@ void WsService::asyncSendMessage(const WsSessions& _ss, std::shared_ptr(); 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(); } @@ -737,12 +733,12 @@ void WsService::asyncSendMessage(const std::set& _endPoints, } } - return asyncSendMessage(ss, _msg, _options, _respFunc); + return asyncSendMessage(ss, std::move(_msg), _options, std::move(_respFunc)); } void WsService::broadcastMessage(std::shared_ptr _msg) { - broadcastMessage(sessions(), _msg); + broadcastMessage(sessions(), std::move(_msg)); } void WsService::broadcastMessage( diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsService.h b/bcos-boostssl/bcos-boostssl/websocket/WsService.h index b93191668b..d53a61afdb 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsService.h +++ b/bcos-boostssl/bcos-boostssl/websocket/WsService.h @@ -53,7 +53,7 @@ using MsgHandler = using ConnectHandler = std::function)>; using DisconnectHandler = std::function)>; using HandshakeHandler = std::function, std::shared_ptr)>; + bcos::Error::Ptr, std::shared_ptr, std::shared_ptr)>; class WsService : public std::enable_shared_from_this { @@ -71,7 +71,8 @@ class WsService : public std::enable_shared_from_this std::promise>>>> asyncConnectToEndpoints(EndPointsPtr _peers); - std::string genConnectError(const std::string& _error, const std::string& endpoint, bool end); + inline static std::string genConnectError( + const std::string& _error, const std::string& endpoint, bool end); void syncConnectToEndpoints(EndPointsPtr _peers); std::shared_ptr newSession( @@ -107,44 +108,50 @@ class WsService : public std::enable_shared_from_this std::shared_ptr messageFactory() { return m_messageFactory; } void setMessageFactory(std::shared_ptr _messageFactory) { - m_messageFactory = _messageFactory; + m_messageFactory = std::move(_messageFactory); } std::shared_ptr sessionFactory() { return m_sessionFactory; } void setSessionFactory(std::shared_ptr _sessionFactory) { - m_sessionFactory = _sessionFactory; + m_sessionFactory = std::move(_sessionFactory); } int32_t waitConnectFinishTimeout() const { return m_waitConnectFinishTimeout; } void setWaitConnectFinishTimeout(int32_t _timeout) { m_waitConnectFinishTimeout = _timeout; } - std::string moduleName() { return m_moduleName; } - void setModuleName(std::string _moduleName) { m_moduleName = _moduleName; } + std::string moduleName() const noexcept { return m_moduleName; } + void setModuleName(std::string _moduleName) { m_moduleName = std::move(_moduleName); } void setIOServicePool(IOServicePool::Ptr _ioservicePool) { - m_ioservicePool = _ioservicePool; + m_ioservicePool = std::move(_ioservicePool); m_timerIoc = m_ioservicePool->getIOService(); } - std::shared_ptr connector() const { return m_connector; } - void setConnector(std::shared_ptr _connector) { m_connector = _connector; } + std::shared_ptr connector() const noexcept { return m_connector; } + void setConnector(std::shared_ptr _connector) + { + m_connector = std::move(_connector); + } void setHostPort(std::string host, uint16_t port) { - m_listenHost = host; + m_listenHost = std::move(host); m_listenPort = port; } - std::string listenHost() { return m_listenHost; } - uint16_t listenPort() { return m_listenPort; } + std::string listenHost() const noexcept { return m_listenHost; } + uint16_t listenPort() const noexcept { return m_listenPort; } - WsConfig::Ptr config() const { return m_config; } - void setConfig(WsConfig::Ptr _config) { m_config = _config; } + WsConfig::Ptr config() const noexcept { return m_config; } + void setConfig(WsConfig::Ptr _config) { m_config = std::move(_config); } - std::shared_ptr httpServer() const { return m_httpServer; } + std::shared_ptr httpServer() const noexcept + { + return m_httpServer; + } void setHttpServer(std::shared_ptr _httpServer) { - m_httpServer = _httpServer; + m_httpServer = std::move(_httpServer); } bool registerMsgHandler(uint16_t _msgType, MsgHandler _msgHandler); @@ -155,23 +162,23 @@ class WsService : public std::enable_shared_from_this void registerConnectHandler(ConnectHandler _connectHandler) { - m_connectHandlers.push_back(_connectHandler); + m_connectHandlers.push_back(std::move(_connectHandler)); } void registerDisconnectHandler(DisconnectHandler _disconnectHandler) { - m_disconnectHandlers.push_back(_disconnectHandler); + m_disconnectHandlers.push_back(std::move(_disconnectHandler)); } void registerHandshakeHandler(HandshakeHandler _handshakeHandler) { - m_handshakeHandlers.push_back(_handshakeHandler); + m_handshakeHandlers.push_back(std::move(_handshakeHandler)); } void setReconnectedPeers(EndPointsPtr _reconnectedPeers) { WriteGuard l(x_peers); - m_reconnectedPeers = _reconnectedPeers; + m_reconnectedPeers = std::move(_reconnectedPeers); } EndPointsPtr reconnectedPeers() const { diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp b/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp index 920e708708..c7e9010a0b 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp +++ b/bcos-boostssl/bcos-boostssl/websocket/WsSession.cpp @@ -130,7 +130,10 @@ void WsSession::startAsServer(HttpRequest _httpRequest) WEBSOCKET_SESSION(INFO) << LOG_BADGE("startAsServer") << LOG_DESC("start websocket handshake") << LOG_KV("endPoint", m_endPoint) << LOG_KV("session", this); m_wsStreamDelegate->asyncAccept( - _httpRequest, std::bind(&WsSession::onWsAccept, shared_from_this(), std::placeholders::_1)); + std::move(_httpRequest), [self = weak_from_this()](auto&& code) { + auto session = self.lock(); + session->onWsAccept(std::forward(code)); + }); } void WsSession::onWsAccept(boost::beast::error_code _ec) @@ -424,7 +427,7 @@ void WsSession::asyncSendMessage( void WsSession::addRespCallback(const std::string& _seq, CallBack::Ptr _callback) { WriteGuard lock(x_callback); - m_callbacks[_seq] = _callback; + m_callbacks[_seq] = std::move(_callback); } WsSession::CallBack::Ptr WsSession::getAndRemoveRespCallback( diff --git a/bcos-boostssl/bcos-boostssl/websocket/WsSession.h b/bcos-boostssl/bcos-boostssl/websocket/WsSession.h index 62f283150e..879064ed45 100644 --- a/bcos-boostssl/bcos-boostssl/websocket/WsSession.h +++ b/bcos-boostssl/bcos-boostssl/websocket/WsSession.h @@ -37,6 +37,7 @@ #include #include #include +#include namespace bcos::boostssl::ws { @@ -91,29 +92,32 @@ class WsSession : public std::enable_shared_from_this, m_connectedEndPoint = _connectedEndPoint; } - void setConnectHandler(WsConnectHandler _connectHandler) { m_connectHandler = _connectHandler; } + void setConnectHandler(WsConnectHandler _connectHandler) + { + m_connectHandler = std::move(_connectHandler); + } WsConnectHandler connectHandler() { return m_connectHandler; } void setDisconnectHandler(WsDisconnectHandler _disconnectHandler) { - m_disconnectHandler = _disconnectHandler; + m_disconnectHandler = std::move(_disconnectHandler); } WsDisconnectHandler disconnectHandler() { return m_disconnectHandler; } void setRecvMessageHandler(WsRecvMessageHandler _recvMessageHandler) { - m_recvMessageHandler = _recvMessageHandler; + m_recvMessageHandler = std::move(_recvMessageHandler); } const WsRecvMessageHandler& recvMessageHandler() { return m_recvMessageHandler; } std::shared_ptr messageFactory() { return m_messageFactory; } void setMessageFactory(std::shared_ptr _messageFactory) { - m_messageFactory = _messageFactory; + m_messageFactory = std::move(_messageFactory); } std::shared_ptr ioc() const { return m_ioc; } - void setIoc(std::shared_ptr _ioc) { m_ioc = _ioc; } + void setIoc(std::shared_ptr _ioc) { m_ioc = std::move(_ioc); } void setVersion(uint16_t _version) { m_version.store(_version); } uint16_t version() const { return m_version.load(); } @@ -121,7 +125,7 @@ class WsSession : public std::enable_shared_from_this, WsStreamDelegate::Ptr wsStreamDelegate() { return m_wsStreamDelegate; } void setWsStreamDelegate(WsStreamDelegate::Ptr _wsStreamDelegate) { - m_wsStreamDelegate = _wsStreamDelegate; + m_wsStreamDelegate = std::move(_wsStreamDelegate); } boost::beast::flat_buffer& buffer() { return m_buffer; } @@ -140,10 +144,10 @@ class WsSession : public std::enable_shared_from_this, } std::string nodeId() { return m_nodeId; } - void setNodeId(std::string _nodeId) { m_nodeId = _nodeId; } + void setNodeId(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); } bool needCheckRspPacket() { return m_needCheckRspPacket; } void setNeedCheckRspPacket(bool _needCheckRespPacket) diff --git a/bcos-sdk/bcos-cpp-sdk/SdkFactory.cpp b/bcos-sdk/bcos-cpp-sdk/SdkFactory.cpp index c5913ad472..7a260b28cb 100644 --- a/bcos-sdk/bcos-cpp-sdk/SdkFactory.cpp +++ b/bcos-sdk/bcos-cpp-sdk/SdkFactory.cpp @@ -104,7 +104,7 @@ Service::Ptr SdkFactory::buildService(std::shared_ptrpayload()->begin(), _msg->payload()->end()); auto service = weakService.lock(); - service->onNotifyGroupInfo(groupInfo, _session); + service->onNotifyGroupInfo(groupInfo, _session->endPoint()); BCOS_LOG(INFO) << "[WS]" << LOG_DESC("receive group info notify") << LOG_KV("endpoint", _session->endPoint()) diff --git a/bcos-sdk/bcos-cpp-sdk/rpc/JsonRpcServiceInterface.h b/bcos-sdk/bcos-cpp-sdk/rpc/JsonRpcServiceInterface.h index 445749643f..0c44042c61 100644 --- a/bcos-sdk/bcos-cpp-sdk/rpc/JsonRpcServiceInterface.h +++ b/bcos-sdk/bcos-cpp-sdk/rpc/JsonRpcServiceInterface.h @@ -26,11 +26,7 @@ #include #include -namespace bcos -{ -namespace cppsdk -{ -namespace jsonrpc +namespace bcos::cppsdk::jsonrpc { class JsonRpcServiceInterface @@ -40,7 +36,7 @@ class JsonRpcServiceInterface using UniquePtr = std::unique_ptr; JsonRpcServiceInterface() = default; - virtual ~JsonRpcServiceInterface() {} + virtual ~JsonRpcServiceInterface() = default; public: virtual std::string sendTransaction(const bcos::crypto::KeyPairInterface& _keyPair, @@ -49,6 +45,4 @@ class JsonRpcServiceInterface RespFunc _respFunc) = 0; }; -} // namespace jsonrpc -} // namespace cppsdk -} // namespace bcos \ No newline at end of file +} // namespace bcos::cppsdk::jsonrpc diff --git a/bcos-sdk/bcos-cpp-sdk/ws/HandshakeResponse.h b/bcos-sdk/bcos-cpp-sdk/ws/HandshakeResponse.h index 6c37b0b27c..85992d7a8e 100644 --- a/bcos-sdk/bcos-cpp-sdk/ws/HandshakeResponse.h +++ b/bcos-sdk/bcos-cpp-sdk/ws/HandshakeResponse.h @@ -23,28 +23,24 @@ #include #include #include +#include - -namespace bcos -{ -namespace cppsdk -{ -namespace service +namespace bcos::cppsdk::service { class HandshakeResponse { public: using Ptr = std::shared_ptr; using ConstPtr = std::shared_ptr; - HandshakeResponse(bcos::group::GroupInfoCodec::Ptr _groupInfoCodec) - : m_groupInfoCodec(_groupInfoCodec) + explicit HandshakeResponse(bcos::group::GroupInfoCodec::Ptr _groupInfoCodec) + : m_groupInfoCodec(std::move(_groupInfoCodec)) {} - virtual ~HandshakeResponse() {} + virtual ~HandshakeResponse() = default; virtual bool decode(std::string const& _data); virtual void encode(std::string& _encodedData) const; - int protocolVersion() const { return m_protocolVersion; } + uint32_t protocolVersion() const noexcept { return m_protocolVersion; } const std::vector& groupInfoList() const { return m_groupInfoList; @@ -69,10 +65,8 @@ class HandshakeResponse std::unordered_map m_groupBlockNumber; bcos::group::GroupInfoCodec::Ptr m_groupInfoCodec; // Note: the nodes determine the protocol version - uint32_t m_protocolVersion; + uint32_t m_protocolVersion{}; bcos::protocol::ProtocolInfo::Ptr m_localProtocol; }; -} // namespace service -} // namespace cppsdk -} // namespace bcos \ No newline at end of file +} // namespace bcos::cppsdk::service diff --git a/bcos-sdk/bcos-cpp-sdk/ws/Service.cpp b/bcos-sdk/bcos-cpp-sdk/ws/Service.cpp index 26a08804a4..5bbb0c6285 100644 --- a/bcos-sdk/bcos-cpp-sdk/ws/Service.cpp +++ b/bcos-sdk/bcos-cpp-sdk/ws/Service.cpp @@ -126,9 +126,9 @@ void Service::onRecvMessage(std::shared_ptr _msg, std::shared_ptrendPoint() : std::string("")) - << LOG_KV("seq", seq); + << LOG_KV("type", _msg->packetType()) << LOG_KV("seq", seq); - _session->drop(bcos::boostssl::ws::WsError::UserDisconnect); + // _session->drop(bcos::boostssl::ws::WsError::UserDisconnect); return; } @@ -202,7 +202,7 @@ void Service::asyncSendMessageByGroupAndNode(const std::string& _group, const st // ---------------------send message end--------------------------------------------------------- -bool Service::checkHandshakeDone(std::shared_ptr _session) +bool Service::checkHandshakeDone(std::shared_ptr const& _session) { return _session && _session->version(); } @@ -292,10 +292,8 @@ void Service::startHandshake(std::shared_ptr _ses } -void Service::onNotifyGroupInfo( - const std::string& _groupInfoJson, std::shared_ptr _session) +void Service::onNotifyGroupInfo(const std::string& _groupInfoJson, const std::string& endPoint) { - std::string endPoint = _session->endPoint(); RPC_WS_LOG(TRACE) << LOG_BADGE("onNotifyGroupInfo") << LOG_KV("endPoint", endPoint) << LOG_KV("groupInfoJson", _groupInfoJson); @@ -312,14 +310,14 @@ void Service::onNotifyGroupInfo( } } -void Service::onNotifyGroupInfo(std::shared_ptr _msg, - std::shared_ptr _session) +void Service::onNotifyGroupInfo( + std::shared_ptr _msg, const std::string& endPoint) { std::string groupInfo = std::string(_msg->payload()->begin(), _msg->payload()->end()); RPC_WS_LOG(INFO) << LOG_BADGE("onNotifyGroupInfo") << LOG_KV("groupInfo", groupInfo); - return onNotifyGroupInfo(groupInfo, std::move(_session)); + return onNotifyGroupInfo(groupInfo, endPoint); } void Service::clearGroupInfoByEp(const std::string& _endPoint) diff --git a/bcos-sdk/bcos-cpp-sdk/ws/Service.h b/bcos-sdk/bcos-cpp-sdk/ws/Service.h index 0d507007dc..2603aab75a 100644 --- a/bcos-sdk/bcos-cpp-sdk/ws/Service.h +++ b/bcos-sdk/bcos-cpp-sdk/ws/Service.h @@ -29,6 +29,7 @@ #include #include #include +#include #include namespace bcos::cppsdk::service @@ -70,15 +71,14 @@ class Service : public bcos::boostssl::ws::WsService // ---------------------oversend message begin---------------------------- virtual void startHandshake(std::shared_ptr _session); - virtual bool checkHandshakeDone(std::shared_ptr _session); + virtual bool checkHandshakeDone(std::shared_ptr const& _session); void clearGroupInfoByEp(const std::string& _endPoint); void clearGroupInfoByEp(const std::string& _endPoint, const std::string& _groupID); void updateGroupInfoByEp(const std::string& _endPoint, bcos::group::GroupInfo::Ptr _groupInfo); + void onNotifyGroupInfo(const std::string& _groupInfo, const std::string& endPoint); void onNotifyGroupInfo( - const std::string& _groupInfo, std::shared_ptr _session); - void onNotifyGroupInfo(std::shared_ptr _msg, - std::shared_ptr _session); + std::shared_ptr _msg, const std::string& endPoint); //------------------------------ Block Notifier begin -------------------------- bool getBlockNumber(const std::string& _group, int64_t& _blockNumber); @@ -118,10 +118,10 @@ class Service : public bcos::boostssl::ws::WsService void registerWsHandshakeSucHandler(WsHandshakeSucHandler _handler) { - m_wsHandshakeSucHandlers.push_back(_handler); + m_wsHandshakeSucHandlers.push_back(std::move(_handler)); } - void callWsHandshakeSucHandlers(std::shared_ptr _session) + void callWsHandshakeSucHandlers(const std::shared_ptr& _session) { for (auto& handler : m_wsHandshakeSucHandlers) { diff --git a/bcos-sdk/sample/tx/deploy_hello.cpp b/bcos-sdk/sample/tx/deploy_hello.cpp index 49785063fe..0ba28ce936 100644 --- a/bcos-sdk/sample/tx/deploy_hello.cpp +++ b/bcos-sdk/sample/tx/deploy_hello.cpp @@ -132,108 +132,171 @@ void usage() std::exit(0); } -int main(int argc, char** argv) +void* bcos_sdk_create_by_config_file(const char* config_file) { - if (argc < 3) + try { - usage(); + // construct sdk object + auto factory = std::make_shared(); + auto sdk = factory->buildSdk(config_file); + auto sdkPointer = sdk.release(); + + BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_create_by_config_file") << LOG_DESC("[NEWOBJ]") + << LOG_KV("sdk", sdkPointer); + return sdkPointer; } - - std::string config = argv[1]; - std::string group = argv[2]; - - std::cout << LOG_DESC(" [DeployHello] params ===>>>> ") << LOG_KV("\n\t # config", config) - << LOG_KV("\n\t # groupID", group) << std::endl; - - auto factory = std::make_shared(); - // construct cpp-sdk object - auto sdk = factory->buildSdk(config); - // start sdk - sdk->start(); - - std::cout << LOG_DESC(" [DeployHello] start sdk ... ") << std::endl; - - // get group info - bcos::group::GroupInfo::Ptr groupInfo = sdk->service()->getGroupInfo(group); - if (!groupInfo) + catch (const std::exception& e) { - std::cout << LOG_DESC(" [DeployHello] group not exist") << LOG_KV("group", group) - << std::endl; - exit(-1); + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_create_by_config_file") + << LOG_KV("configFile", config_file) << LOG_KV("errorMsg", errorMsg); } + return nullptr; +} - crypto::SignatureCrypto::Ptr keyPairFactory; - crypto::KeyPairInterface::UniquePtr keyPair; - if (groupInfo->smCryptoType()) +void bcos_sdk_start(void* sdk) +{ + try { - keyPairFactory = std::make_shared(); - keyPair = keyPairFactory->generateKeyPair(); + ((bcos::cppsdk::Sdk*)sdk)->start(); } - else + catch (const std::exception& e) { - keyPairFactory = std::make_shared(); - keyPair = keyPairFactory->generateKeyPair(); + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(ERROR) << LOG_BADGE("bcos_sdk_start") << LOG_KV("sdk", sdk) + << LOG_KV("errorMsg", errorMsg); } - std::cout << LOG_DESC(" [DeployHello] sm_crypto_type ") << groupInfo->smCryptoType() - << std::endl; - - std::cout << LOG_DESC(" [DeployHello] new account ") - << LOG_KV("address", keyPair->publicKey()->hex()) << std::endl; - - int64_t blockLimit = -1; - sdk->service()->getBlockLimit(group, blockLimit); - - std::cout << LOG_DESC(" [DeployHello] block limit ") << LOG_KV("blockLimit", blockLimit) - << std::endl; - - auto hexBin = getBinary(groupInfo->smCryptoType()); - auto binBytes = fromHexString(std::string(hexBin)); - - auto rpcService = sdk->jsonRpcService(); + BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_start") << LOG_KV("sdk", sdk); +} - std::promise p; - auto f = p.get_future(); - rpcService->sendTransaction(*keyPair, group, "", "", std::move(*binBytes), "", 0, "extraData", - [&p](bcos::Error::Ptr _error, std::shared_ptr _resp) { - if (_error && _error->errorCode() != 0) - { - std::cout << LOG_DESC(" [DeployHello] send transaction response error") - << LOG_KV("errorCode", _error->errorCode()) - << LOG_KV("errorMessage", _error->errorMessage()) << std::endl; - } - else - { - std::string receipt = std::string(_resp->begin(), _resp->end()); - std::cout << LOG_DESC(" [DeployHello] recv response success ") - << LOG_KV("transaction receipt", receipt) << std::endl; +int main(int argc, char** argv) +{ + if (argc < 3) + { + usage(); + } - Json::Value root; - Json::Reader jsonReader; + std::string config = argv[1]; + std::string group = argv[2]; - try - { - if (!jsonReader.parse(receipt, root)) - { - std::cout << LOG_DESC(" [DeployHello] [ERROR] recv invalid json object") - << LOG_KV("resp", receipt) << std::endl; - return; - } + std::cout << LOG_DESC(" [DeployHello] params ===>>>> ") << LOG_KV("\n\t # config", config) + << LOG_KV("\n\t # groupID", group) << std::endl; + // { + // // 1. create sdk object by config + // void* sdk = bcos_sdk_create_by_config_file(config.c_str()); + // if (!sdk) + // { + // printf(" [HelloSample] bcos_sdk_create_by_config_file failed\n"); + // exit(-1); + // } + // printf(" [HelloSample] start sdk ... \n"); + // + // // 2. start bcos c sdk + // bcos_sdk_start(sdk); + // + // printf(" [HelloSample] start sdk ... \n"); + // } + // printf(" [HelloSample] start sdk ... \n"); + // printf(" [HelloSample] start sdk ... \n"); + // + // exit(0); + { + auto factory = std::make_shared(); + // construct cpp-sdk object + auto sdk = factory->buildSdk(config); + // start sdk + sdk->start(); + + std::cout << LOG_DESC(" [DeployHello] start sdk ... ") << std::endl; + + // get group info + bcos::group::GroupInfo::Ptr groupInfo = sdk->service()->getGroupInfo(group); + if (!groupInfo) + { + std::cout << LOG_DESC(" [DeployHello] group not exist") << LOG_KV("group", group) + << std::endl; + exit(-1); + } + } - std::cout << LOG_DESC(" [DeployHello] contract address ==> " + - root["result"]["contractAddress"].asString()) - << std::endl; - } - catch (const std::exception& _e) - { - std::cout << LOG_DESC(" [DeployHello] [ERROR] recv invalid json object") - << LOG_KV("resp", receipt) << std::endl; - } - } - p.set_value(true); - }); + std::cout << LOG_DESC(" [DeployHello] start sdk ... ") << std::endl; + std::cout << LOG_DESC(" [DeployHello] start sdk ... ") << std::endl; + return 0; - f.get(); + // crypto::SignatureCrypto::Ptr keyPairFactory; + // crypto::KeyPairInterface::UniquePtr keyPair; + // if (groupInfo->smCryptoType()) + // { + // keyPairFactory = std::make_shared(); + // keyPair = keyPairFactory->generateKeyPair(); + // } + // else + // { + // keyPairFactory = std::make_shared(); + // keyPair = keyPairFactory->generateKeyPair(); + // } + // + // std::cout << LOG_DESC(" [DeployHello] sm_crypto_type ") << groupInfo->smCryptoType() + // << std::endl; + // + // std::cout << LOG_DESC(" [DeployHello] new account ") + // << LOG_KV("address", keyPair->publicKey()->hex()) << std::endl; + // + // int64_t blockLimit = -1; + // sdk->service()->getBlockLimit(group, blockLimit); + // + // std::cout << LOG_DESC(" [DeployHello] block limit ") << LOG_KV("blockLimit", blockLimit) + // << std::endl; + // + // auto hexBin = getBinary(groupInfo->smCryptoType()); + // auto binBytes = fromHexString(std::string(hexBin)); + // + // auto rpcService = sdk->jsonRpcService(); + // + // std::promise p; + // auto f = p.get_future(); + // rpcService->sendTransaction(*keyPair, group, "", "", std::move(*binBytes), "", 0, + // "extraData", + // [&p](bcos::Error::Ptr _error, std::shared_ptr _resp) { + // if (_error && _error->errorCode() != 0) + // { + // std::cout << LOG_DESC(" [DeployHello] send transaction response error") + // << LOG_KV("errorCode", _error->errorCode()) + // << LOG_KV("errorMessage", _error->errorMessage()) << std::endl; + // } + // else + // { + // std::string receipt = std::string(_resp->begin(), _resp->end()); + // std::cout << LOG_DESC(" [DeployHello] recv response success ") + // << LOG_KV("transaction receipt", receipt) << std::endl; + // + // Json::Value root; + // Json::Reader jsonReader; + // + // try + // { + // if (!jsonReader.parse(receipt, root)) + // { + // std::cout << LOG_DESC(" [DeployHello] [ERROR] recv invalid json object") + // << LOG_KV("resp", receipt) << std::endl; + // return; + // } + // + // std::cout << LOG_DESC(" [DeployHello] contract address ==> " + + // root["result"]["contractAddress"].asString()) + // << std::endl; + // } + // catch (const std::exception& _e) + // { + // std::cout << LOG_DESC(" [DeployHello] [ERROR] recv invalid json object") + // << LOG_KV("resp", receipt) << std::endl; + // } + // } + // p.set_value(true); + // }); + // + // f.get(); /* auto transactionBuilderService = @@ -294,5 +357,5 @@ int main(int argc, char** argv) f.get(); */ - return 0; + // return 0; } \ No newline at end of file