From 3301a80d995079a1a4870a265fcb11725317f673 Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Tue, 4 Aug 2020 15:38:38 +0200 Subject: [PATCH] send string --- slsReceiverSoftware/src/ClientInterface.cpp | 53 +++++++-------------- slsSupportLib/include/DataSocket.h | 18 +++++-- slsSupportLib/src/DataSocket.cpp | 10 ++++ 3 files changed, 41 insertions(+), 40 deletions(-) diff --git a/slsReceiverSoftware/src/ClientInterface.cpp b/slsReceiverSoftware/src/ClientInterface.cpp index 185c63b40..f709385ea 100644 --- a/slsReceiverSoftware/src/ClientInterface.cpp +++ b/slsReceiverSoftware/src/ClientInterface.cpp @@ -797,58 +797,41 @@ int ClientInterface::stop_receiver(Interface &socket) { } int ClientInterface::set_file_dir(Interface &socket) { - char fPath[MAX_STR_LENGTH]{}; - char retval[MAX_STR_LENGTH]{}; - socket.Receive(fPath); + std::string fpath = socket.Receive(MAX_STR_LENGTH); - if (strlen(fPath) == 0) { + if (fpath.empty()) { throw RuntimeError("Cannot set empty file path"); } - if (fPath[0] != '/') + if (fpath[0] != '/') throw RuntimeError("Receiver path needs to be absolute path"); - LOG(logDEBUG1) << "Setting file path: " << fPath; - impl()->setFilePath(fPath); - - std::string s = impl()->getFilePath(); - sls::strcpy_safe(retval, s.c_str()); - if ((s.empty()) || (strlen(fPath) && strcasecmp(fPath, retval))) - throw RuntimeError("Receiver file path does not exist"); - else - LOG(logDEBUG1) << "file path:" << retval; - + + LOG(logDEBUG1) << "Setting file path: " << fpath; + impl()->setFilePath(fpath); return socket.Send(OK); } int ClientInterface::get_file_dir(Interface &socket) { - char retval[MAX_STR_LENGTH]{}; - std::string s = impl()->getFilePath(); - sls::strcpy_safe(retval, s.c_str()); - LOG(logDEBUG1) << "file path:" << retval; - return socket.sendResult(retval); + auto fpath = impl()->getFilePath(); + LOG(logDEBUG1) << "file path:" << fpath; + fpath.resize(MAX_STR_LENGTH); + return socket.sendResult(fpath); } int ClientInterface::set_file_name(Interface &socket) { - char fName[MAX_STR_LENGTH]{}; - char retval[MAX_STR_LENGTH]{}; - socket.Receive(fName); - if (strlen(fName) == 0) { + std::string fname = socket.Receive(MAX_STR_LENGTH); + if (fname.empty()) { throw RuntimeError("Cannot set empty file name"); } - LOG(logDEBUG1) << "Setting file name: " << fName; - impl()->setFileName(fName); - - std::string s = impl()->getFileName(); - sls::strcpy_safe(retval, s.c_str()); - LOG(logDEBUG1) << "file name:" << retval; + LOG(logDEBUG1) << "Setting file name: " << fname; + impl()->setFileName(fname); return socket.Send(OK); } int ClientInterface::get_file_name(Interface &socket) { - char retval[MAX_STR_LENGTH]{}; - std::string s = impl()->getFileName(); - sls::strcpy_safe(retval, s.c_str()); - LOG(logDEBUG1) << "file name:" << retval; - return socket.sendResult(retval); + auto fname = impl()->getFileName(); + LOG(logDEBUG1) << "file name:" << fname; + fname.resize(MAX_STR_LENGTH); + return socket.sendResult(fname); } int ClientInterface::set_file_index(Interface &socket) { diff --git a/slsSupportLib/include/DataSocket.h b/slsSupportLib/include/DataSocket.h index eb1726b8a..2d7acbb47 100644 --- a/slsSupportLib/include/DataSocket.h +++ b/slsSupportLib/include/DataSocket.h @@ -3,6 +3,7 @@ #include "TypeTraits.h" #include #include +#include #include #include #include @@ -27,12 +28,16 @@ class DataSocket { int Send(const void *buffer, size_t size); - // Send everything that is not a vector by using address and sizeof + // Send everything that is not a vector or string by using address and + // sizeof // TODO! We probably should restrict this even more to avoid bugs when // we send object instead of data template typename std::enable_if< - !is_vector::type>::value, int>::type + !is_vector::type>::value && + !std::is_same::type, + std::string>::value, + int>::type Send(T &&data) { return Send(&data, sizeof(data)); } @@ -41,6 +46,8 @@ class DataSocket { return Send(vec.data(), sizeof(T) * vec.size()); } + int Send(const std::string &s); + // Variadic template to send all arguments template int SendAll(Args &&... args) { auto l = std::initializer_list{Send(args)...}; @@ -48,13 +55,12 @@ class DataSocket { return sum; } int Receive(void *buffer, size_t size); - + template int Receive(T &arg) { return Receive(&arg, sizeof(arg)); } - template - int Receive(std::vector& buff){ + template int Receive(std::vector &buff) { return Receive(buff.data(), sizeof(T) * buff.size()); } @@ -64,6 +70,8 @@ class DataSocket { return arg; } + std::string Receive(size_t length); + int read(void *buffer, size_t size); int write(void *buffer, size_t size); int setTimeOut(int t_seconds); diff --git a/slsSupportLib/src/DataSocket.cpp b/slsSupportLib/src/DataSocket.cpp index 49750cba9..28aad7c94 100644 --- a/slsSupportLib/src/DataSocket.cpp +++ b/slsSupportLib/src/DataSocket.cpp @@ -63,6 +63,14 @@ int DataSocket::Receive(void *buffer, size_t size) { } } +std::string DataSocket::Receive(size_t length) { + std::string buff(length, '\0'); + Receive(&buff[0], buff.size()); + auto pos = buff.find('\0'); + if (pos != std::string::npos) + buff.erase(pos); + return buff; +} int DataSocket::Send(const void *buffer, size_t size) { int bytes_sent = 0; int data_size = static_cast(size); // signed size @@ -81,6 +89,8 @@ int DataSocket::Send(const void *buffer, size_t size) { return bytes_sent; } +int DataSocket::Send(const std::string &s) { return Send(&s[0], s.size()); } + int DataSocket::write(void *buffer, size_t size) { return ::write(getSocketId(), buffer, size); }