@@ -565,7 +565,7 @@ void Simulator::send(const SimulatorProtocol::Message& message)
565565 }
566566}
567567
568- void Simulator::receive (const SimulatorProtocol::Message& message)
568+ void Simulator::receive (const SimulatorProtocol::Message& message, size_t fromConnId )
569569{
570570 using namespace SimulatorProtocol ;
571571
@@ -669,6 +669,30 @@ void Simulator::receive(const SimulatorProtocol::Message& message)
669669 case OpCode::Handshake:
670670 case OpCode::HandshakeResponse:
671671 break ; // handled by SimulatorConnection already
672+ case OpCode::RequestChannel:
673+ {
674+ const auto & m = static_cast <const RequestChannel&>(message);
675+ std::lock_guard<std::mutex> lock (m_stateMutex);
676+
677+ const size_t count = staticData.sensors .size ();
678+ for (size_t i = 0 ; i < count; ++i)
679+ {
680+ const auto & sensor = staticData.sensors [i];
681+ if (m.channel != invalidAddress && m.channel != sensor.channel )
682+ continue ;
683+
684+ auto & sensorState = m_stateData.sensors [i];
685+
686+ for (const auto & connection : m_connections)
687+ {
688+ if (connection->connectionId () != fromConnId)
689+ continue ;
690+ connection->send (SimulatorProtocol::SensorChanged (sensor.channel , sensor.address , sensorState.value ));
691+ break ;
692+ }
693+ }
694+ break ;
695+ }
672696 }
673697}
674698
@@ -693,8 +717,13 @@ void Simulator::accept()
693717 {
694718 if (!ec)
695719 {
696- m_connections.emplace_back (std::make_shared<SimulatorConnection>(shared_from_this (), std::move (socket)))->start ();
697- sendInitialState (*m_connections.rbegin ());
720+ lastConnectionId++;
721+ if (lastConnectionId == invalidIndex)
722+ lastConnectionId = 0 ;
723+
724+ m_connections.emplace_back (std::make_shared<SimulatorConnection>(
725+ shared_from_this (), std::move (socket),
726+ lastConnectionId))->start ();
698727 accept ();
699728 }
700729 });
@@ -1643,15 +1672,3 @@ Simulator::StaticData Simulator::load(const nlohmann::json& world, StateData& st
16431672
16441673 return data;
16451674}
1646-
1647- void Simulator::sendInitialState (const std::shared_ptr<SimulatorConnection> &connection)
1648- {
1649- // Send current sensor state
1650- const size_t count = staticData.sensors .size ();
1651- for (size_t i = 0 ; i < count; ++i)
1652- {
1653- const auto & sensor = staticData.sensors [i];
1654- auto & sensorState = m_stateData.sensors [i];
1655- connection->send (SimulatorProtocol::SensorChanged (sensor.channel , sensor.address , sensorState.value ));
1656- }
1657- }
0 commit comments