mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-26 03:46:28 -08:00
Fix race condition in DataFeedHandler::sendDataFeedUpdate (#1621)
Co-authored-by: lucas lelievre <[email protected]> Co-authored-by: Butterscotch! <[email protected]>
This commit is contained in:
3 files changed
+67
-51
No files matched your search
@@ -1,20 +1,12 @@
|
||||
package dev.slimevr.protocol;
|
||||
|
||||
import solarxr_protocol.data_feed.DataFeedConfigT;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public class ConnectionContext {
|
||||
|
||||
private final List<DataFeedConfigT> dataFeedConfigList = new ArrayList<>();
|
||||
|
||||
// I did it in a separate array because it was more convenient than making a
|
||||
// parent object of the DataFeedConfigT
|
||||
// idk if it should be a concern or not, i think it is fine tbh
|
||||
// Futurabeast
|
||||
private final List<Long> dataFeedTimers = new ArrayList<>();
|
||||
private final List<DataFeed> dataFeedList = new ArrayList<>();
|
||||
|
||||
private final List<Integer> subscribedTopics = new ArrayList<>();
|
||||
|
||||
@@ -23,17 +15,8 @@ public class ConnectionContext {
|
||||
private boolean useProvisioning = false;
|
||||
private boolean useAutoBone = false;
|
||||
|
||||
public List<DataFeedConfigT> getDataFeedConfigList() {
|
||||
return dataFeedConfigList;
|
||||
}
|
||||
|
||||
public List<Long> getDataFeedTimers() {
|
||||
return dataFeedTimers;
|
||||
}
|
||||
|
||||
public void clearDataFeeds() {
|
||||
this.dataFeedConfigList.clear();
|
||||
this.dataFeedTimers.clear();
|
||||
public List<DataFeed> getDataFeedList() {
|
||||
return dataFeedList;
|
||||
}
|
||||
|
||||
public List<Integer> getSubscribedTopics() {
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package dev.slimevr.protocol;
|
||||
|
||||
import solarxr_protocol.data_feed.DataFeedConfigT;
|
||||
|
||||
|
||||
public class DataFeed {
|
||||
private DataFeedConfigT config;
|
||||
private Long timeLastSent;
|
||||
|
||||
public DataFeed(DataFeedConfigT config) {
|
||||
this.config = config;
|
||||
this.timeLastSent = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public DataFeedConfigT getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public Long getTimeLastSent() {
|
||||
return timeLastSent;
|
||||
}
|
||||
|
||||
public void setTimeLastSent(Long timeLastSent) {
|
||||
this.timeLastSent = timeLastSent;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.slimevr.protocol.datafeed;
|
||||
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import dev.slimevr.protocol.DataFeed;
|
||||
import dev.slimevr.protocol.GenericConnection;
|
||||
import dev.slimevr.protocol.ProtocolAPI;
|
||||
import dev.slimevr.protocol.ProtocolHandler;
|
||||
@@ -9,12 +10,12 @@ import io.eiren.util.logging.LogManager;
|
||||
import solarxr_protocol.MessageBundle;
|
||||
import solarxr_protocol.data_feed.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
public class DataFeedHandler extends ProtocolHandler<DataFeedMessageHeader> {
|
||||
|
||||
private final ProtocolAPI api;
|
||||
|
||||
public DataFeedHandler(ProtocolAPI api) {
|
||||
@@ -32,14 +33,16 @@ public class DataFeedHandler extends ProtocolHandler<DataFeedMessageHeader> {
|
||||
return;
|
||||
int dataFeeds = req.dataFeedsLength();
|
||||
|
||||
conn.getContext().clearDataFeeds();
|
||||
for (int i = 0; i < dataFeeds; i++) {
|
||||
// Using the object api here because we need to copy from the buffer
|
||||
// anyway so
|
||||
// let's do it from here and send the reference to an arraylist
|
||||
DataFeedConfigT config = req.dataFeeds(i).unpack();
|
||||
conn.getContext().getDataFeedConfigList().add(config);
|
||||
conn.getContext().getDataFeedTimers().add(System.currentTimeMillis());
|
||||
List<DataFeed> feedList = conn.getContext().getDataFeedList();
|
||||
synchronized (feedList) {
|
||||
feedList.clear();
|
||||
for (int i = 0; i < dataFeeds; i++) {
|
||||
// Using the object api here because we
|
||||
// need to copy from the buffer, anyway let's
|
||||
// do it from here and send the reference to an arraylist
|
||||
DataFeedConfigT config = req.dataFeeds(i).unpack();
|
||||
feedList.add(new DataFeed(config));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,31 +124,35 @@ public class DataFeedHandler extends ProtocolHandler<DataFeedMessageHeader> {
|
||||
this.api.getAPIServers().forEach((server) -> server.getAPIConnections().forEach((conn) -> {
|
||||
FlatBufferBuilder fbb = null;
|
||||
|
||||
int configsCount = conn.getContext().getDataFeedConfigList().size();
|
||||
List<DataFeed> feedList = conn.getContext().getDataFeedList();
|
||||
synchronized (feedList) {
|
||||
int configsCount = feedList.size();
|
||||
|
||||
int[] data = new int[configsCount];
|
||||
int[] data = new int[configsCount];
|
||||
|
||||
for (int index = 0; index < configsCount; index++) {
|
||||
Long lastTimeSent = conn.getContext().getDataFeedTimers().get(index);
|
||||
DataFeedConfigT configT = conn.getContext().getDataFeedConfigList().get(index);
|
||||
if (currTime - lastTimeSent > configT.getMinimumTimeSinceLast()) {
|
||||
if (fbb == null) {
|
||||
// That way we create a buffer only when needed
|
||||
fbb = new FlatBufferBuilder(300);
|
||||
for (int index = 0; index < configsCount; index++) {
|
||||
DataFeed feed = feedList.get(index);
|
||||
Long lastTimeSent = feed.getTimeLastSent();
|
||||
DataFeedConfigT configT = feed.getConfig();
|
||||
if (currTime - lastTimeSent > configT.getMinimumTimeSinceLast()) {
|
||||
if (fbb == null) {
|
||||
// That way we create a buffer only when needed
|
||||
fbb = new FlatBufferBuilder(300);
|
||||
}
|
||||
|
||||
int messageOffset = this.buildDatafeed(fbb, configT, index);
|
||||
|
||||
DataFeedMessageHeader.startDataFeedMessageHeader(fbb);
|
||||
DataFeedMessageHeader.addMessage(fbb, messageOffset);
|
||||
DataFeedMessageHeader.addMessageType(fbb, DataFeedMessage.DataFeedUpdate);
|
||||
data[index] = DataFeedMessageHeader.endDataFeedMessageHeader(fbb);
|
||||
|
||||
feed.setTimeLastSent(currTime);
|
||||
int messages = MessageBundle.createDataFeedMsgsVector(fbb, data);
|
||||
int packet = createMessage(fbb, messages);
|
||||
fbb.finish(packet);
|
||||
conn.send(fbb.dataBuffer());
|
||||
}
|
||||
|
||||
int messageOffset = this.buildDatafeed(fbb, configT, index);
|
||||
|
||||
DataFeedMessageHeader.startDataFeedMessageHeader(fbb);
|
||||
DataFeedMessageHeader.addMessage(fbb, messageOffset);
|
||||
DataFeedMessageHeader.addMessageType(fbb, DataFeedMessage.DataFeedUpdate);
|
||||
data[index] = DataFeedMessageHeader.endDataFeedMessageHeader(fbb);
|
||||
|
||||
conn.getContext().getDataFeedTimers().set(index, currTime);
|
||||
int messages = MessageBundle.createDataFeedMsgsVector(fbb, data);
|
||||
int packet = createMessage(fbb, messages);
|
||||
fbb.finish(packet);
|
||||
conn.send(fbb.dataBuffer());
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
Reference in new issue
Block a user