Skip to content

Commit

Permalink
Server ping
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Dec 14, 2024
1 parent efceb75 commit 8a79e90
Show file tree
Hide file tree
Showing 33 changed files with 587 additions and 496 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/Moonlight/bin/Debug/net9.0/linux-x64/Moonlight.dll",
"args": [],
"cwd": "${workspaceFolder}/src/Moonlight",
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
},
Expand Down
Binary file added res/server-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Moonlight.Api.Events.EventArgs
public sealed class HandshakeAsyncServerEventArgs : AsyncServerEventArgs
{
public required HandshakePacket HandshakePacket { get; init; }
public required PacketHandler PacketReader { get; init; }
public required PacketHandler PacketHandler { get; init; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Moonlight.Api.Events.EventArgs
{
public sealed class PacketReceivedAsyncServerEventArgs : AsyncServerEventArgs
{
public required PacketHandler Reader { get; init; }
public required PacketHandler PacketHandler { get; init; }
public required IPacket Packet { get; init; }
}
}
85 changes: 83 additions & 2 deletions src/Moonlight.Api/Net/PacketHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.IO.Pipelines;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
Expand All @@ -11,24 +12,31 @@

namespace Moonlight.Api.Net
{
public sealed class PacketHandler : IDisposable
public sealed partial class PacketHandler : IDisposable
{
private readonly PacketHandlerFactory _factory;
private readonly Stream _stream;
private readonly ILogger<PacketHandler> _logger;
private readonly PipeReader _pipeReader;
private readonly PipeWriter _pipeWriter;
private object? _disposed;

public PacketHandler(PacketHandlerFactory factory, Stream stream, ILogger<PacketHandler> logger)
{
_factory = factory;
_stream = stream;
_pipeReader = PipeReader.Create(_stream);
_pipeWriter = PipeWriter.Create(_stream);
_logger = logger;
}

public async ValueTask<ReadOnlySequence<byte>?> TryReadSequenceAsync(CancellationToken cancellationToken = default)
{
if (_disposed is not null)
{
return new ReadOnlySequence<byte>();
}

ReadResult readResult = await _pipeReader.ReadAsync(cancellationToken);
if (readResult.IsCanceled || cancellationToken.IsCancellationRequested)
{
Expand Down Expand Up @@ -70,12 +78,16 @@ public async ValueTask<T> ReadPacketAsync<T>(CancellationToken cancellationToken
return packet;
}

public async ValueTask<IPacket> ReadPacketAsync(CancellationToken cancellationToken = default)
public async ValueTask<IPacket?> ReadPacketAsync(CancellationToken cancellationToken = default)
{
if (await TryReadSequenceAsync(cancellationToken) is not ReadOnlySequence<byte> sequence)
{
throw new OperationCanceledException();
}
else if (sequence.Length == 0)
{
return null;
}

IPacket? packet = ReadPacket(sequence, out SequencePosition position);
if (packet is null)
Expand Down Expand Up @@ -137,6 +149,74 @@ public async ValueTask<IPacket> ReadPacketAsync(CancellationToken cancellationTo
return packet;
}

public int WritePacket<T>(T packet) where T : IPacket<T>
{
VarInt length = T.Id.Length + T.CalculateSize(packet);
Span<byte> buffer = _pipeWriter.GetSpan(length.Length + length.Value);

// Write the total packet length
int position = VarInt.Serialize(length, buffer);

// Write the packet ID
position += VarInt.Serialize(T.Id, buffer[position..]);

// Write the packet data
position += T.Serialize(packet, buffer[position..]);

// Send the packet
if (_logger.IsEnabled(LogLevel.Trace))
{
StringBuilder stringBuilder = new();
foreach (byte b in buffer[..position])
{
//stringBuilder.Append("0x");
stringBuilder.Append(b);
stringBuilder.Append(", ");
}

stringBuilder.Length -= 2;
_logger.LogTrace("Sent {PacketType} packet ({BufferSize}): [{Packet}]", packet.GetType(), position, stringBuilder.ToString());
}

_pipeWriter.Advance(position);
return position;
}

public int WritePacket(IPacket packet)
{
VarInt length = _factory.PreparedPacketSizeCalculators[packet.Id](packet);
Span<byte> buffer = _pipeWriter.GetSpan(length.Length + length.Value);

// Write the total packet length
int position = VarInt.Serialize(length, buffer);

// Write the packet ID
position += VarInt.Serialize(packet.Id, buffer[position..]);

// Write the packet data
position += _factory.PreparedPacketSerializers[packet.Id](packet, buffer[position..]);

// Send the packet
if (_logger.IsEnabled(LogLevel.Trace))
{
StringBuilder stringBuilder = new();
foreach (byte b in buffer[..position])
{
//stringBuilder.Append("0x");
stringBuilder.Append(b);
stringBuilder.Append(", ");
}

stringBuilder.Length -= 2;
_logger.LogTrace("Sent {PacketType} packet ({BufferSize}): [{Packet}]", packet.GetType(), position, stringBuilder.ToString());
}

_pipeWriter.Advance(position);
return position;
}

public async ValueTask FlushAsync(CancellationToken cancellationToken = default) => await _pipeWriter.FlushAsync(cancellationToken);

public void Dispose()
{
if (_disposed is not null)
Expand All @@ -146,6 +226,7 @@ public void Dispose()

_disposed = new object();
_pipeReader.Complete();
_pipeWriter.Complete();
_stream.Dispose();
GC.SuppressFinalize(this);
}
Expand Down
105 changes: 105 additions & 0 deletions src/Moonlight.Api/Net/PacketHandlerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Buffers;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging;
using Moonlight.Protocol.Net;
using Moonlight.Protocol.VariableTypes;

namespace Moonlight.Api.Net
{
public delegate IPacket DeserializerDelegate(ref SequenceReader<byte> reader);
public delegate int CalculateSizeDelegate(IPacket packet);
public delegate int SerializerDelegate(IPacket packet, Span<byte> target);

public sealed class PacketHandlerFactory
{
public Dictionary<int, DeserializerDelegate> PacketDeserializers { get; init; } = [];
public Dictionary<int, CalculateSizeDelegate> PacketSizeCalculators { get; init; } = [];
public Dictionary<int, SerializerDelegate> PacketSerializers { get; init; } = [];

public FrozenDictionary<int, DeserializerDelegate> PreparedPacketDeserializers { get; private set; } = FrozenDictionary<int, DeserializerDelegate>.Empty;
public FrozenDictionary<int, CalculateSizeDelegate> PreparedPacketSizeCalculators { get; private set; } = FrozenDictionary<int, CalculateSizeDelegate>.Empty;
public FrozenDictionary<int, SerializerDelegate> PreparedPacketSerializers { get; private set; } = FrozenDictionary<int, SerializerDelegate>.Empty;

private readonly ILoggerFactory _loggerFactory;

public PacketHandlerFactory(ILoggerFactory loggerFactory) => _loggerFactory = loggerFactory;

public PacketHandler Create(Stream stream) => new(this, stream, _loggerFactory.CreateLogger<PacketHandler>());

public void Prepare()
{
PreparedPacketDeserializers = PacketDeserializers.ToFrozenDictionary();
PreparedPacketSizeCalculators = PacketSizeCalculators.ToFrozenDictionary();
PreparedPacketSerializers = PacketSerializers.ToFrozenDictionary();
}


public void RegisterPacket<T>() where T : IPacket<T>
{
PacketDeserializers[T.Id] = (ref SequenceReader<byte> reader) => T.Deserialize(ref reader);
PacketSizeCalculators[T.Id] = (IPacket packet) => T.CalculateSize((T)packet);
PacketSerializers[T.Id] = (IPacket packet, Span<byte> target) => T.Serialize((T)packet, target);
}

public void RegisterPacket<T>(VarInt id) where T : IPacket<T>
{
PacketDeserializers[id] = (ref SequenceReader<byte> reader) => T.Deserialize(ref reader);
PacketSizeCalculators[id] = (IPacket packet) => T.CalculateSize((T)packet);
PacketSerializers[id] = (IPacket packet, Span<byte> target) => T.Serialize((T)packet, target);
}

public void RegisterPacket(Type type)
{
if (!type.GetInterfaces().Any(inter => inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IPacket<>)))
{
throw new ArgumentException("Type must implement IPacket<T>.");
}
else if (type.GetProperty("Id")?.GetValue(null) is not VarInt id)
{
throw new ArgumentException("Type must have a static Id property.");
}
else
{
RegisterPacket(id, type);
}
}

public void RegisterPacket(VarInt id, Type type)
{
if (!type.GetInterfaces().Any(inter => inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IPacket<>)))
{
throw new ArgumentException("Type must implement IPacket<T>.");
}

typeof(PacketHandlerFactory).GetMethod(nameof(RegisterPacket), [typeof(VarInt)])!.MakeGenericMethod(type).Invoke(this, [id]);
}

public void UnregisterPacket<T>() where T : IPacket<T> => UnregisterPacket(T.Id);
public void UnregisterPacket(Type type)
{
if (!type.GetInterfaces().Any(inter => inter.IsGenericType && inter.GetGenericTypeDefinition() == typeof(IPacket<>)))
{
throw new ArgumentException("Type must implement IPacket<T>.");
}
else if (type.GetProperty("Id")?.GetValue(null) is not VarInt id)
{
throw new ArgumentException("Type must have a static Id property.");
}
else
{
UnregisterPacket(id);
}
}

public void UnregisterPacket(int id)
{
PacketDeserializers.Remove(id);
PacketSizeCalculators.Remove(id);
PacketSerializers.Remove(id);
}
}
}
Loading

0 comments on commit 8a79e90

Please sign in to comment.