Skip to content

Commit 06dd414

Browse files
committed
Update to latest DelegateMQ library
1 parent 0bcf5f0 commit 06dd414

File tree

7 files changed

+183
-78
lines changed

7 files changed

+183
-78
lines changed

DelegateMQ/predef/serialize/serialize/msg_serialize.h

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,40 @@ class serialize
546546
std::is_class<typename std::remove_pointer<T>::type>::value)),
547547
"T cannot be a pointer to a built-in or custom data type");
548548

549+
// If C++17 or higher
550+
#if defined(_MSVC_LANG) && _MSVC_LANG >= 201703L || __cplusplus >= 201703L
551+
// Check if T is a user-defined class
552+
if constexpr (std::is_class<T>::value)
553+
{
554+
// Ensure that the class T inherits from serialize::I
555+
static_assert(std::is_base_of<serialize::I, T>::value, "T must inherit from serialize::I");
556+
557+
// Proceed with serialization for user-defined type
558+
return write(os, (serialize::I*)&t_);
559+
}
560+
else
561+
{
562+
// If T is not a class, handle built-in data types
563+
if (std::is_pointer<T>::value == false)
564+
{
565+
if (prependType)
566+
{
567+
write_type(os, Type::LITERAL);
568+
}
569+
return write_internal(os, (const char*)&t_, sizeof(t_));
570+
}
571+
else
572+
{
573+
// Can't write pointers to built-in types
574+
raiseError(ParsingError::INVALID_INPUT, __LINE__, __FILE__);
575+
os.setstate(std::ios::failbit);
576+
return os;
577+
}
578+
}
579+
#else
549580
// Is T type a built-in data type (e.g. float, int, ...)?
550581
if (std::is_class<T>::value == false)
551-
{
582+
{
552583
// Is T is not a pointer type
553584
if (std::is_pointer<T>::value == false)
554585
{
@@ -568,12 +599,13 @@ class serialize
568599
}
569600
// Else T type is a user defined data type (e.g. MyData)
570601
else
571-
{
602+
{
572603
return write(os, (serialize::I*)&t_);
573604
}
605+
#endif
574606
}
575607

576-
/// Write a vector container to a stream. The items in vector are stored
608+
/// Write a vector container to a stream. The items in vector are stored
577609
/// by value.
578610
/// @param[in] os - the output stream
579611
/// @param[in] container - the vector container to write

DelegateMQ/predef/transport/ITransport.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ class ITransport
1515
virtual int Send(xostringstream& os, const DmqHeader& header) = 0;
1616

1717
/// Receive data from a remote
18+
/// @param[out] is The received incoming data bytes, not including the header.
1819
/// @param[out] header Incoming delegate message header.
19-
/// @return The received incoming data bytes, not including the header.
20-
virtual xstringstream Receive(DmqHeader& header) = 0;
20+
/// @return 0 if success.
21+
virtual int Receive(xstringstream& is, DmqHeader& header) = 0;
2122
};
2223

2324
#endif

DelegateMQ/predef/transport/mqtt/MqttTransport.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,12 @@ class MqttTransport : public ITransport
185185
}
186186
}
187187

188-
virtual xstringstream Receive(DmqHeader& header) override
188+
virtual int Receive(xstringstream& is, DmqHeader& header) override
189189
{
190190
xstringstream headerStream(std::ios::in | std::ios::out | std::ios::binary);
191191

192192
if (!m_payload)
193-
return headerStream;
193+
return -1;
194194

195195
// Find the position of the opening square bracket
196196
size_t headerStart = 0;
@@ -200,7 +200,7 @@ class MqttTransport : public ITransport
200200

201201
if (headerStart == m_payloadLen) {
202202
std::cerr << "Invalid header format: Missing opening '['" << std::endl;
203-
return headerStream;
203+
return -1;
204204
}
205205

206206
// Find the position of the closing square bracket
@@ -211,7 +211,7 @@ class MqttTransport : public ITransport
211211

212212
if (headerEnd == m_payloadLen) {
213213
std::cerr << "Invalid header format: Missing closing ']'" << std::endl;
214-
return headerStream;
214+
return -1;
215215
}
216216

217217
// Calculate the header size (everything between '[' and ']')
@@ -232,14 +232,12 @@ class MqttTransport : public ITransport
232232
// Check for the correct sync marker
233233
if (header.GetMarker() != DmqHeader::MARKER) {
234234
std::cerr << "Invalid sync marker!" << std::endl;
235-
return headerStream;
235+
return -1;
236236
}
237237

238-
xstringstream argStream(std::ios::in | std::ios::out | std::ios::binary);
239-
240238
// The remaining data after the closing bracket is the JSON part
241239
size_t jsonStart = headerEnd + 1; // After the closing header bracket ']'
242-
argStream.write(m_payload + jsonStart, m_payloadLen - jsonStart);
240+
is.write(m_payload + jsonStart, m_payloadLen - jsonStart);
243241

244242
if (id == dmq::ACK_REMOTE_ID)
245243
{
@@ -260,8 +258,8 @@ class MqttTransport : public ITransport
260258
}
261259
}
262260

263-
// argStream contains the serialized remote argument data
264-
return argStream;
261+
// `is` stream contains the serialized remote argument data
262+
return 0; // success
265263
}
266264

267265
void SetReceiveHandler(IMqttReceiveHandler* handler)

DelegateMQ/predef/transport/win32-pipe/Win32PipeTransport.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,25 +110,25 @@ class Win32PipeTransport : public ITransport
110110
return 0;
111111
}
112112

113-
virtual xstringstream Receive(DmqHeader& header) override
113+
virtual int Receive(xstringstream& is, DmqHeader& header) override
114114
{
115115
xstringstream headerStream(std::ios::in | std::ios::out | std::ios::binary);
116116

117117
// Check if client connected
118118
BOOL connected = ConnectNamedPipe(m_hPipe, NULL) ?
119119
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
120120
if (connected == FALSE)
121-
return headerStream;
121+
return -1;
122122

123123
DWORD size = 0;
124124
BOOL success = ReadFile(m_hPipe, m_buffer, BUFFER_SIZE, &size, NULL);
125125

126126
if (success == FALSE || size <= 0)
127-
return headerStream;
127+
return -1;
128128

129129
if (size <= DmqHeader::HEADER_SIZE) {
130130
std::cerr << "Received data is too small to process." << std::endl;
131-
return headerStream;
131+
return -1;
132132
}
133133

134134
// Write the received data into the stringstream
@@ -141,7 +141,7 @@ class Win32PipeTransport : public ITransport
141141

142142
if (header.GetMarker() != DmqHeader::MARKER) {
143143
std::cerr << "Invalid sync marker!" << std::endl;
144-
return headerStream; // TODO: Optionally handle this case more gracefully
144+
return -1; // TODO: Optionally handle this case more gracefully
145145
}
146146

147147
// Read the DelegateRemoteId (2 bytes) into the `id` variable
@@ -154,13 +154,11 @@ class Win32PipeTransport : public ITransport
154154
headerStream.read(reinterpret_cast<char*>(&seqNum), sizeof(seqNum));
155155
header.SetSeqNum(seqNum);
156156

157-
xstringstream argStream(std::ios::in | std::ios::out | std::ios::binary);
158-
159157
// Write the remaining argument data to stream
160-
argStream.write(m_buffer + DmqHeader::HEADER_SIZE, size - DmqHeader::HEADER_SIZE);
158+
is.write(m_buffer + DmqHeader::HEADER_SIZE, size - DmqHeader::HEADER_SIZE);
161159

162160
// Now `is` contains the rest of the remote argument data
163-
return argStream;
161+
return 0; // Success
164162
}
165163

166164
private:

DelegateMQ/predef/transport/win32-udp/Win32UdpTransport.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Win32UdpTransport : public ITransport
121121
return 0;
122122
}
123123

124-
virtual xstringstream Receive(DmqHeader& header) override
124+
virtual int Receive(xstringstream& is, DmqHeader& header) override
125125
{
126126
xstringstream headerStream(std::ios::in | std::ios::out | std::ios::binary);
127127

@@ -130,7 +130,7 @@ class Win32UdpTransport : public ITransport
130130
if (size == SOCKET_ERROR)
131131
{
132132
std::cerr << "recvfrom failed." << std::endl;
133-
return headerStream;
133+
return -1;
134134
}
135135

136136
// Write the received data into the stringstream
@@ -143,7 +143,7 @@ class Win32UdpTransport : public ITransport
143143

144144
if (header.GetMarker() != DmqHeader::MARKER) {
145145
std::cerr << "Invalid sync marker!" << std::endl;
146-
return headerStream; // TODO: Optionally handle this case more gracefully
146+
return -1; // TODO: Optionally handle this case more gracefully
147147
}
148148

149149
// Read the DelegateRemoteId (2 bytes) into the `id` variable
@@ -156,13 +156,11 @@ class Win32UdpTransport : public ITransport
156156
headerStream.read(reinterpret_cast<char*>(&seqNum), sizeof(seqNum));
157157
header.SetSeqNum(seqNum);
158158

159-
xstringstream argStream(std::ios::in | std::ios::out | std::ios::binary);
160-
161159
// Write the remaining argument data to stream
162-
argStream.write(m_buffer + DmqHeader::HEADER_SIZE, size - DmqHeader::HEADER_SIZE);
160+
is.write(m_buffer + DmqHeader::HEADER_SIZE, size - DmqHeader::HEADER_SIZE);
163161

164162
// argStream contains the serialized remote argument data
165-
return argStream;
163+
return 0; // Success
166164
}
167165

168166
private:

0 commit comments

Comments
 (0)