Skip to content

Commit 2ec7d8e

Browse files
Fix dropped startup data and resubscribe defects, update to v1.1.4
- Subscriber now delivers data published before signal index cache confirmation - Publisher now flags data packets with the active signal index cache index - Resubscribe no longer stops subscriber command channel processing - Faster metadata, timestamp, string and signal index cache processing - Added PublisherCacheIndexTest and extended SubscriberStartupTest - Updated C++ API version to 1.1.4 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 07c8327 commit 2ec7d8e

18 files changed

Lines changed: 957 additions & 56 deletions

‎src/CMakeLists.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ target_link_libraries (ConfigurationFramesTest sttp)
499499
add_executable (SubscriberStartupTest EXCLUDE_FROM_ALL samples/SubscriberStartupTest/SubscriberStartupTest.cpp)
500500
target_link_libraries (SubscriberStartupTest sttp)
501501

502+
# PublisherCacheIndexTest sample
503+
add_executable (PublisherCacheIndexTest EXCLUDE_FROM_ALL samples/PublisherCacheIndexTest/PublisherCacheIndexTest.cpp)
504+
target_link_libraries (PublisherCacheIndexTest sttp)
505+
502506
# SubscriberShutdownTest sample
503507
add_executable (SubscriberShutdownTest EXCLUDE_FROM_ALL samples/SubscriberShutdownTest/SubscriberShutdownTest.cpp)
504508
target_link_libraries (SubscriberShutdownTest sttp)
@@ -549,6 +553,7 @@ add_custom_target (samples DEPENDS
549553
AdvancedSubscribe
550554
AverageFrequencyCalculator
551555
ConfigurationFramesTest
556+
PublisherCacheIndexTest
552557
SubscriberShutdownTest
553558
SubscriberStartupTest
554559
DynamicMetadataPublish

‎src/lib/CommonTypes.cpp‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ size_t StringHash::operator()(const string& value) const
7070
size_t seed = 0;
7171
const locale locale;
7272

73+
// Character facet is resolved once, per-character locale lookups are
74+
// expensive and this hash is used for every lookup into a StringMap
75+
const ctype<char>& facet = use_facet<ctype<char>>(locale);
76+
7377
for (const auto it : value)
74-
hash_combine(seed, toupper(it, locale));
78+
hash_combine(seed, facet.toupper(it));
7579

7680
return seed;
7781
}
@@ -132,9 +136,24 @@ bool sttp::IsEmptyOrWhiteSpace(const string& value)
132136

133137
bool sttp::IsEqual(const string& left, const string& right, const bool ignoreCase)
134138
{
135-
return ignoreCase ?
136-
iequals(left, right) :
137-
equals(left, right);
139+
if (!ignoreCase)
140+
return equals(left, right);
141+
142+
if (left.size() != right.size())
143+
return false;
144+
145+
// Character facet is resolved once, per-character locale lookups are
146+
// expensive and this comparison is used for every lookup into a StringMap
147+
const locale locale;
148+
const ctype<char>& facet = use_facet<ctype<char>>(locale);
149+
150+
for (size_t i = 0; i < left.size(); i++)
151+
{
152+
if (left[i] != right[i] && facet.toupper(left[i]) != facet.toupper(right[i]))
153+
return false;
154+
}
155+
156+
return true;
138157
}
139158

140159
bool sttp::StartsWith(const string& value, const string& findValue, const bool ignoreCase)
@@ -276,12 +295,24 @@ string sttp::Replace(const string& value, const string& findValue, const string&
276295

277296
string sttp::ToUpper(const string& value)
278297
{
279-
return to_upper_copy(value);
298+
// Character facet converts full range, avoids per-character locale lookups
299+
const locale locale;
300+
string result = value;
301+
302+
use_facet<ctype<char>>(locale).toupper(result.data(), result.data() + result.size());
303+
304+
return result;
280305
}
281306

282307
string sttp::ToLower(const string& value)
283308
{
284-
return to_lower_copy(value);
309+
// Character facet converts full range, avoids per-character locale lookups
310+
const locale locale;
311+
string result = value;
312+
313+
use_facet<ctype<char>>(locale).tolower(result.data(), result.data() + result.size());
314+
315+
return result;
285316
}
286317

287318
string sttp::Trim(const string& value)

‎src/lib/CommonTypes.h‎

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -413,24 +413,50 @@ namespace sttp
413413
}
414414
};
415415

416+
// Buffer size for stream filters, e.g., compression, that are read in blocks. Filters otherwise
417+
// default to very small buffers which, for zlib, also bypasses its optimized inflate code path.
418+
static constexpr std::streamsize StreamFilterBufferSize = 65536;
419+
416420
template<class T, class TElem = char>
417421
void CopyStream(T* source, std::vector<uint8_t>& sink)
418422
{
419-
std::istreambuf_iterator<TElem> it{ source };
420-
std::istreambuf_iterator<TElem> eos{};
423+
if (source == nullptr)
424+
return;
425+
426+
if constexpr (sizeof(TElem) == 1)
427+
{
428+
// Read in blocks, per-element iteration is slow for large streams, e.g., metadata
429+
static constexpr size_t BlockSize = static_cast<size_t>(StreamFilterBufferSize);
430+
size_t length = sink.size();
431+
432+
while (true)
433+
{
434+
sink.resize(length + BlockSize);
435+
436+
const std::streamsize count = source->sgetn(reinterpret_cast<TElem*>(sink.data() + length), static_cast<std::streamsize>(BlockSize));
437+
438+
if (count <= 0)
439+
break;
421440

422-
for (; it != eos; ++it)
423-
sink.push_back(static_cast<uint8_t>(*it));
441+
length += static_cast<size_t>(count);
442+
}
443+
444+
sink.resize(length);
445+
}
446+
else
447+
{
448+
std::istreambuf_iterator<TElem> it{ source };
449+
std::istreambuf_iterator<TElem> eos{};
450+
451+
for (; it != eos; ++it)
452+
sink.push_back(static_cast<uint8_t>(*it));
453+
}
424454
}
425455

426456
template<class T, class TElem = char>
427457
void CopyStream(T& source, std::vector<uint8_t>& sink)
428458
{
429-
std::istreambuf_iterator<TElem> it{ source };
430-
std::istreambuf_iterator<TElem> eos{};
431-
432-
for (; it != eos; ++it)
433-
sink.push_back(static_cast<uint8_t>(*it));
459+
CopyStream<std::basic_streambuf<TElem>, TElem>(source.rdbuf(), sink);
434460
}
435461

436462
template<class T>

‎src/lib/Convert.cpp‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,111 @@ const char* sttp::Coalesce(const char* data, const char* nonEmptyValue)
660660
return data;
661661
}
662662

663+
// Attempt to parse a fully specified ISO 8601 style timestamp, e.g.: 2018-03-14T19:23:11.665-04:00
664+
// This is the format used for timestamps in metadata exchange where a very large number of values
665+
// may need to be parsed, so fixed positions are read directly. Any deviation from this format, or
666+
// any unusual value, returns false so that general parsing can be applied instead.
667+
bool TryParseISOTimestamp(const char* time, datetime_t& timestamp, TimeSpan& utcOffset)
668+
{
669+
// A null terminator fails the digit test, so parsing never reads beyond end of string
670+
const auto parseDigits = [](const char* value, const int32_t count, int32_t& result)
671+
{
672+
result = 0;
673+
674+
for (int32_t i = 0; i < count; i++)
675+
{
676+
if (value[i] < '0' || value[i] > '9')
677+
return false;
678+
679+
result = result * 10 + (value[i] - '0');
680+
}
681+
682+
return true;
683+
};
684+
685+
int32_t year, month, day, hour, minute, second;
686+
687+
// Fixed width date and time: YYYY-MM-DDTHH:MM:SS
688+
if (!parseDigits(time, 4, year) || time[4] != '-' ||
689+
!parseDigits(time + 5, 2, month) || time[7] != '-' ||
690+
!parseDigits(time + 8, 2, day) || (time[10] != 'T' && time[10] != 't' && time[10] != ' ') ||
691+
!parseDigits(time + 11, 2, hour) || time[13] != ':' ||
692+
!parseDigits(time + 14, 2, minute) || time[16] != ':' ||
693+
!parseDigits(time + 17, 2, second))
694+
return false;
695+
696+
if (year < 1400 || month < 1 || month > 12 || day < 1 || hour > 23 || minute > 59 || second > 59)
697+
return false;
698+
699+
if (day > gregorian_calendar::end_of_month_day(static_cast<uint16_t>(year), static_cast<uint16_t>(month)))
700+
return false;
701+
702+
const char* position = time + 19;
703+
int64_t fraction = 0;
704+
705+
// Optional fractional seconds: digits beyond supported resolution are dropped
706+
if (*position == '.')
707+
{
708+
const int32_t precision = TimeSpan::num_fractional_digits();
709+
int32_t digits = 0;
710+
711+
for (++position; *position >= '0' && *position <= '9'; ++position, ++digits)
712+
{
713+
if (digits < precision)
714+
fraction = fraction * 10 + (*position - '0');
715+
}
716+
717+
if (digits == 0)
718+
return false;
719+
720+
for (; digits < precision; ++digits)
721+
fraction *= 10;
722+
}
723+
724+
int32_t offsetHours = 0, offsetMinutes = 0;
725+
bool negativeOffset = false;
726+
727+
// Optional time zone: Z, +HH:MM, -HH:MM, +HHMM or -HHMM
728+
if (*position == 'Z' || *position == 'z')
729+
{
730+
++position;
731+
}
732+
else if (*position == '+' || *position == '-')
733+
{
734+
negativeOffset = *position == '-';
735+
736+
if (!parseDigits(++position, 2, offsetHours))
737+
return false;
738+
739+
position += 2;
740+
741+
if (*position == ':')
742+
++position;
743+
744+
if (!parseDigits(position, 2, offsetMinutes))
745+
return false;
746+
747+
position += 2;
748+
749+
if (offsetHours > 23 || offsetMinutes > 59 || (offsetHours == 0 && offsetMinutes != 0))
750+
return false;
751+
}
752+
753+
// Entire string must be consumed
754+
if (*position != '\0')
755+
return false;
756+
757+
timestamp = datetime_t(date(static_cast<uint16_t>(year), static_cast<uint16_t>(month), static_cast<uint16_t>(day)), TimeSpan(hour, minute, second, fraction));
758+
759+
// Time zone sign is swapped for conversion to UTC
760+
utcOffset = TimeSpan(offsetHours, offsetMinutes, 0);
761+
762+
if (!negativeOffset)
763+
utcOffset = -utcOffset;
764+
765+
return true;
766+
}
767+
663768
// Attempt to parse a timestamp string, e.g.: 2018-03-14T19:23:11.665-04:00
664769
bool sttp::TryParseTimestamp(const char* time, datetime_t& timestamp, const datetime_t& defaultValue, const bool parseAsUTC)
665770
{
@@ -669,6 +774,16 @@ bool sttp::TryParseTimestamp(const char* time, datetime_t& timestamp, const date
669774
};
670775

671776
TimeSpan utcOffset{};
777+
778+
// Common timestamp format is parsed directly, this is much faster than general parsing
779+
if (time != nullptr && TryParseISOTimestamp(time, timestamp, utcOffset))
780+
{
781+
if (parseAsUTC)
782+
timestamp += utcOffset;
783+
784+
return true;
785+
}
786+
672787
const string cleanTimestamp = PreparseTimestamp(time, utcOffset);
673788

674789
for (const locale& format : formats)

‎src/lib/Version.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ constexpr auto STTP_TITLE = "STTP C++ Library";
2828

2929
// Defines the STTP library API version used for data subscriber identification.
3030
// Note: This is not the STTP protocol version, but the version of the STTP library API.
31-
constexpr auto STTP_VERSION = "1.1.3";
31+
constexpr auto STTP_VERSION = "1.1.4";
3232

3333
// Defines when the STTP library API was last updated used for data subscriber identification.
34-
constexpr auto STTP_UPDATEDON = "2026-09-15";
34+
constexpr auto STTP_UPDATEDON = "2026-09-17";

‎src/lib/transport/DataSubscriber.cpp‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ DataSubscriber::DataSubscriber() :
7777
m_subscribed(false),
7878
m_disconnecting(false),
7979
m_disconnected(false),
80+
m_dataChannelDisconnecting(false),
8081
m_userData(nullptr),
8182
m_totalCommandChannelBytesReceived(0UL),
8283
m_totalDataChannelBytesReceived(0UL),
@@ -261,7 +262,7 @@ void DataSubscriber::RunDataChannelResponseThread()
261262
{
262263
const uint32_t length = ConvertUInt32(m_dataChannelSocket.receive_from(asio::buffer(buffer), endpoint, 0, error));
263264

264-
if (IsDisconnecting())
265+
if (IsDisconnecting() || m_dataChannelDisconnecting)
265266
break;
266267

267268
if (error)
@@ -473,7 +474,7 @@ void DataSubscriber::HandleUpdateSignalIndexCache(const uint8_t* data, uint32_t
473474
// Perform zlib decompression on buffer
474475
StreamBuffer streamBuffer;
475476

476-
streamBuffer.push(GZipDecompressor());
477+
streamBuffer.push(GZipDecompressor(), StreamFilterBufferSize);
477478
streamBuffer.push(memoryStream);
478479

479480
CopyStream(&streamBuffer, uncompressedBuffer);
@@ -489,12 +490,24 @@ void DataSubscriber::HandleUpdateSignalIndexCache(const uint8_t* data, uint32_t
489490
m_signalIndexCacheMutex.lock();
490491
m_signalIndexCache[cacheIndex].swap(signalIndexCache);
491492
m_cacheIndex = cacheIndex;
493+
const SignalIndexCachePtr activeSignalIndexCache = m_signalIndexCache[cacheIndex];
494+
495+
// For the first cache of a connection, publisher applies the same signal mappings to its current
496+
// cache index and starts publishing with it right away, i.e., before confirmation of this cache
497+
// is processed. Data received in this window is flagged with the alternate cache index, so this
498+
// cache is referenced there as well; otherwise, all initial data would be dropped. This matters
499+
// when the window is long, e.g., publisher is busy establishing a large subscription, or when
500+
// the data is the only data for some time, e.g., cached values for slowly updating signals.
501+
// Publisher resets TSSC when it transitions to the confirmed cache, see ParseTSSCMeasurements.
502+
if (m_version > 1 && m_signalIndexCache[cacheIndex ^ 1] == nullptr)
503+
m_signalIndexCache[cacheIndex ^ 1] = activeSignalIndexCache;
504+
492505
m_signalIndexCacheMutex.unlock();
493506

494507
if (m_version > 1)
495508
SendServerCommand(ServerCommand::ConfirmUpdateSignalIndexCache);
496509

497-
DispatchSubscriptionUpdated(AddDispatchReference(m_signalIndexCache[cacheIndex]));
510+
DispatchSubscriptionUpdated(AddDispatchReference(activeSignalIndexCache));
498511
}
499512

500513
// Updates base time offsets.
@@ -766,13 +779,11 @@ void DataSubscriber::Dispatch(const DispatcherFunction& function, const uint8_t*
766779
CallbackDispatcher dispatcher;
767780
const SharedPtr<vector<uint8_t>> dataVector = NewSharedPtr<vector<uint8_t>>();
768781

769-
dataVector->resize(length);
770-
782+
// Payloads can be large, e.g., metadata, so copy as a block
771783
if (data != nullptr)
772-
{
773-
for (uint32_t i = 0; i < length; ++i)
774-
dataVector->at(i) = data[offset + i];
775-
}
784+
dataVector->assign(data + offset, data + offset + length);
785+
else
786+
dataVector->resize(length);
776787

777788
dispatcher.Source = this;
778789
dispatcher.Data = dataVector;
@@ -1374,6 +1385,13 @@ void DataSubscriber::SetupConnection()
13741385
m_totalDataChannelBytesReceived = 0UL;
13751386
m_totalMeasurementsReceived = 0UL;
13761387

1388+
// Signal index caches only apply to the connection that defined them
1389+
m_signalIndexCacheMutex.lock();
1390+
m_signalIndexCache[0] = SignalIndexCache::NullPtr;
1391+
m_signalIndexCache[1] = SignalIndexCache::NullPtr;
1392+
m_cacheIndex = 0;
1393+
m_signalIndexCacheMutex.unlock();
1394+
13771395
// TODO: Clear UDP key and initialization vectors
13781396
// TODO: Clear buffer block expected sequence number
13791397
// TODO: Reinitialize measurement metadata registry
@@ -1501,11 +1519,14 @@ void DataSubscriber::Unsubscribe()
15011519
{
15021520
ErrorCode error;
15031521

1504-
m_disconnecting = true;
1522+
// Only the data channel is being torn down here - a subscription change is not a disconnect, so
1523+
// the subscriber-wide disconnecting flag must not be set: command channel reads and the callback
1524+
// thread check that flag and would permanently stop if they observed it during a resubscribe.
1525+
m_dataChannelDisconnecting = true;
15051526
m_dataChannelSocket.shutdown(UdpSocket::shutdown_receive, error);
15061527
m_dataChannelSocket.close(error);
15071528
m_dataChannelResponseThread.join();
1508-
m_disconnecting = false;
1529+
m_dataChannelDisconnecting = false;
15091530

15101531
SendServerCommand(ServerCommand::Unsubscribe);
15111532
}

‎src/lib/transport/DataSubscriber.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ namespace sttp::transport
8686
Thread m_disconnectThread;
8787
std::atomic_bool m_disconnecting;
8888
std::atomic_bool m_disconnected;
89+
std::atomic_bool m_dataChannelDisconnecting;
8990

9091
void* m_userData;
9192

0 commit comments

Comments
 (0)