Skip to content

Commit da98fba

Browse files
committed
[simulator] RequestChannel message instead of sendInitialState()
1 parent 066f570 commit da98fba

File tree

6 files changed

+59
-23
lines changed

6 files changed

+59
-23
lines changed

server/src/simulator/simulatoriohandler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ void SimulatorIOHandler::receive(const SimulatorProtocol::Message& message)
148148
break;
149149

150150
case OpCode::HandshakeResponse:
151-
// This is never sent by simulator, only by us in response
151+
case OpCode::RequestChannel:
152+
// This is never sent by simulator, only by simulator clients
152153
break;
153154
}
154155
}

shared/src/traintastic/simulator/protocol.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ enum class OpCode : uint8_t
3636
SensorChanged = 3,
3737
AccessorySetState = 4,
3838
Handshake = 5,
39-
HandshakeResponse = 6
39+
HandshakeResponse = 6,
40+
// Other messages
41+
RequestChannel = 9
4042
};
4143

4244
struct Message
@@ -128,6 +130,19 @@ struct AccessorySetState : Message
128130
} ATTRIBUTE_PACKED;
129131
static_assert(sizeof(AccessorySetState) == 7);
130132

133+
struct RequestChannel : Message
134+
{
135+
//! NOTE: pass invalidAddress to get all channels state
136+
uint16_t channel;
137+
138+
RequestChannel(uint16_t ch)
139+
: Message(OpCode::RequestChannel, sizeof(RequestChannel))
140+
, channel{ch}
141+
{
142+
}
143+
} ATTRIBUTE_PACKED;
144+
static_assert(sizeof(RequestChannel) == 4);
145+
131146
PRAGMA_PACK_POP
132147

133148
}

shared/src/traintastic/simulator/simulator.cpp

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
}

shared/src/traintastic/simulator/simulator.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class Simulator : public std::enable_shared_from_this<Simulator>
263263
void toggleTurnoutState(size_t segmentIndex);
264264

265265
void send(const SimulatorProtocol::Message& message);
266-
void receive(const SimulatorProtocol::Message& message);
266+
void receive(const SimulatorProtocol::Message& message, size_t fromConnId);
267267
void removeConnection(const std::shared_ptr<SimulatorConnection>& connection);
268268

269269
private:
@@ -292,8 +292,6 @@ class Simulator : public std::enable_shared_from_this<Simulator>
292292
void doReceive();
293293
void onConnectionRemoved(const std::shared_ptr<SimulatorConnection> &);
294294

295-
void sendInitialState(const std::shared_ptr<SimulatorConnection>& connection);
296-
297295
void tick();
298296
void handShake();
299297

shared/src/traintastic/simulator/simulatorconnection.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@
2323
#include "simulator.hpp"
2424
#include "protocol.hpp"
2525

26-
SimulatorConnection::SimulatorConnection(std::shared_ptr<Simulator> simulator, boost::asio::ip::tcp::socket&& socket)
26+
SimulatorConnection::SimulatorConnection(std::shared_ptr<Simulator> simulator, boost::asio::ip::tcp::socket&& socket, size_t connId)
2727
: m_simulator{std::move(simulator)}
2828
, m_socket{std::move(socket)}
29+
, m_connectionId(connId)
2930
{
3031
m_socket.set_option(boost::asio::ip::tcp::no_delay(true));
3132
m_socket.set_option(boost::asio::socket_base::send_buffer_size(8192 * 2));
@@ -90,7 +91,7 @@ void SimulatorConnection::read()
9091
}
9192
else
9293
{
93-
m_simulator->receive(*message);
94+
m_simulator->receive(*message, m_connectionId);
9495
}
9596

9697
pos += message->size;

shared/src/traintastic/simulator/simulatorconnection.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,16 @@ class SimulatorConnection : public std::enable_shared_from_this<SimulatorConnect
4141
size_t m_readBufferOffset = 0;
4242
std::array<std::byte, 1024> m_writeBuffer;
4343
size_t m_writeBufferOffset = 0;
44+
size_t m_connectionId = 0;
4445
bool m_handShakeResponseReceived = true;
4546

4647
void read();
4748
void write();
4849
void close();
4950

5051
public:
51-
SimulatorConnection(std::shared_ptr<Simulator> simulator, boost::asio::ip::tcp::socket&& socket);
52+
SimulatorConnection(std::shared_ptr<Simulator> simulator, boost::asio::ip::tcp::socket&& socket,
53+
size_t connId);
5254

5355
void start();
5456
void stop();
@@ -61,6 +63,8 @@ class SimulatorConnection : public std::enable_shared_from_this<SimulatorConnect
6163
{
6264
m_handShakeResponseReceived = newHandShakeResponseReceived;
6365
}
66+
67+
inline size_t connectionId() const { return m_connectionId; }
6468
};
6569

6670
#endif

0 commit comments

Comments
 (0)