Skip to content
This repository was archived by the owner on Oct 22, 2023. It is now read-only.

Commit b0ad5b2

Browse files
committed
Send input on every frame, even without commands
Serverside packet inspection => todo: analyze input on server and speed up slow clients/slow down fast clients
1 parent cd75f20 commit b0ad5b2

31 files changed

Lines changed: 55 additions & 40 deletions

Engine/Common/Logging/Log.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Lockstep.Common.Logging
44
{
55
public static class Log
66
{
7-
public static LogSeverity LogSeverityLevel = 0;
7+
public static LogSeverity LogSeverityLevel = LogSeverity.Info | LogSeverity.Warn | LogSeverity.Error | LogSeverity.Exception;
88

99
public static event EventHandler<LogEventArgs> OnMessage;
1010

Engine/CopySourceToUnity.cmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
del ..\Unity\Assets\Integration\Lockstep.*
2+
xcopy "Game" "..\Unity\Assets\GitIgnored\Game\" /exclude:excludeList.txt /y /S
3+
xcopy "Common" "..\Unity\Assets\GitIgnored\Common\" /exclude:excludeList.txt /y /S
4+
xcopy "Core.Logic" "..\Unity\Assets\GitIgnored\Core.Logic\" /exclude:excludeList.txt /y /S
5+
xcopy "Core.State" "..\Unity\Assets\GitIgnored\Core.State\" /exclude:excludeList.txt /y /S
6+
xcopy "Network" "..\Unity\Assets\GitIgnored\Network\" /exclude:excludeList.txt /y /S
7+
xcopy "Network.Client" "..\Unity\Assets\GitIgnored\Network.Client\" /exclude:excludeList.txt /y /S

Engine/Core.Logic/Systems/WorldSystems.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public WorldSystems(Contexts contexts, params Feature[] features)
2121

2222
Add(new CalculateHashCode(contexts));
2323

24-
Add(new VerifyNoDuplicateBackups(contexts));
24+
//Performance-hit, only use for serious debugging
25+
//Add(new VerifyNoDuplicateBackups(contexts));
2526

2627
Add(new DestroyDestroyedGameSystem(contexts));
2728

Engine/Game.UrhoSharp.Desktop/Scenes/StaticScene.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private void CreateWorld()
8080
mushroomObject.SetMaterial(ResourceCache.GetMaterial("Materials/Mushroom.xml"));
8181
}
8282

83-
simulation.Start(init.TargetFPS, init.ActorID, init.AllActors);
83+
simulation.Start(init.SimulationSpeed, init.ActorID, init.AllActors);
8484
};
8585

8686
simulation = new Simulation(Contexts.sharedInstance, networkCommandQueue, new ViewService(ResourceCache, scene.GetChild("Jacks")));

Engine/Game/Services/DefaultViewService.cs renamed to Engine/Game/DefaultServices/DefaultViewService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Lockstep.Game.Interfaces;
22

3-
namespace Lockstep.Game.Services
3+
namespace Lockstep.Game.DefaultServices
44
{
55
public class DefaultViewService : IViewService
66
{

Engine/Game/Features/Input/InputFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public InputFeature(Contexts contexts, ServiceContainer services) : base("Input"
77
//TODO: Add InputValidationSystem
88
Add(new ExecuteSpawnInput(contexts, services));
99

10-
//Massive performance-hit:
10+
//Performance-hit, only use for serious debugging
1111
//Add(new VerifySelectionIdExists(contexts));
1212

1313
Add(new ExecuteNavigationInput(contexts, services));

Engine/Game/Simulation.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ public void Start(int targetFps, byte localActorId, byte[] allActors)
6363
Running = true;
6464

6565
Started?.Invoke(this, EventArgs.Empty);
66-
}
67-
66+
}
6867

6968
public void Update(float elapsedMilliseconds)
7069
{
@@ -78,12 +77,9 @@ public void Update(float elapsedMilliseconds)
7877
while (_accumulatedTime >= _tickDt)
7978
{
8079
lock (_localCommandBuffer)
81-
{
82-
//if (_localCommandBuffer.Any())
83-
{
84-
_commandQueue.Enqueue(new Input(_world.Tick, LocalActorId, _localCommandBuffer.ToArray()));
85-
_localCommandBuffer.Clear();
86-
}
80+
{
81+
_commandQueue.Enqueue(new Input(_world.Tick, LocalActorId, _localCommandBuffer.ToArray()));
82+
_localCommandBuffer.Clear();
8783

8884
ProcessInputQueue();
8985

@@ -105,8 +101,7 @@ public void Execute(ICommand command)
105101
{
106102
_localCommandBuffer.Add(command);
107103
}
108-
}
109-
104+
}
110105

111106
public void DumpGameLog(Stream outputStream, bool closeStream = true)
112107
{

Engine/Network.Client/NetworkCommandQueue.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public override void Enqueue(Input input)
4646
//Tell the server
4747
var writer = new Serializer();
4848
writer.Put((byte)MessageTag.Input);
49-
writer.Put(input.ActorId);
5049
writer.Put(input.Tick + LagCompensation);
5150
writer.Put(input.Commands.Count());
51+
writer.Put(input.ActorId);
5252
foreach (var command in input.Commands)
5353
{
5454
writer.Put(command.Tag);
@@ -73,9 +73,9 @@ private void NetworkOnDataReceived(byte[] rawData)
7373
InitReceived?.Invoke(this, paket);
7474
break;
7575
case MessageTag.Input:
76-
var actorId = reader.GetByte();
7776
var tick = reader.GetUInt();
7877
var countCommands = reader.GetInt();
78+
var actorId = reader.GetByte();
7979
var commands = new ICommand[countCommands];
8080
for (var i = 0; i < countCommands; i++)
8181
{

Engine/Network.Server/Room.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ namespace Lockstep.Network.Server
1313
/// </summary>
1414
public class Room
1515
{
16-
private const int TargetFps = 20;
16+
/// <summary>
17+
/// Server determines the default speed of the simulation measured in FPS
18+
/// </summary>
19+
private const int SimulationSpeed = 20;
1720

1821
private byte _nextPlayerId;
1922
private readonly int _size;
@@ -70,7 +73,12 @@ private void OnDataReceived(int clientId, byte[] data)
7073
switch (messageTag)
7174
{
7275
case MessageTag.Input:
73-
_server.Distribute(clientId, data);
76+
var clientTick = reader.GetUInt();
77+
var commandsCount = reader.GetInt();
78+
if (commandsCount > 0)
79+
{
80+
_server.Distribute(clientId, data);
81+
}
7482
break;
7583

7684
case MessageTag.HashCode:
@@ -111,7 +119,7 @@ private void StartSimulationOnConnectedPeers()
111119
var writer = new Serializer();
112120

113121
//Create a new seed and send it with a start-message to all clients
114-
//The message also contains the respective player-id and the servers' frame rate
122+
//The message also contains the respective player-id and the initial simulation speed
115123
var seed = new Random().Next(int.MinValue, int.MaxValue);
116124

117125
foreach (var player in _playerIds)
@@ -123,7 +131,7 @@ private void StartSimulationOnConnectedPeers()
123131
Seed = seed,
124132
ActorID = player.Value,
125133
AllActors = _playerIds.Values.ToArray(),
126-
TargetFPS = TargetFps
134+
SimulationSpeed = SimulationSpeed
127135
}.Serialize(writer);
128136

129137
_server.Send(player.Key, Compressor.Compress(writer));

Engine/Network/Messages/Init.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ public class Init : ISerializable
1111

1212
public byte[] AllActors { get; set; }
1313

14-
public int TargetFPS { get; set; }
14+
public int SimulationSpeed { get; set; }
1515

1616
public void Serialize(Serializer writer)
1717
{
1818
writer.Put(Seed);
1919
writer.Put(ActorID);
2020
writer.PutBytesWithLength(AllActors);
21-
writer.Put(TargetFPS);
21+
writer.Put(SimulationSpeed);
2222
}
2323

2424
public void Deserialize(Deserializer reader)
2525
{
2626
Seed = reader.GetInt();
2727
ActorID = reader.GetByte();
2828
AllActors = reader.GetBytesWithLength();
29-
TargetFPS = reader.GetInt();
29+
SimulationSpeed = reader.GetInt();
3030
}
3131
}
3232
}

0 commit comments

Comments
 (0)