From 9e657323838d48127dfb949b0d24178df6ffdf0f Mon Sep 17 00:00:00 2001 From: polistern Date: Wed, 19 Jan 2022 04:37:20 +0000 Subject: [PATCH] fix: Rewrite createSAMRequest function with template. --- i2psam.cpp | 48 +----------------------------------------------- i2psam.h | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/i2psam.cpp b/i2psam.cpp index d2e1b17..588a6bc 100644 --- a/i2psam.cpp +++ b/i2psam.cpp @@ -9,8 +9,6 @@ */ #include -#include -#include #include #include #include @@ -29,16 +27,6 @@ namespace SAM { -static void print_error(const std::string &err) { -#ifdef DEBUG_ON_STDOUT -#ifdef WIN32 - std::cout << err << "(" << WSAGetLastError() << ")" << std::endl; -#else - std::cout << err << "(" << errno << ")" << std::endl; -#endif -#endif // DEBUG_ON_STDOUT -} - #ifdef WIN32 int I2pSocket::instances_ = 0; @@ -1047,41 +1035,7 @@ const std::string &RawSession::getSAMMinVer() const { return socket_.minVer_; } const std::string &RawSession::getSAMMaxVer() const { return socket_.maxVer_; } -//-------------------------------------------------------------------------------------------------- - -std::string Message::createSAMRequest(const char *format, ...) { - // ToDo: GR note: Creating a 65K byte buffer on the stack, and then wasting the time to zero it out - // before using it. Just to send a 30 byte string?, seems really wasteful to me, time more than storage, many mSec... - - std::va_list args; - va_start(args, format); - - std::va_list argsCopy; - va_copy(argsCopy, args); - const char * const formatCopy = format; - const int bufferStatus = std::vsnprintf(nullptr, 0, formatCopy, argsCopy); - va_end(argsCopy); - - if (bufferStatus < 0) { - print_error("Failed to allocate buffer"); - return {}; - } - - std::vector buffer(bufferStatus + 1); - const int status = std::vsnprintf(buffer.data(), buffer.size(), formatCopy, args); - va_end(args); - - if (status < 0) { - print_error("Failed to format message"); - return {}; - } - -#ifdef DEBUG_ON_STDOUT - std::cout << "Status: " << status << std::endl; -#endif // DEBUG_ON_STDOUT - - return {buffer.data()}; -} +//----------------------------------------------------------------------------- std::string Message::hello(const std::string &minVer, const std::string &maxVer) { /** diff --git a/i2psam.h b/i2psam.h index 138fa7f..8d3b307 100644 --- a/i2psam.h +++ b/i2psam.h @@ -56,6 +56,8 @@ //#ifdef __cplusplus +//#include +#include #include #include #include @@ -70,6 +72,16 @@ namespace SAM { typedef u_int SOCKET; +static void print_error(const std::string &err) { +#ifdef DEBUG_ON_STDOUT +#ifdef WIN32 + std::cout << err << "(" << WSAGetLastError() << ")" << std::endl; +#else + std::cout << err << "(" << errno << ")" << std::endl; +#endif +#endif // DEBUG_ON_STDOUT +} + class Message { public: enum SessionStyle { sssStream, sssDatagram, sssRaw }; @@ -240,7 +252,39 @@ class Message { const std::string &key); private: - static std::string createSAMRequest(const char *format, ...); + template + static std::string + createSAMRequest(const char *msg) { return {msg}; } + + template + static std::string + createSAMRequest(const char *format, t_args &&... args) + { + // ToDo: Check allocated buffer size + + const int bufferStatus = std::snprintf(nullptr, 0, format, args...); + if (bufferStatus < 0) + { + print_error("Failed to allocate buffer"); + return {}; + } + + std::vector buffer(bufferStatus + 1); + const int status = + std::snprintf(buffer.data(), buffer.size(), format, args...); + + if (status < 0) + { + print_error("Failed to format message"); + return {}; + } + +#ifdef DEBUG_ON_STDOUT + std::cout << "Status: " << status << std::endl; +#endif // DEBUG_ON_STDOUT + + return {buffer.data()}; + } }; class I2pSocket {