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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
|
// Copyright (c) 2015-2018 Josh Blum
// SPDX-License-Identifier: BSL-1.0
#include "SoapySocketDefs.hpp"
#include "SoapyRemoteDefs.hpp"
#include "SoapyRPCSocket.hpp"
#include "SoapyRPCUnpacker.hpp"
#include "SoapyRPCPacker.hpp"
#include <SoapySDR/Logger.hpp>
#include <SoapySDR/Version.hpp> //feature defines
#include <cfloat> //DBL_MANT_DIG
#include <cmath> //ldexp
#include <cstring> //memcpy
#include <cstdlib> //malloc
#include <algorithm> //min, max
#include <stdexcept>
#include <chrono>
//! How long to wait for the server presence checks
static const long SERVER_CHECK_TIMEOUT_US = 3000000; //3 seconds
static void testServerConnection(const std::string &url)
{
SoapyRPCSocket s;
int ret = s.connect(url, SERVER_CHECK_TIMEOUT_US);
if (ret != 0) throw std::runtime_error("SoapyRPCUnpacker::recv() FAIL test server connection: "+std::string(s.lastErrorMsg()));
SoapyRPCPacker packerHangup(s);
packerHangup & SOAPY_REMOTE_HANGUP;
packerHangup();
s.selectRecv(SERVER_CHECK_TIMEOUT_US);
}
SoapyRPCUnpacker::SoapyRPCUnpacker(SoapyRPCSocket &sock, const bool autoRecv, const long timeoutUs):
_sock(sock),
_message(NULL),
_offset(0),
_capacity(0),
_remoteRPCVersion(SoapyRPCVersion)
{
//auto recv expects a reply packet within a reasonable time window
//or else the link might be down, in which case we throw an error.
//Calls are allowed to take a long time (up to 31 seconds).
//However, we continually check that the server is active
//so that we can tear down immediately if the server goes away.
if (timeoutUs >= SERVER_CHECK_TIMEOUT_US)
{
const auto exitTime = std::chrono::high_resolution_clock::now() + std::chrono::microseconds(timeoutUs);
while (not _sock.selectRecv(SERVER_CHECK_TIMEOUT_US))
{
testServerConnection(_sock.getpeername());
if (std::chrono::high_resolution_clock::now() > exitTime)
throw std::runtime_error("SoapyRPCUnpacker::recv() TIMEOUT");
}
}
//small timeout but not -1 for infinite timeout
else if (timeoutUs >= 0 and not _sock.selectRecv(timeoutUs))
{
throw std::runtime_error("SoapyRPCUnpacker::recv() TIMEOUT");
}
if (autoRecv) this->recv();
}
SoapyRPCUnpacker::~SoapyRPCUnpacker(void)
{
free(_message);
_message = NULL;
_offset += sizeof(SoapyRPCTrailer); //consume trailer
if (_offset != _capacity)
{
SoapySDR::logf(SOAPY_SDR_ERROR, "~SoapyRPCUnpacker: Unconsumed payload bytes %d", int(_capacity-_offset));
}
}
void SoapyRPCUnpacker::recv(void)
{
//receive the header
SoapyRPCHeader header;
int ret = _sock.recv(&header, sizeof(header), MSG_WAITALL);
if (ret != sizeof(header))
{
throw std::runtime_error("SoapyRPCUnpacker::recv(header) FAIL: "+std::string(_sock.lastErrorMsg()));
}
//inspect and parse the header
if (ntohl(header.headerWord) != SoapyRPCHeaderWord)
{
throw std::runtime_error("SoapyRPCUnpacker::recv() FAIL: header word");
}
_remoteRPCVersion = ntohl(header.version);
//TODO ignoring the version for now
//the check may need to be delicate with the version major, minor vs patch number
const size_t length = ntohl(header.length);
if (length <= sizeof(SoapyRPCHeader) + sizeof(SoapyRPCTrailer))
{
throw std::runtime_error("SoapyRPCUnpacker::recv() FAIL: header length");
}
//receive the remaining payload
_capacity = length - sizeof(SoapyRPCHeader);
_message = (char *)malloc(_capacity);
size_t bytesReceived = 0;
while (bytesReceived != _capacity)
{
const size_t toRecv = std::min<size_t>(SOAPY_REMOTE_SOCKET_BUFFMAX, _capacity-bytesReceived);
ret = _sock.recv(_message+bytesReceived, toRecv);
if (ret < 0)
{
throw std::runtime_error("SoapyRPCUnpacker::recv(payload) FAIL: "+std::string(_sock.lastErrorMsg()));
}
bytesReceived += ret;
}
//check the trailer
SoapyRPCTrailer trailer;
std::memcpy(&trailer, _message + _capacity - sizeof(SoapyRPCTrailer), sizeof(trailer));
if (ntohl(trailer.trailerWord) != SoapyRPCTrailerWord)
{
throw std::runtime_error("SoapyRPCUnpacker::recv() FAIL: trailer word");
}
//auto-consume void
if (this->peekType() == SOAPY_REMOTE_VOID)
{
SoapyRemoteTypes type;
*this & type;
}
//check for exceptions
else if (this->peekType() == SOAPY_REMOTE_EXCEPTION)
{
SoapyRemoteTypes type;
std::string errorMsg;
*this & type;
*this & errorMsg;
throw std::runtime_error("RemoteError: "+errorMsg);
}
}
void SoapyRPCUnpacker::unpack(void *buff, const size_t length)
{
std::memcpy(buff, this->unpack(length), length);
}
void *SoapyRPCUnpacker::unpack(const size_t length)
{
if (_offset + length > _capacity - sizeof(SoapyRPCTrailer))
{
throw std::runtime_error("SoapyRPCUnpacker::unpack() OVER-CONSUME");
}
void *buff = _message+_offset;
_offset += length;
return buff;
}
bool SoapyRPCUnpacker::done(void) const
{
return (_offset + sizeof(SoapyRPCTrailer)) == _capacity;
}
#define UNPACK_TYPE_HELPER(expected) \
SoapyRemoteTypes type; *this & type; \
if (type != expected) {throw std::runtime_error("SoapyRPCUnpacker type check FAIL:" #expected);} else {}
void SoapyRPCUnpacker::operator&(SoapyRemoteCalls &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_CALL);
int call = 0;
*this & call;
value = SoapyRemoteCalls(call);
}
void SoapyRPCUnpacker::operator&(char &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_CHAR);
value = this->unpack();
}
void SoapyRPCUnpacker::operator&(bool &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_BOOL);
char in = this->unpack();
value = (in == 0)?false:true;
}
void SoapyRPCUnpacker::operator&(int &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_INT32);
this->unpack(&value, sizeof(value));
value = ntohl(value);
}
void SoapyRPCUnpacker::operator&(long long &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_INT64);
this->unpack(&value, sizeof(value));
value = ntohll(value);
}
void SoapyRPCUnpacker::operator&(double &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_FLOAT64);
int exp = 0;
long long man = 0;
*this & exp;
*this & man;
value = std::ldexp(double(man), exp-DBL_MANT_DIG);
}
void SoapyRPCUnpacker::operator&(std::complex<double> &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_COMPLEX128);
double r = 0.0, i = 0.0;
*this & r;
*this & i;
value = std::complex<double>(r, i);
}
void SoapyRPCUnpacker::operator&(std::string &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_STRING);
int size = 0;
*this & size;
value = std::string((const char *)this->unpack(size), size);
}
void SoapyRPCUnpacker::operator&(SoapySDR::Range &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_RANGE);
double minimum = 0.0, maximum = 0.0, step = 0.0;
*this & minimum;
*this & maximum;
//a step size is sent when the remote version matches our current
if (_remoteRPCVersion >= SoapyRPCVersion)
{
*this & step;
}
#ifdef SOAPY_SDR_API_HAS_RANGE_TYPE_STEP
value = SoapySDR::Range(minimum, maximum, step);
#else
value = SoapySDR::Range(minimum, maximum);
#endif
}
void SoapyRPCUnpacker::operator&(SoapySDR::RangeList &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_RANGE_LIST);
int size = 0;
*this & size;
value.resize(size);
for (size_t i = 0; i < size_t(size); i++) *this & value[i];
}
void SoapyRPCUnpacker::operator&(std::vector<std::string> &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_STRING_LIST);
int size = 0;
*this & size;
value.resize(size);
for (size_t i = 0; i < size_t(size); i++) *this & value[i];
}
void SoapyRPCUnpacker::operator&(std::vector<double> &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_FLOAT64_LIST);
int size = 0;
*this & size;
value.resize(size);
for (size_t i = 0; i < size_t(size); i++) *this & value[i];
}
void SoapyRPCUnpacker::operator&(SoapySDR::Kwargs &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_KWARGS);
int size = 0;
*this & size;
value.clear();
for (size_t i = 0; i < size_t(size); i++)
{
std::string key, val;
*this & key;
*this & val;
value[key] = val;
}
}
void SoapyRPCUnpacker::operator&(SoapySDR::KwargsList &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_KWARGS_LIST);
int size = 0;
*this & size;
value.resize(size);
for (size_t i = 0; i < size_t(size); i++) *this & value[i];
}
void SoapyRPCUnpacker::operator&(std::vector<size_t> &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_SIZE_LIST);
int size = 0;
*this & size;
value.resize(size);
for (size_t i = 0; i < value.size(); i++)
{
*this & size;
value[i] = size;
}
}
void SoapyRPCUnpacker::operator&(SoapySDR::ArgInfo &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_ARG_INFO);
*this & value.key;
*this & value.value;
*this & value.name;
*this & value.description;
*this & value.units;
int intType = 0; *this & intType;
value.type = SoapySDR::ArgInfo::Type(intType);
*this & value.range;
*this & value.options;
*this & value.optionNames;
}
void SoapyRPCUnpacker::operator&(SoapySDR::ArgInfoList &value)
{
UNPACK_TYPE_HELPER(SOAPY_REMOTE_ARG_INFO_LIST);
int size = 0;
*this & size;
value.resize(size);
for (size_t i = 0; i < size_t(size); i++) *this & value[i];
}
|