@@ -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}
0 commit comments