mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-10 17:31:28 +01:00
fix Low accuracy tick rate
(cherry picked from commit ce9d254a23791403a2b19ee4bb6bb52e175fa5dd)
(cherry picked from commit 4e33a5e6d8
)
This commit is contained in:
parent
c3a9841967
commit
7cbce377a7
@ -297,6 +297,9 @@ final class ServerProcessImpl implements ServerProcess {
|
||||
// Flush all waiting packets
|
||||
PacketUtils.flush();
|
||||
|
||||
// Server connection tick
|
||||
server().tick();
|
||||
|
||||
// Monitoring
|
||||
{
|
||||
final double acquisitionTimeMs = Acquirable.resetAcquiringTime() / 1e6D;
|
||||
|
@ -91,6 +91,10 @@ public final class Server {
|
||||
}, "Ms-entrypoint").start();
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
this.workers.forEach(Worker::tick);
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
return !stop;
|
||||
}
|
||||
|
@ -39,6 +39,10 @@ public final class Worker extends MinestomThread {
|
||||
}
|
||||
}
|
||||
|
||||
public void tick() {
|
||||
this.selector.wakeup();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (server.isOpen()) {
|
||||
@ -86,7 +90,7 @@ public final class Worker extends MinestomThread {
|
||||
MinecraftServer.getExceptionManager().handleException(t);
|
||||
connection.disconnect();
|
||||
}
|
||||
}, MinecraftServer.TICK_MS);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
MinecraftServer.getExceptionManager().handleException(e);
|
||||
}
|
||||
@ -118,7 +122,6 @@ public final class Worker extends MinestomThread {
|
||||
socket.setTcpNoDelay(Server.NO_DELAY);
|
||||
socket.setSoTimeout(30 * 1000); // 30 seconds
|
||||
}
|
||||
this.selector.wakeup();
|
||||
}
|
||||
|
||||
public MessagePassingQueue<Runnable> queue() {
|
||||
|
@ -25,9 +25,27 @@ public final class TickSchedulerThread extends MinestomThread {
|
||||
} catch (Exception e) {
|
||||
serverProcess.exception().handleException(e);
|
||||
}
|
||||
final long wait = tickStart + tickNs - System.nanoTime();
|
||||
assert wait <= tickNs : "Wait time is too long: " + (wait / 1e6) + "ms";
|
||||
LockSupport.parkNanos(wait);
|
||||
fixTickRate(tickNs);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final long startTickNs = System.nanoTime();
|
||||
private long tick = 1;
|
||||
|
||||
private void fixTickRate(long tickNs) {
|
||||
long nextTickNs = startTickNs + (tickNs * tick);
|
||||
if (System.nanoTime() < nextTickNs) {
|
||||
while (true) {
|
||||
// Checks in every 1/10 ms to see if the current time has reached the next scheduled time.
|
||||
Thread.yield();
|
||||
LockSupport.parkNanos(100000);
|
||||
long currentNs = System.nanoTime();
|
||||
if (currentNs >= nextTickNs) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
tick++;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user