Skip to content

Commit

Permalink
Fixed #10329 by timing out when UDP snapshots are not received
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuken committed Jan 14, 2025
1 parent 4b92364 commit 3acac7d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions core/assets/bundles/bundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ disconnect.error = Connection error.
disconnect.closed = Connection closed.
disconnect.timeout = Timed out.
disconnect.data = Failed to load world data!
disconnect.snapshottimeout = Timed out while receiving UDP snapshots.\nThis may be caused by an unstable network or connection.
cantconnect = Unable to join game ([accent]{0}[]).
connecting = [accent]Connecting...
reconnecting = [accent]Reconnecting...
Expand Down
5 changes: 5 additions & 0 deletions core/src/mindustry/ai/types/MissileAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
public class MissileAI extends AIController{
public @Nullable Unit shooter;

@Override
protected void resetTimers(){
timer.reset(timerTarget, Mathf.random(3f));
}

@Override
public void updateMovement(){
unloadPayloads();
Expand Down
4 changes: 2 additions & 2 deletions core/src/mindustry/content/Blocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -4127,7 +4127,7 @@ Items.oxide, new BasicBulletType(8f, 120){{
hitEffect = despawnEffect = Fx.hitSquaresColor;
buildingDamageMultiplier = 0.2f;
}},
Items.silicon, new BasicBulletType(8f, 34){{
Items.silicon, new BasicBulletType(8f, 35){{
knockback = 3f;
width = 25f;
hitSize = 7f;
Expand Down Expand Up @@ -4980,7 +4980,7 @@ Items.surgeAlloy, new BulletType(0f, 0f){{
fragLifeMin = 0.1f;
fragBullets = 5;
fragRandomSpread = 0f;
fragSpread = 37f;
fragSpread = 30f;
fragBullet = new BulletType(){{
shootEffect = Fx.shootBig;
smokeEffect = Fx.shootSmokeMissileColor;
Expand Down
19 changes: 18 additions & 1 deletion core/src/mindustry/core/NetClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static mindustry.Vars.*;

public class NetClient implements ApplicationListener{
private static final long entitySnapshotTimeout = 1000 * 20;
private static final float dataTimeout = 60 * 30;
/** ticks between syncs, e.g. 5 means 60/5 = 12 syncs/sec*/
private static final float playerSyncTime = 4;
Expand All @@ -50,6 +51,8 @@ public class NetClient implements ApplicationListener{
private boolean quietReset = false;
/** Counter for data timeout. */
private float timeoutTime = 0f;
/** Timestamp for last UDP state snapshot received. */
private long lastSnapshotTimestamp;
/** Last sent client snapshot ID. */
private int lastSent;

Expand Down Expand Up @@ -478,6 +481,7 @@ public static void readSyncEntity(DataInputStream input, Reads read) throws IOEx
@Remote(variants = Variant.one, priority = PacketPriority.low, unreliable = true)
public static void entitySnapshot(short amount, byte[] data){
try{
netClient.lastSnapshotTimestamp = Time.millis();
netClient.byteStream.setBytes(data);
DataInputStream input = netClient.dataStream;

Expand Down Expand Up @@ -575,7 +579,18 @@ public void update(){
if(!net.client()) return;

if(state.isGame()){
if(!connecting) sync();
if(!connecting){
sync();

//timeout if UDP snapshot packets are not received for a while
if(lastSnapshotTimestamp > 0 && Time.timeSinceMillis(lastSnapshotTimestamp) > entitySnapshotTimeout){
Log.err("Timed out after not received UDP snapshots.");
quiet = true;
ui.showErrorMessage("@disconnect.snapshottimeout");
net.disconnect();
lastSnapshotTimestamp = 0;
}
}
}else if(!connecting){
net.disconnect();
}else{ //...must be connecting
Expand Down Expand Up @@ -612,6 +627,7 @@ private void finishConnecting(){
Core.app.post(Call::connectConfirm);
Time.runTask(40f, platform::updateRPC);
Core.app.post(ui.loadfrag::hide);
lastSnapshotTimestamp = Time.millis();
}

private void reset(){
Expand All @@ -622,6 +638,7 @@ private void reset(){
quietReset = false;
quiet = false;
lastSent = 0;
lastSnapshotTimestamp = 0;

Groups.clear();
ui.chatfrag.clearMessages();
Expand Down
2 changes: 1 addition & 1 deletion core/src/mindustry/core/NetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ void sync(){
try{
writeEntitySnapshot(player);
}catch(IOException e){
e.printStackTrace();
Log.err(e);
}
});

Expand Down
8 changes: 6 additions & 2 deletions core/src/mindustry/entities/units/AIController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ public class AIController implements UnitController{
protected Teamc target;

{
timer.reset(0, Mathf.random(40f));
timer.reset(1, Mathf.random(60f));
resetTimers();
}

protected void resetTimers(){
timer.reset(timerTarget, Mathf.random(40f));
timer.reset(timerTarget2, Mathf.random(60f));
}

@Override
Expand Down

0 comments on commit 3acac7d

Please sign in to comment.