This repository has been archived by the owner on Oct 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from proepkes/develop
Develop
- Loading branch information
Showing
104 changed files
with
1,432 additions
and
935 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.