1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
// Copyright (c) 2015-2017 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "SoapySocketDefs.hpp"
#include "SoapyRemoteDefs.hpp"
#include "SoapyRPCSocket.hpp"
#include "SoapyRPCPacker.hpp"
#include <SoapySDR/Version.hpp> //feature defines
#include <cfloat> //DBL_MANT_DIG
#include <cmath> //frexp
#include <cstring> //memcpy
#include <cstdlib> //malloc
#include <algorithm> //min, max
#include <stdexcept>
SoapyRPCPacker::SoapyRPCPacker(SoapyRPCSocket &sock, unsigned int remoteRPCVersion):
_sock(sock),
_message(NULL),
_size(0),
_capacity(0),
_remoteRPCVersion(remoteRPCVersion)
{
//default allocation
this->ensureSpace(512);
//allot space for the header (filled in by send)
SoapyRPCHeader header;
this->pack(&header, sizeof(header));
}
SoapyRPCPacker::~SoapyRPCPacker(void)
{
free(_message);
_message = NULL;
}
void SoapyRPCPacker::send(void)
{
//load the trailer
SoapyRPCTrailer trailer;
trailer.trailerWord = htonl(SoapyRPCTrailerWord);
this->pack(&trailer, sizeof(trailer));
//load the header
SoapyRPCHeader *header = (SoapyRPCHeader *)_message;
header->headerWord = htonl(SoapyRPCHeaderWord);
header->version = htonl(SoapyRPCVersion);
header->length = htonl(_size);
//send the entire message
size_t bytesSent = 0;
while (bytesSent != _size)
{
const size_t toSend = std::min<size_t>(SOAPY_REMOTE_SOCKET_BUFFMAX, _size-bytesSent);
int ret = _sock.send(_message+bytesSent, toSend);
if (ret < 0)
{
throw std::runtime_error("SoapyRPCPacker::send() FAIL: "+std::string(_sock.lastErrorMsg()));
}
bytesSent += ret;
}
}
void SoapyRPCPacker::ensureSpace(const size_t length)
{
if (_size+length <= _capacity) return;
const size_t newSize = std::max(_capacity*2, _size+length);
_message = (char *)realloc(_message, newSize);
}
void SoapyRPCPacker::pack(const void *buff, const size_t length)
{
this->ensureSpace(length);
std::memcpy(_message+_size, buff, length);
_size += length;
}
void SoapyRPCPacker::operator&(const char value)
{
*this & SOAPY_REMOTE_CHAR;
this->pack(value);
}
void SoapyRPCPacker::operator&(const bool value)
{
*this & SOAPY_REMOTE_BOOL;
char out = value?1:0;
this->pack(out);
}
void SoapyRPCPacker::operator&(const int value)
{
*this & SOAPY_REMOTE_INT32;
int out = htonl(value);
this->pack(&out, sizeof(out));
}
void SoapyRPCPacker::operator&(const long long value)
{
*this & SOAPY_REMOTE_INT64;
long long out = htonll(value);
this->pack(&out, sizeof(out));
}
void SoapyRPCPacker::operator&(const double value)
{
*this & SOAPY_REMOTE_FLOAT64;
int exp = 0;
const double x = std::frexp(value, &exp);
const long long man = (long long)std::ldexp(x, DBL_MANT_DIG);
*this & exp;
*this & man;
}
void SoapyRPCPacker::operator&(const std::complex<double> &value)
{
*this & SOAPY_REMOTE_COMPLEX128;
*this & value.real();
*this & value.imag();
}
void SoapyRPCPacker::operator&(const std::string &value)
{
*this & SOAPY_REMOTE_STRING;
*this & int(value.size());
this->pack(value.c_str(), value.size());
}
void SoapyRPCPacker::operator&(const SoapySDR::Range &value)
{
*this & SOAPY_REMOTE_RANGE;
*this & value.minimum();
*this & value.maximum();
//a step size is sent when the remote version matches our current
if (_remoteRPCVersion >= SoapyRPCVersion)
{
#ifdef SOAPY_SDR_API_HAS_RANGE_TYPE_STEP
*this & value.step();
#else
*this & double(0.0);
#endif
}
}
void SoapyRPCPacker::operator&(const SoapySDR::RangeList &value)
{
*this & SOAPY_REMOTE_RANGE_LIST;
*this & int(value.size());
for (size_t i = 0; i < value.size(); i++) *this & value[i];
}
void SoapyRPCPacker::operator&(const std::vector<std::string> &value)
{
*this & SOAPY_REMOTE_STRING_LIST;
*this & int(value.size());
for (size_t i = 0; i < value.size(); i++) *this & value[i];
}
void SoapyRPCPacker::operator&(const std::vector<double> &value)
{
*this & SOAPY_REMOTE_FLOAT64_LIST;
*this & int(value.size());
for (size_t i = 0; i < value.size(); i++) *this & value[i];
}
void SoapyRPCPacker::operator&(const SoapySDR::Kwargs &value)
{
*this & SOAPY_REMOTE_KWARGS;
*this & int(value.size());
for (auto it = value.begin(); it != value.end(); ++it)
{
*this & it->first;
*this & it->second;
}
}
void SoapyRPCPacker::operator&(const SoapySDR::KwargsList &value)
{
*this & SOAPY_REMOTE_KWARGS_LIST;
*this & int(value.size());
for (size_t i = 0; i < value.size(); i++) *this & value[i];
}
void SoapyRPCPacker::operator&(const std::vector<size_t> &value)
{
*this & SOAPY_REMOTE_SIZE_LIST;
*this & int(value.size());
for (size_t i = 0; i < value.size(); i++) *this & int(value[i]);
}
void SoapyRPCPacker::operator&(const SoapySDR::ArgInfo &value)
{
*this & SOAPY_REMOTE_ARG_INFO;
*this & value.key;
*this & value.value;
*this & value.name;
*this & value.description;
*this & value.units;
*this & int(value.type);
*this & value.range;
*this & value.options;
*this & value.optionNames;
}
void SoapyRPCPacker::operator&(const SoapySDR::ArgInfoList &value)
{
*this & SOAPY_REMOTE_ARG_INFO_LIST;
*this & int(value.size());
for (size_t i = 0; i < value.size(); i++) *this & value[i];
}
void SoapyRPCPacker::operator&(const std::exception &value)
{
*this & SOAPY_REMOTE_EXCEPTION;
std::string msg(value.what());
*this & msg;
}
|