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

Commit

Permalink
Merge pull request #21 from proepkes/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
proepkes authored Jan 27, 2019
2 parents 3e1a018 + f15426b commit 994423c
Show file tree
Hide file tree
Showing 104 changed files with 1,432 additions and 935 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,6 @@ Unity/Assembly-CSharp-Editor\.csproj
/Unity/Assets/Integration/FixMath.NET.pdb.meta
/Unity/Assets/Integration/ECS.pdb.meta
/Unity/Assets/Integration/BEPUutilities.pdb.meta
/Unity/Assets/Editor Default Resources
/Unity/Assets/Editor Default Resources.meta
/Unity/Assets/Debug.meta
4 changes: 4 additions & 0 deletions Engine/Client/Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\Network\Network.csproj" />
</ItemGroup>

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="xcopy /d /y &quot;$(TargetDir)Lockstep.*.dll&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;&#xD;&#xA;xcopy /d /y &quot;$(TargetDir)Lockstep.*.pdb&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;&#xD;&#xA;xcopy /d /y &quot;$(TargetDir)Lockstep.*.json&quot; &quot;$(SolutionDir)..\Unity\Assets\Integration\&quot;" />
</Target>

</Project>
60 changes: 60 additions & 0 deletions Engine/Client/CommandBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Lockstep.Core.Data;
using Lockstep.Core.Interfaces;

namespace Lockstep.Client.Implementations
{
public class CommandBuffer : ICommandBuffer
{
private readonly Dictionary<long, List<ICommand>> _commands = new Dictionary<long, List<ICommand>>();

public event Action<long, ICommand> Inserted;

public long Count
{
get
{
lock (_commands)
{
return _commands.LongCount();
}
}
}

public long ItemIndex { get; private set; }

public long Remaining => Count - ItemIndex;

public virtual void Insert(long frameNumber, ICommand command)
{
lock (_commands)
{
if (!_commands.ContainsKey(frameNumber))
{
_commands.Add(frameNumber, new List<ICommand>(10));
}

_commands[frameNumber].Add(command);

Inserted?.Invoke(frameNumber, command);
}
}

public ICommand[] GetNext()
{
lock (_commands)
{
//If no commands were inserted then return an empty list
if (!_commands.ContainsKey(ItemIndex))
{
_commands[ItemIndex] = new List<ICommand>();
}

return _commands[ItemIndex++].ToArray();

}
}
}
}
33 changes: 0 additions & 33 deletions Engine/Client/FrameBuffer.cs

This file was deleted.

27 changes: 0 additions & 27 deletions Engine/Client/Implementations/LocalDataReceiver.cs

This file was deleted.

90 changes: 0 additions & 90 deletions Engine/Client/Implementations/NetworkedDataReceiver.cs

This file was deleted.

13 changes: 0 additions & 13 deletions Engine/Client/Interfaces/IDataReceiver.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Engine/Client/Interfaces/INetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public interface INetwork
/// <summary>
/// Send data reliable ordered
/// </summary>
void Send(byte[] data, int length);
void Send(byte[] data);
}
}
81 changes: 81 additions & 0 deletions Engine/Client/NetworkCommandBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections.Generic;
using System.IO;
using Lockstep.Client.Implementations;
using Lockstep.Client.Interfaces;
using Lockstep.Core.Data;
using Lockstep.Network;
using Lockstep.Network.Messages;
using Lockstep.Network.Utils;

namespace Lockstep.Client
{
public class NetworkCommandBuffer : CommandBuffer
{
public event Action<Init> InitReceived;

private readonly INetwork _network;
private readonly IDictionary<ushort, Func<ISerializableCommand>> _commandFactories = new Dictionary<ushort, Func<ISerializableCommand>>();

public NetworkCommandBuffer(INetwork network)
{
_network = network;
_network.DataReceived += OnDataReceived;
}

public void RegisterCommand(Func<ISerializableCommand> commandFactory)
{
var tag = commandFactory.Invoke().Tag;
if (_commandFactories.ContainsKey(tag))
{
throw new InvalidDataException("The command tag " + tag + " is already registered. Every command tag must be unique.");
}
_commandFactories.Add(tag, commandFactory);
}

public override void Insert(long frameNumber, ICommand command)
{
if (command is ISerializableCommand serializable)
{
//Tell the server
var writer = new Serializer();
writer.Put((byte)MessageTag.Input);
writer.Put(frameNumber);
writer.Put(serializable.Tag);
serializable.Serialize(writer);

_network.Send(Compressor.Compress(writer));
}
}

private void OnDataReceived(byte[] data)
{
data = Compressor.Decompress(data);

var reader = new Deserializer(data);
var messageTag = (MessageTag)reader.GetByte();
switch (messageTag)
{
case MessageTag.StartSimulation:
var init = new Init();
init.Deserialize(reader);
InitReceived?.Invoke(init);
break;
case MessageTag.Input:
var frameNumber = reader.GetLong();
var tag = reader.GetUShort();

if (_commandFactories.ContainsKey(tag))
{
var newCommand = _commandFactories[tag].Invoke();
newCommand.Deserialize(reader);


base.Insert(frameNumber, newCommand);
}

break;
}
}
}
}
Loading

0 comments on commit 994423c

Please sign in to comment.