fix: Rewrite createSAMRequest function with template.

This commit is contained in:
polistern
2022-01-19 04:37:20 +00:00
parent fdec8b4b13
commit 9e65732383
2 changed files with 46 additions and 48 deletions

View File

@ -9,8 +9,6 @@
*/
#include <iostream>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
@ -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<char> 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) {
/**

View File

@ -56,6 +56,8 @@
//#ifdef __cplusplus
//#include <cstdarg>
#include <stdio.h>
#include <cstdint>
#include <list>
#include <memory>
@ -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<typename... t_args>
static std::string
createSAMRequest(const char *msg) { return {msg}; }
template<typename... t_args>
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<char> 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 {