send string

This commit is contained in:
Erik Frojdh
2020-08-04 15:38:38 +02:00
parent 4174d193b4
commit 3301a80d99
3 changed files with 41 additions and 40 deletions

View File

@ -3,6 +3,7 @@
#include "TypeTraits.h"
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <netdb.h>
#include <numeric>
#include <string>
@ -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 T>
typename std::enable_if<
!is_vector<typename std::remove_reference<T>::type>::value, int>::type
!is_vector<typename std::remove_reference<T>::type>::value &&
!std::is_same<typename std::remove_reference<T>::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 <class... Args> int SendAll(Args &&... args) {
auto l = std::initializer_list<int>{Send(args)...};
@ -48,13 +55,12 @@ class DataSocket {
return sum;
}
int Receive(void *buffer, size_t size);
template <typename T> int Receive(T &arg) {
return Receive(&arg, sizeof(arg));
}
template<typename T>
int Receive(std::vector<T>& buff){
template <typename T> int Receive(std::vector<T> &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);

View File

@ -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<int>(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);
}