1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include "StarSystemWorldServerThread.hpp"
#include "StarTickRateMonitor.hpp"
#include "StarNetPackets.hpp"
namespace Star {
SystemWorldServerThread::SystemWorldServerThread(Vec3I const& location, SystemWorldServerPtr systemWorld, String storageFile)
: Thread(strf("SystemWorldServer: {}", location))
, m_systemLocation(location)
, m_systemWorld(std::move(systemWorld))
, m_storageFile(storageFile)
{
}
SystemWorldServerThread::~SystemWorldServerThread() {
m_stop = true;
join();
}
Vec3I SystemWorldServerThread::location() const {
return m_systemLocation;
}
List<ConnectionId> SystemWorldServerThread::clients() {
return m_clients.values();
}
void SystemWorldServerThread::addClient(ConnectionId clientId, Uuid const& uuid, float shipSpeed, SystemLocation const& location) {
WriteLocker locker(m_mutex);
m_clients.add(clientId);
m_outgoingPacketQueue.set(clientId, List<PacketPtr>());
m_systemWorld->addClientShip(clientId, uuid, shipSpeed, location);
m_clientShipLocations.set(clientId, {m_systemWorld->clientShipLocation(clientId), m_systemWorld->clientSkyParameters(clientId)});
if (auto warpAction = m_systemWorld->clientWarpAction(clientId))
m_clientWarpActions.set(clientId, *warpAction);
}
void SystemWorldServerThread::removeClient(ConnectionId clientId) {
WriteLocker locker(m_mutex);
m_systemWorld->removeClientShip(clientId);
m_clients.remove(clientId);
m_clientShipDestinations.remove(clientId);
m_clientShipLocations.remove(clientId);
m_outgoingPacketQueue.remove(clientId);
}
void SystemWorldServerThread::setPause(shared_ptr<const atomic<bool>> pause) {
m_pause = std::move(pause);
}
void SystemWorldServerThread::run() {
TickRateApproacher tickApproacher(1.0 / SystemWorldTimestep, 0.5);
while (!m_stop) {
LogMap::set(strf("system_{}_update_rate", m_systemLocation), strf("{:4.2f}Hz", tickApproacher.rate()));
update();
m_periodicStorage -= 1.0 / tickApproacher.rate();
if (m_triggerStorage || m_periodicStorage <= 0.0) {
m_triggerStorage = false;
m_periodicStorage = 300.0; // store every 5 minutes
store();
}
tickApproacher.tick();
double spareTime = tickApproacher.spareTime();
uint64_t millis = floor(spareTime * 1000);
if (spareTime > 0)
sleepPrecise(millis);
}
store();
}
void SystemWorldServerThread::stop() {
m_stop = true;
}
void SystemWorldServerThread::update() {
WriteLocker queueLocker(m_queueMutex);
WriteLocker locker(m_mutex);
for (auto p : take(m_incomingPacketQueue))
m_systemWorld->handleIncomingPacket(p.first, p.second);
for (auto p : take(m_clientShipActions))
p.second(m_systemWorld->clientShip(p.first).get());
if (!m_pause || *m_pause == false)
m_systemWorld->update(SystemWorldTimestep * GlobalTimescale);
m_triggerStorage = m_systemWorld->triggeredStorage();
// important to set destinations before getting locations
// setting a destination nullifies the current location
for (auto p : take(m_clientShipDestinations))
m_systemWorld->setClientDestination(p.first, p.second);
m_activeInstanceWorlds = m_systemWorld->activeInstanceWorlds();
for (auto clientId : m_clients) {
m_outgoingPacketQueue[clientId].appendAll(m_systemWorld->pullOutgoingPackets(clientId));
auto shipSystemLocation = m_systemWorld->clientShipLocation(clientId);
auto& shipLocation = m_clientShipLocations[clientId];
if (shipLocation.first != shipSystemLocation) {
shipLocation.first = shipSystemLocation;
shipLocation.second = m_systemWorld->clientSkyParameters(clientId);
}
if (auto warpAction = m_systemWorld->clientWarpAction(clientId))
m_clientWarpActions.set(clientId, *warpAction);
else if (m_clientWarpActions.contains(clientId))
m_clientWarpActions.remove(clientId);
}
queueLocker.unlock();
if (m_updateAction)
m_updateAction(this);
}
void SystemWorldServerThread::setClientDestination(ConnectionId clientId, SystemLocation const& destination) {
WriteLocker locker(m_queueMutex);
m_clientShipDestinations.set(clientId, destination);
}
void SystemWorldServerThread::executeClientShipAction(ConnectionId clientId, ClientShipAction action) {
WriteLocker locker(m_queueMutex);
m_clientShipActions.append({clientId, std::move(action)});
}
SystemLocation SystemWorldServerThread::clientShipLocation(ConnectionId clientId) {
ReadLocker locker(m_queueMutex);
// while a ship destination is pending the ship is assumed to be flying
if (m_clientShipDestinations.contains(clientId))
return {};
return m_clientShipLocations.get(clientId).first;
}
Maybe<pair<WarpAction, WarpMode>> SystemWorldServerThread::clientWarpAction(ConnectionId clientId) {
ReadLocker locker(m_queueMutex);
if (m_clientShipDestinations.contains(clientId))
return {};
return m_clientWarpActions.maybe(clientId);
}
SkyParameters SystemWorldServerThread::clientSkyParameters(ConnectionId clientId) {
ReadLocker locker(m_queueMutex);
return m_clientShipLocations.get(clientId).second;
}
List<InstanceWorldId> SystemWorldServerThread::activeInstanceWorlds() const {
return m_activeInstanceWorlds;
}
void SystemWorldServerThread::setUpdateAction(function<void(SystemWorldServerThread*)> updateAction) {
m_updateAction = updateAction;
}
void SystemWorldServerThread::pushIncomingPacket(ConnectionId clientId, PacketPtr packet) {
WriteLocker locker(m_queueMutex);
m_incomingPacketQueue.append({std::move(clientId), std::move(packet)});
}
List<PacketPtr> SystemWorldServerThread::pullOutgoingPackets(ConnectionId clientId) {
WriteLocker locker(m_queueMutex);
return take(m_outgoingPacketQueue[clientId]);
}
void SystemWorldServerThread::store() {
ReadLocker locker(m_mutex);
Json store = m_systemWorld->diskStore();
locker.unlock();
Logger::debug("Trigger disk storage for system world {}:{}:{}", m_systemLocation.x(), m_systemLocation.y(), m_systemLocation.z());
auto versioningDatabase = Root::singleton().versioningDatabase();
auto versionedStore = versioningDatabase->makeCurrentVersionedJson("System", store);
VersionedJson::writeFile(versionedStore, m_storageFile);
}
}
|