Skip to content

Commit

Permalink
allow socket injection
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Mar 21, 2024
1 parent ccaf352 commit 888a8bf
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 35 deletions.
8 changes: 7 additions & 1 deletion src/Backdash/Backends/BackendServices.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Backdash.Core;
using Backdash.Network;
using Backdash.Network.Client;
using Backdash.Network.Protocol;
using Backdash.Serialization;
using Backdash.Sync.Input;
Expand Down Expand Up @@ -30,15 +31,20 @@ public BackendServices(RollbackOptions options, SessionServices<TInput, TGameSta
Random = new DefaultRandomNumberGenerator(services?.Random ?? System.Random.Shared);
DelayStrategy = DelayStrategyFactory.Create(Random, options.Protocol.DelayStrategy);
InputGenerator = services?.InputGenerator;

InputSerializer = services?.InputSerializer ?? BinarySerializerFactory
.FindOrThrow<TInput>(options.NetworkEndianness);

var logWriter = services?.LogWriter is null || options.Log.EnabledLevel is LogLevel.None
? new ConsoleTextLogWriter()
: services.LogWriter;

Logger = new Logger(options.Log, logWriter);
Clock = new Clock();
JobManager = new BackgroundJobManager(Logger);
ProtocolClientFactory = new ProtocolClientFactory(options, Logger);

var socketFactory = services?.PeerSocketFactory ?? new PeerSocketFactory();
ProtocolClientFactory = new ProtocolClientFactory(options, socketFactory, Logger);
}
}

Expand Down
35 changes: 32 additions & 3 deletions src/Backdash/Network/Client/PeerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface IPeerJobClient<in T> : IBackgroundJob, IPeerClient<T> where T : struct

sealed class PeerClient<T> : IPeerJobClient<T> where T : struct
{
readonly UdpSocket socket;
readonly IPeerSocket socket;
readonly IPeerObserver<T> observer;
readonly IBinarySerializer<T> serializer;
readonly Logger logger;
Expand All @@ -41,7 +41,7 @@ sealed class PeerClient<T> : IPeerJobClient<T> where T : struct
public string JobName { get; }

public PeerClient(
UdpSocket socket,
IPeerSocket socket,
IBinarySerializer<T> serializer,
IPeerObserver<T> observer,
Logger logger,
Expand Down Expand Up @@ -120,7 +120,6 @@ async Task ReceiveLoop(CancellationToken ct)
catch (NetcodeDeserializationException ex)
{
logger.Write(LogLevel.Warning, $"UDP Message error: {ex}");
continue;
}
}

Expand Down Expand Up @@ -167,3 +166,33 @@ public void Dispose()
socket.Dispose();
}
}

/// <summary>
/// Create new instances of <see cref="IPeerClient{T}"/>
/// </summary>
public static class PeerClientFactory
{
internal static IPeerClient<T> Create<T>(
IPeerSocket socket,
IBinarySerializer<T> serializer,
IPeerObserver<T> observer, Logger logger,
int maxPacketSize = Max.UdpPacketSize
) where T : struct =>
new PeerClient<T>(socket, serializer, observer, logger, maxPacketSize);

/// <summary>
/// Creates new <see cref="IPeerClient{T}"/>
/// </summary>
public static IPeerClient<T> Create<T>(
IPeerSocket socket,
IPeerObserver<T> observer,
IBinarySerializer<T>? serializer = null,
int maxPacketSize = Max.UdpPacketSize
) where T : struct =>
Create(socket,
serializer ?? BinarySerializerFactory.FindOrThrow<T>(),
observer,
Logger.CreateConsoleLogger(LogLevel.None),
maxPacketSize
);
}
27 changes: 0 additions & 27 deletions src/Backdash/Network/Client/PeerClientFactory.cs

This file was deleted.

45 changes: 45 additions & 0 deletions src/Backdash/Network/Client/PeerSocket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Net;
using System.Net.Sockets;

namespace Backdash.Network.Client;

/// <summary>
/// Socket abstraction over peers
/// </summary>
public interface IPeerSocket : IDisposable
{
/// <inheritdoc cref="Socket.AddressFamily" />
AddressFamily AddressFamily { get; }

/// <summary>
/// Binding port
/// </summary>
int Port { get; }

/// <summary>
/// Receive bytes from specified remote host
/// </summary>
ValueTask<int> ReceiveFromAsync(Memory<byte> buffer, SocketAddress address, CancellationToken cancellationToken);

/// <summary>
/// Sends data to the specified remote host.
/// </summary>
ValueTask<int> SendToAsync(ReadOnlyMemory<byte> buffer, SocketAddress socketAddress,
CancellationToken cancellationToken);
}

/// <summary>
/// Factory for peer sockets
/// </summary>
public interface IPeerSocketFactory
{
/// <summary>
/// Creates instance of <see cref="IPeerSocket"/>
/// </summary>
IPeerSocket Create(int port, RollbackOptions options);
}

sealed class PeerSocketFactory : IPeerSocketFactory
{
public IPeerSocket Create(int port, RollbackOptions options) => new UdpSocket(port, options.UseIPv6);
}
2 changes: 1 addition & 1 deletion src/Backdash/Network/Client/UdpSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Backdash.Network.Client;
/// <summary>
/// UDP specialized socket interface.
/// </summary>
public sealed class UdpSocket : IDisposable
public sealed class UdpSocket : IPeerSocket
{
// ReSharper disable InconsistentNaming
const uint IOC_IN = 0x80000000;
Expand Down
5 changes: 2 additions & 3 deletions src/Backdash/Network/Protocol/ProtocolClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ interface IProtocolClientFactory

sealed class ProtocolClientFactory(
RollbackOptions options,
IPeerSocketFactory socketFactory,
Logger logger
) : IProtocolClientFactory
{
public IProtocolClient CreateProtocolClient(int port, IPeerObserver<ProtocolMessage> observer)
{
UdpSocket socket = new UdpSocket(port, options.UseIPv6);

PeerClient<ProtocolMessage> peerClient = new(
socket,
socketFactory.Create(port, options),
new ProtocolMessageBinarySerializer(options.NetworkEndianness),
observer,
logger,
Expand Down
6 changes: 6 additions & 0 deletions src/Backdash/SessionServices.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Backdash.Core;
using Backdash.Network.Client;
using Backdash.Serialization;
using Backdash.Sync.Input;
using Backdash.Sync.State;
Expand Down Expand Up @@ -46,6 +47,11 @@ public sealed class SessionServices<TInput, TGameState>
/// </summary>
public IStateStore<TGameState>? StateStore { get; set; }

/// <summary>
/// State store service for session.
/// </summary>
public IPeerSocketFactory? PeerSocketFactory { get; set; }

/// <summary>
/// Default random service
/// </summary>
Expand Down

0 comments on commit 888a8bf

Please sign in to comment.