Skip to content

Commit

Permalink
TgBot++: socket: ClientBackend: Add ability to store the path
Browse files Browse the repository at this point in the history
- Don't blindly hardcode it to main path every time
  • Loading branch information
Royna2544 committed Jun 26, 2024
1 parent ebe00b5 commit 6f810af
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/database/utils/SendMediaToChat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
4 changes: 3 additions & 1 deletion src/logging/LoggingClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@

#include "AbslLogInit.hpp"
#include "LogcatData.hpp"
#include "SocketBase.hpp"

int main() {
TgBot_AbslLogInit();

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";
Expand Down
5 changes: 4 additions & 1 deletion src/socket/TgBotSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <optional>
#include <string>

#include "SocketBase.hpp"
#include "TgBotCommandMap.hpp"
#include "Types.h"

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/socket/interface/impl/bot/ClientBackend.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <SocketBase.hpp>
#include <filesystem>
#include <memory>

struct SocketClientWrapper {
Expand All @@ -9,11 +10,15 @@ 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";
constexpr static std::string_view kIPv6EnvVar = "IPV6_ADDRESS";
constexpr static std::string_view kPortEnvVar = "PORT_NUM";

std::shared_ptr<SocketInterfaceBase> backend;
std::optional<std::filesystem::path> localSocketPath;
};
12 changes: 9 additions & 3 deletions src/socket/interface/impl/bot/ClientBackendPosix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ SocketClientWrapper::SocketClientWrapper() {
LOG(INFO) << "Chose IPv6 with address " << addressString;
} else {
backend = std::make_shared<SocketInterfaceUnixLocal>();
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;
Expand All @@ -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;
}
}
5 changes: 3 additions & 2 deletions src/socket/interface/impl/bot/ClientBackendWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ SocketClientWrapper::SocketClientWrapper() {
LOG(INFO) << "Chose IPv6 with address " << addressString;
} else {
backend = std::make_shared<SocketInterfaceWindowsLocal>();
addressString = SocketInterfaceBase::LocalHelper::getSocketPath().string();
LOG(INFO) << "Chose Unix Local socket";
}
if (needPortCfg) {
Expand All @@ -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;
}
}

0 comments on commit 6f810af

Please sign in to comment.