Skip to content

Commit

Permalink
<fix>(sdk,boostssl): fix ws service not destruct bug, fix sdk log ini…
Browse files Browse the repository at this point in the history
…t logic.
  • Loading branch information
kyonRay committed Jan 8, 2024
1 parent 637646e commit 321c3c4
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 79 deletions.
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 @@ -60,7 +60,7 @@ void WsConnector::connectToWsServer(const std::string& _host, uint16_t _port, bo
auto connector = shared_from_this();

// resolve host
resolver->async_resolve(_host.c_str(), std::to_string(_port).c_str(),
resolver->async_resolve(_host, std::to_string(_port),
[this, _host, _port, _disableSsl, endpoint, ioc, ctx, connector, builder, _callback](
boost::beast::error_code _ec, boost::asio::ip::tcp::resolver::results_type _results) {
if (_ec)
Expand Down
2 changes: 1 addition & 1 deletion bcos-boostssl/bcos-boostssl/websocket/WsConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class WsConnector : public std::enable_shared_from_this<WsConnector>
using Ptr = std::shared_ptr<WsConnector>;
using ConstPtr = std::shared_ptr<const WsConnector>;

WsConnector(std::shared_ptr<boost::asio::ip::tcp::resolver> _resolver)
explicit WsConnector(std::shared_ptr<boost::asio::ip::tcp::resolver> _resolver)
: m_resolver(std::move(_resolver))
{}

Expand Down
9 changes: 7 additions & 2 deletions bcos-boostssl/bcos-boostssl/websocket/WsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using namespace std::chrono_literals;
using namespace bcos::boostssl;
using namespace bcos::boostssl::ws;

WsService::WsService(std::string _moduleName) : m_moduleName(_moduleName)
WsService::WsService(std::string _moduleName) : m_moduleName(std::move(_moduleName))
{
WEBSOCKET_SERVICE(INFO) << LOG_KV("[NEWOBJ][WsService]", this);
}
Expand Down Expand Up @@ -126,6 +126,11 @@ void WsService::stop()
m_heartbeat->cancel();
}

if (m_httpServer)
{
m_httpServer->stop();
}

WEBSOCKET_SERVICE(INFO) << LOG_BADGE("stop") << LOG_DESC("stop websocket service successfully");
}

Expand Down Expand Up @@ -333,7 +338,7 @@ void WsService::reconnect()

if (!connectPeers->empty())
{
for (auto reconnectPeer : *connectPeers)
for (const auto& reconnectPeer : *connectPeers)
{
WEBSOCKET_SERVICE(INFO)
<< ("reconnect")
Expand Down
2 changes: 1 addition & 1 deletion bcos-boostssl/bcos-boostssl/websocket/WsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class WsService : public std::enable_shared_from_this<WsService>
{
public:
using Ptr = std::shared_ptr<WsService>;
WsService(std::string _moduleName = "DEFAULT");
explicit WsService(std::string _moduleName = "DEFAULT");
virtual ~WsService();

virtual void start();
Expand Down
20 changes: 13 additions & 7 deletions bcos-sdk/bcos-cpp-sdk/Sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
*/
#pragma once
#include "rpc/JsonRpcServiceImpl.h"
#include "utilities/logger/LogInitializer.h"
#include <bcos-boostssl/websocket/WsConfig.h>
#include <bcos-boostssl/websocket/WsService.h>
#include <bcos-cpp-sdk/amop/AMOP.h>
#include <bcos-cpp-sdk/event/EventSub.h>
#include <bcos-cpp-sdk/rpc/JsonRpcImpl.h>
#include <bcos-cpp-sdk/ws/Service.h>
#include <bcos-utilities/BoostLogInitializer.h>
#include <bcos-utilities/ThreadPool.h>
#include <memory>
#include <utility>
Expand All @@ -46,8 +48,11 @@ class Sdk
m_jsonRpc(std::move(_jsonRpc)),
m_amop(std::move(_amop)),
m_eventSub(std::move(_eventSub)),
m_jsonRpcService(std::move(_jsonRpcService))
{}
m_jsonRpcService(std::move(_jsonRpcService)),
m_logInitializer(LogInitializer())
{
m_logInitializer.initLog();
}
Sdk(bcos::cppsdk::service::Service::Ptr _service,
bcos::cppsdk::jsonrpc::JsonRpcImpl::Ptr _jsonRpc, bcos::cppsdk::amop::AMOP::Ptr _amop,
bcos::cppsdk::event::EventSub::Ptr _eventSub,
Expand All @@ -65,10 +70,16 @@ class Sdk
bcos::cppsdk::amop::AMOP::Ptr m_amop;
bcos::cppsdk::event::EventSub::Ptr m_eventSub;
bcos::cppsdk::jsonrpc::JsonRpcServiceImpl::Ptr m_jsonRpcService;
bcos::cppsdk::LogInitializer m_logInitializer;

public:
virtual void start()
{
if (m_service)
{
m_service->start();
}

if (m_jsonRpc)
{
m_jsonRpc->start();
Expand All @@ -83,11 +94,6 @@ class Sdk
{
m_eventSub->start();
}

if (m_service)
{
m_service->start();
}
}

virtual void stop()
Expand Down
15 changes: 5 additions & 10 deletions bcos-sdk/bcos-cpp-sdk/SdkFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,6 @@ using namespace bcos::cppsdk::jsonrpc;
using namespace bcos::cppsdk::event;
using namespace bcos::cppsdk::service;

SdkFactory::SdkFactory()
{
// TODO: how to init log in cpp sdk
LogInitializer::initLog();
}

bcos::cppsdk::Sdk::UniquePtr SdkFactory::buildSdk(
std::shared_ptr<bcos::boostssl::ws::WsConfig> _config, bool _sendRequestToHighestBlockNode)
{
Expand Down Expand Up @@ -94,20 +88,21 @@ Service::Ptr SdkFactory::buildService(std::shared_ptr<bcos::boostssl::ws::WsConf
auto initializer = std::make_shared<WsInitializer>();
initializer->setConfig(std::move(_config));
initializer->initWsService(service);
auto weakService = std::weak_ptr<Service>(service);
service->registerMsgHandler(
bcos::protocol::MessageType::BLOCK_NOTIFY, [service](auto&& _msg, auto&& _session) {
bcos::protocol::MessageType::BLOCK_NOTIFY, [weakService](auto&& _msg, auto&& _session) {
auto blkMsg = std::string(_msg->payload()->begin(), _msg->payload()->end());

auto service = weakService.lock();
service->onRecvBlockNotifier(blkMsg);

BCOS_LOG(INFO) << "[WS]" << LOG_DESC("receive block notify")
<< LOG_KV("endpoint", _session->endPoint()) << LOG_KV("blk", blkMsg);
});

service->registerMsgHandler(
bcos::protocol::MessageType::GROUP_NOTIFY, [service](auto&& _msg, auto&& _session) {
bcos::protocol::MessageType::GROUP_NOTIFY, [weakService](auto&& _msg, auto&& _session) {
std::string groupInfo = std::string(_msg->payload()->begin(), _msg->payload()->end());

auto service = weakService.lock();
service->onNotifyGroupInfo(groupInfo, _session);

BCOS_LOG(INFO) << "[WS]" << LOG_DESC("receive group info notify")
Expand Down
2 changes: 1 addition & 1 deletion bcos-sdk/bcos-cpp-sdk/SdkFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SdkFactory : public std::enable_shared_from_this<SdkFactory>
public:
using Ptr = std::shared_ptr<SdkFactory>;

SdkFactory();
SdkFactory() = default;

public:
bcos::cppsdk::service::Service::Ptr buildService(
Expand Down
4 changes: 4 additions & 0 deletions bcos-sdk/bcos-cpp-sdk/amop/AMOP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ void AMOP::start()
}
void AMOP::stop()
{
if (m_service)
{
m_service->stop();
}
AMOP_CLIENT(INFO) << LOG_BADGE("stop") << LOG_DESC("stop amop");
}

Expand Down
10 changes: 2 additions & 8 deletions bcos-sdk/bcos-cpp-sdk/amop/AMOP.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
#include <bcos-cpp-sdk/amop/TopicManager.h>
#include <unordered_map>

namespace bcos
{
namespace cppsdk
{
namespace amop
namespace bcos::cppsdk::amop
{
class AMOP : public AMOPInterface
{
Expand Down Expand Up @@ -136,6 +132,4 @@ class AMOP : public AMOPInterface

std::shared_ptr<bcos::boostssl::ws::WsService> m_service;
};
} // namespace amop
} // namespace cppsdk
} // namespace bcos
} // namespace bcos::cppsdk::amop
7 changes: 6 additions & 1 deletion bcos-sdk/bcos-cpp-sdk/event/EventSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void EventSub::start()
// start websocket service
m_service->start();

m_timer = std::make_shared<bcos::Timer>(m_config->reconnectPeriod(), "doLoop");
m_timer = std::make_shared<bcos::Timer>(m_config->reconnectPeriod(), "sdkEventLoop");
m_timer->registerTimeoutHandler([this]() { doLoop(); });
m_timer->start();
EVENT_SUB(INFO) << LOG_BADGE("start") << LOG_DESC("start event sub successfully")
Expand All @@ -71,6 +71,11 @@ void EventSub::stop()
if (m_timer)
{
m_timer->stop();
m_timer->destroy();
}
if (m_service)
{
m_service->stop();
}

EVENT_SUB(INFO) << LOG_BADGE("stop") << LOG_DESC("stop event sub successfully");
Expand Down
18 changes: 8 additions & 10 deletions bcos-sdk/bcos-cpp-sdk/event/EventSub.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
#include <mutex>
#include <utility>

namespace bcos
{
namespace cppsdk
{
namespace event
namespace bcos::cppsdk::event
{
class EventSub : public EventSubInterface
{
public:
using Ptr = std::shared_ptr<EventSub>;
using UniquePtr = std::unique_ptr<EventSub>;

EventSub() {}
virtual ~EventSub() { stop(); }
EventSub() = default;
~EventSub() override
{
stop();
EVENT_SUB(INFO) << LOG_KV("[DELOBJ][EventSub]", this);
}

public:
virtual void start() override;
Expand Down Expand Up @@ -132,6 +132,4 @@ class EventSub : public EventSubInterface
//
boostssl::ws::WsConfig::ConstPtr m_config;
};
} // namespace event
} // namespace cppsdk
} // namespace bcos
} // namespace bcos::cppsdk::event
4 changes: 4 additions & 0 deletions bcos-sdk/bcos-cpp-sdk/rpc/JsonRpcImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ void JsonRpcImpl::start()

void JsonRpcImpl::stop()
{
if (m_service)
{
m_service->stop();
}
RPCIMPL_LOG(INFO) << LOG_BADGE("stop") << LOG_DESC("stop rpc");
}

Expand Down
8 changes: 6 additions & 2 deletions bcos-sdk/bcos-cpp-sdk/rpc/JsonRpcImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@ class JsonRpcImpl : public JsonRpcInterface, public std::enable_shared_from_this
using UniquePtr = std::unique_ptr<JsonRpcImpl>;

public:
JsonRpcImpl(bcos::group::GroupInfoCodec::Ptr _groupInfoCodec)
explicit JsonRpcImpl(bcos::group::GroupInfoCodec::Ptr _groupInfoCodec)
: m_groupInfoCodec(std::move(_groupInfoCodec))
{}

~JsonRpcImpl() override { stop(); }
~JsonRpcImpl() override
{
stop();
BCOS_LOG(INFO) << LOG_KV("[DELOBJ][JsonRpcImpl]", this);
}

public:
void start() override;
Expand Down
23 changes: 0 additions & 23 deletions bcos-sdk/bcos-cpp-sdk/utilities/logger/LogInitializer.cpp

This file was deleted.

13 changes: 5 additions & 8 deletions bcos-sdk/bcos-cpp-sdk/utilities/logger/LogInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace cppsdk
class LogInitializer
{
public:
static void initLog(const std::string& _configPath = "./clog.ini")
void initLog(const std::string& _configPath = "./clog.ini")
{
boost::property_tree::ptree pt;
try
Expand All @@ -54,17 +54,14 @@ class LogInitializer
initLog(pt);
}

static void initLog(const boost::property_tree::ptree& _pt)
void initLog(const boost::property_tree::ptree& _pt)
{
std::call_once(m_flag, [_pt]() {
m_logInitializer = new bcos::BoostLogInitializer();
m_logInitializer->initLog(_pt, bcos::FileLogger, "cpp_sdk_log");
});
m_logInitializer = std::make_unique<bcos::BoostLogInitializer>();
m_logInitializer->initLog(_pt, bcos::FileLogger, "cpp_sdk_log");
}

private:
static std::once_flag m_flag;
static bcos::BoostLogInitializer* m_logInitializer;
std::unique_ptr<bcos::BoostLogInitializer> m_logInitializer;
};
} // namespace cppsdk
} // namespace bcos
2 changes: 2 additions & 0 deletions bcos-sdk/tests/unittests/tx/TransactionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ BOOST_AUTO_TEST_CASE(test_transaction_v2)
bcos::bytes input = fromHex(std::string("1a10012606636861696e30360667726f7570304100855628313331343736363932313039343437323233313631323634333933363134333635383636353733317d00010426608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033b0010b2d000020f03b8fdfa96a9b2128a83c22ddc691e6f1d9e0589b7c34088675e33cb059dbb23d000041313563c18509553f1d83747c004f29ec67f1137bb1c3104e8551ecc4c057187609c58a9d0220f02971abcf3f9bc6ba44423de56cb435b20b1fcf5eee4587840b01"));
// clang-format on
auto tx = transactionBuilderV2.decodeTransaction(input);
auto txJson = transactionBuilderV2.decodeTransactionToJsonObj(input);
std::cout << txJson << std::endl;
auto txData = tx->data;
auto hash = transactionBuilderV2.calculateTransactionDataHash(CryptoType::Secp256K1, tx->data);

Expand Down
12 changes: 8 additions & 4 deletions bcos-utilities/bcos-utilities/IOServicePool.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class IOServicePool
using ExecutorType = boost::asio::io_context::executor_type;
using Work = boost::asio::executor_work_guard<ExecutorType>;
using WorkPtr = std::unique_ptr<Work>;
IOServicePool(size_t _workerNum = std::thread::hardware_concurrency())
explicit IOServicePool(size_t _workerNum = std::thread::hardware_concurrency())
: m_works(_workerNum), m_nextIOService(0)
{
// create the ioservices
Expand All @@ -57,10 +57,14 @@ class IOServicePool
}

// one io_context per thread
for (size_t i = 0; i < m_ioServices.size(); ++i)
for (const auto& ioService : m_ioServices)
{
auto ioService = m_ioServices[i];
m_threads.emplace_back([ioService]() { ioService->run(); });
auto weakService = std::weak_ptr<IOService>(ioService);
m_threads.emplace_back([weakService]() {
bcos::pthread_setThreadName("ioService");
auto ioService = weakService.lock();
ioService->run();
});
}
}

Expand Down

0 comments on commit 321c3c4

Please sign in to comment.