From 6f810af60e4f985a4cc90e8e562357037c1cc8d5 Mon Sep 17 00:00:00 2001 From: Soo Hwan Na Date: Wed, 26 Jun 2024 13:51:12 +0900 Subject: [PATCH] TgBot++: socket: ClientBackend: Add ability to store the path - Don't blindly hardcode it to main path every time --- src/database/utils/SendMediaToChat.cc | 6 +++++- src/logging/LoggingClient.cpp | 4 +++- src/socket/TgBotSocketClient.cpp | 5 ++++- src/socket/interface/impl/bot/ClientBackend.hpp | 5 +++++ src/socket/interface/impl/bot/ClientBackendPosix.cpp | 12 +++++++++--- .../interface/impl/bot/ClientBackendWindows.cpp | 5 +++-- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/database/utils/SendMediaToChat.cc b/src/database/utils/SendMediaToChat.cc index de9dc79e..d65bae3a 100644 --- a/src/database/utils/SendMediaToChat.cc +++ b/src/database/utils/SendMediaToChat.cc @@ -50,5 +50,9 @@ int main(int argc, char* const* argv) { struct TgBotSocket::Packet pkt( TgBotSocket::Command::CMD_SEND_FILE_TO_CHAT_ID, data); - SocketClientWrapper()->writeAsClientToSocket(pkt.toSocketData()); + SocketClientWrapper wrapper; + + wrapper.setLocalSocketPath( + SocketInterfaceBase::LocalHelper::getSocketPath()); + wrapper->writeAsClientToSocket(pkt.toSocketData()); } diff --git a/src/logging/LoggingClient.cpp b/src/logging/LoggingClient.cpp index e0959370..426f7e9a 100644 --- a/src/logging/LoggingClient.cpp +++ b/src/logging/LoggingClient.cpp @@ -10,6 +10,7 @@ #include "AbslLogInit.hpp" #include "LogcatData.hpp" +#include "SocketBase.hpp" int main() { TgBot_AbslLogInit(); @@ -17,7 +18,8 @@ int main() { SocketClientWrapper wrapper; LogEntry entry{}; - wrapper->options.address = getSocketPathForLogging().string(); + wrapper->options.port = SocketInterfaceBase::kTgBotLogPort; + wrapper.setLocalSocketPath(getSocketPathForLogging()); auto clientSocket = wrapper->createClientSocket(); if (!clientSocket) { LOG(ERROR) << "Failed to create client socket"; diff --git a/src/socket/TgBotSocketClient.cpp b/src/socket/TgBotSocketClient.cpp index 857747c8..29ddd4ab 100644 --- a/src/socket/TgBotSocketClient.cpp +++ b/src/socket/TgBotSocketClient.cpp @@ -15,6 +15,7 @@ #include #include +#include "SocketBase.hpp" #include "TgBotCommandMap.hpp" #include "Types.h" @@ -279,7 +280,9 @@ int main(int argc, char** argv) { pkt->header.checksum = crc.checksum(); } - auto backend = SocketClientWrapper(); + SocketClientWrapper backend; + backend.setLocalSocketPath( + SocketInterfaceBase::LocalHelper::getSocketPath()); auto handle = backend->createClientSocket(); if (handle) { diff --git a/src/socket/interface/impl/bot/ClientBackend.hpp b/src/socket/interface/impl/bot/ClientBackend.hpp index f5a53e32..d74f97ac 100644 --- a/src/socket/interface/impl/bot/ClientBackend.hpp +++ b/src/socket/interface/impl/bot/ClientBackend.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include struct SocketClientWrapper { @@ -9,6 +10,9 @@ struct SocketClientWrapper { return backend.get(); } SocketInterfaceBase *operator->() const { return getRawInterface(); } + void setLocalSocketPath(const std::filesystem::path& path) { + localSocketPath = path; + } private: constexpr static std::string_view kIPv4EnvVar = "IPV4_ADDRESS"; @@ -16,4 +20,5 @@ struct SocketClientWrapper { constexpr static std::string_view kPortEnvVar = "PORT_NUM"; std::shared_ptr backend; + std::optional localSocketPath; }; diff --git a/src/socket/interface/impl/bot/ClientBackendPosix.cpp b/src/socket/interface/impl/bot/ClientBackendPosix.cpp index fd0b191b..f61d18ea 100644 --- a/src/socket/interface/impl/bot/ClientBackendPosix.cpp +++ b/src/socket/interface/impl/bot/ClientBackendPosix.cpp @@ -22,8 +22,12 @@ SocketClientWrapper::SocketClientWrapper() { LOG(INFO) << "Chose IPv6 with address " << addressString; } else { backend = std::make_shared(); - addressString = SocketInterfaceBase::LocalHelper::getSocketPath(); - LOG(INFO) << "Chose Unix Local socket"; + if (localSocketPath) { + addressString = localSocketPath->string(); + LOG(INFO) << "Chose Unix Local socket with path " << addressString; + } else { + LOG(INFO) << "Chose Unix Local socket"; + } } if (needPortCfg) { std::string portStr; @@ -38,5 +42,7 @@ SocketClientWrapper::SocketClientWrapper() { LOG(INFO) << "Using port " << port; backend->options.port = port; } - backend->options.address = addressString; + if (!addressString.empty()) { + backend->options.address = addressString; + } } diff --git a/src/socket/interface/impl/bot/ClientBackendWindows.cpp b/src/socket/interface/impl/bot/ClientBackendWindows.cpp index dbe5996e..5a392a60 100644 --- a/src/socket/interface/impl/bot/ClientBackendWindows.cpp +++ b/src/socket/interface/impl/bot/ClientBackendWindows.cpp @@ -21,7 +21,6 @@ SocketClientWrapper::SocketClientWrapper() { LOG(INFO) << "Chose IPv6 with address " << addressString; } else { backend = std::make_shared(); - addressString = SocketInterfaceBase::LocalHelper::getSocketPath().string(); LOG(INFO) << "Chose Unix Local socket"; } if (needPortCfg) { @@ -37,5 +36,7 @@ SocketClientWrapper::SocketClientWrapper() { LOG(INFO) << "Using port " << port; backend->options.port = port; } - backend->options.address = addressString; + if (!addressString.empty()) { + backend->options.address = addressString; + } }