1 // Copyright (c) 2015-2017 Josh Blum
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #pragma once
5 #include "SoapyRemoteConfig.hpp"
6 #include <SoapySDR/Types.hpp>
7 #include <vector>
8 #include <complex>
9 #include <string>
10 #include <stdexcept>
11 
12 class SoapyRPCSocket;
13 
14 /*!
15  * The packer object accepts primitive Soapy SDR types
16  * and encodes them into a portable network RPC format.
17  */
18 class SOAPY_REMOTE_API SoapyRPCPacker
19 {
20 public:
21     SoapyRPCPacker(SoapyRPCSocket &sock, unsigned int remoteRPCVersion = SoapyRPCVersion);
22 
23     ~SoapyRPCPacker(void);
24 
25     //! Shortcut operator for send
operator ()(void)26     void operator()(void)
27     {
28         this->send();
29     }
30 
31     //! Send the message when packing is complete
32     void send(void);
33 
34     //! Pack a binary blob
35     void pack(const void *buff, const size_t length);
36 
37     //! Pack a single byte
pack(const char byte)38     void pack(const char byte)
39     {
40         this->ensureSpace(1);
41         _message[_size] = byte;
42         _size++;
43     }
44 
45     //! Pack the call
operator &(const SoapyRemoteCalls value)46     void operator&(const SoapyRemoteCalls value)
47     {
48         *this & SOAPY_REMOTE_CALL;
49         *this & int(value);
50     }
51 
52     //! Pack the type
operator &(const SoapyRemoteTypes value)53     void operator&(const SoapyRemoteTypes value)
54     {
55         this->pack(char(value));
56     }
57 
58     //! Pack a character
59     void operator&(const char value);
60 
61     //! Pack a boolean
62     void operator&(const bool value);
63 
64     //! Pack a 32-bit integer
65     void operator&(const int value);
66 
67     //! Pack a 64-bit integer
68     void operator&(const long long value);
69 
70     //! Pack a double float
71     void operator&(const double value);
72 
73     //! Pack a complex double float
74     void operator&(const std::complex<double> &value);
75 
76     //! Pack a string
77     void operator&(const std::string &value);
78 
79     //! Pack a range
80     void operator&(const SoapySDR::Range &value);
81 
82     //! Pack a list of ranges
83     void operator&(const SoapySDR::RangeList &value);
84 
85     //! Pack a list of strings
86     void operator&(const std::vector<std::string> &value);
87 
88     //! Pack a list of double floats
89     void operator&(const std::vector<double> &value);
90 
91     //! Pack a kwargs dictionary
92     void operator&(const SoapySDR::Kwargs &value);
93 
94     //! Pack a list of kwargs
95     void operator&(const SoapySDR::KwargsList &value);
96 
97     //! Pack a list of sizes
98     void operator&(const std::vector<size_t> &value);
99 
100     //! Pack an arg info structure
101     void operator&(const SoapySDR::ArgInfo &value);
102 
103     //! Pack a list of arg infos
104     void operator&(const SoapySDR::ArgInfoList &value);
105 
106     //! Pack an exception
107     void operator&(const std::exception &value);
108 
109 private:
110 
111     void ensureSpace(const size_t length);
112 
113     SoapyRPCSocket &_sock;
114     char *_message;
115     size_t _size;
116     size_t _capacity;
117     unsigned int _remoteRPCVersion;
118 };
119