mirror of
https://github.com/LeavesMC/Leaves.git
synced 2026-01-04 15:41:31 +00:00
Always sync process and async save (Replay api) (#523)
This commit is contained in:
@@ -46,21 +46,21 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class Recorder extends Connection {
|
||||
|
||||
private static final LeavesLogger LOGGER = LeavesLogger.LOGGER;
|
||||
public static final Executor saveService = Executors.newVirtualThreadPerTaskExecutor();
|
||||
public static final LeavesLogger LOGGER = LeavesLogger.LOGGER;
|
||||
|
||||
private final ReplayFile replayFile;
|
||||
private final ServerPhotographer photographer;
|
||||
private final RecorderOption recorderOption;
|
||||
private final RecordMetaData metaData;
|
||||
|
||||
private final ExecutorService saveService = Executors.newSingleThreadExecutor();
|
||||
|
||||
private boolean stopped = false;
|
||||
private boolean paused = false;
|
||||
private boolean resumeOnNextPacket = true;
|
||||
@@ -209,7 +209,7 @@ public class Recorder extends Connection {
|
||||
}
|
||||
|
||||
private void saveMetadata() {
|
||||
saveService.submit(() -> {
|
||||
saveService.execute(() -> {
|
||||
try {
|
||||
replayFile.saveMetaData(metaData);
|
||||
} catch (IOException e) {
|
||||
@@ -225,14 +225,7 @@ public class Recorder extends Connection {
|
||||
private void savePacket(Packet<?> packet, final ConnectionProtocol protocol) {
|
||||
try {
|
||||
final long timestamp = getCurrentTimeAndUpdate();
|
||||
saveService.submit(() -> {
|
||||
try {
|
||||
replayFile.savePacket(timestamp, packet, protocol);
|
||||
} catch (Exception e) {
|
||||
LOGGER.severe("Error saving packet");
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
replayFile.savePacket(timestamp, packet, protocol);
|
||||
} catch (Exception e) {
|
||||
LOGGER.severe("Error saving packet");
|
||||
e.printStackTrace();
|
||||
@@ -250,13 +243,7 @@ public class Recorder extends Connection {
|
||||
metaData.duration = (int) lastPacket;
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
saveMetadata();
|
||||
saveService.shutdown();
|
||||
boolean interrupted = false;
|
||||
try {
|
||||
saveService.awaitTermination(10, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
interrupted = true;
|
||||
}
|
||||
try {
|
||||
if (save) {
|
||||
replayFile.closeAndSave(dest);
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.leavesmc.leaves.replay;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.network.ConnectionProtocol;
|
||||
@@ -37,6 +38,9 @@ import java.util.zip.CRC32;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.leavesmc.leaves.replay.Recorder.LOGGER;
|
||||
import static org.leavesmc.leaves.replay.Recorder.saveService;
|
||||
|
||||
public class ReplayFile {
|
||||
|
||||
private static final String RECORDING_FILE = "recording.tmcpr";
|
||||
@@ -93,8 +97,7 @@ public class ReplayFile {
|
||||
protocol.codec().encode(buf, packet);
|
||||
|
||||
buf.readerIndex(0);
|
||||
byte[] ret = new byte[buf.readableBytes()];
|
||||
buf.readBytes(ret);
|
||||
byte[] ret = ByteBufUtil.getBytes(buf);
|
||||
buf.release();
|
||||
return ret;
|
||||
}
|
||||
@@ -118,9 +121,16 @@ public class ReplayFile {
|
||||
|
||||
public void savePacket(long timestamp, Packet<?> packet, ConnectionProtocol protocol) throws Exception {
|
||||
byte[] data = getPacketBytes(packet, protocol);
|
||||
packetStream.writeInt((int) timestamp);
|
||||
packetStream.writeInt(data.length);
|
||||
packetStream.write(data);
|
||||
saveService.execute(() -> {
|
||||
try {
|
||||
packetStream.writeInt((int) timestamp);
|
||||
packetStream.writeInt(data.length);
|
||||
packetStream.write(data);
|
||||
} catch (Exception e) {
|
||||
LOGGER.severe("Error saving packet");
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public synchronized void closeAndSave(File file) throws IOException {
|
||||
|
||||
Reference in New Issue
Block a user