Skip to content

Commit

Permalink
Rewrite DiscordIpDiscoveryPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
OoLunar committed Jan 10, 2024
1 parent 6d0d61b commit db5e67c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 33 deletions.
51 changes: 51 additions & 0 deletions src/DSharpPlus.VoiceLink/DiscordIpDiscoveryPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Buffers.Binary;
using System.Text;

namespace DSharpPlus.VoiceLink
{
public readonly struct DiscordIpDiscoveryPacket
{
public ushort Type { get; init; }
public ushort Length { get; init; }
public uint Ssrc { get; init; }
public string Address { get; init; }
public ushort Port { get; init; }

public DiscordIpDiscoveryPacket(byte[] data)
{
Span<byte> dataSpan = data.AsSpan();
Type = BinaryPrimitives.ReadUInt16BigEndian(dataSpan[0..2]);
Length = BinaryPrimitives.ReadUInt16BigEndian(dataSpan[2..4]);
Ssrc = BinaryPrimitives.ReadUInt32BigEndian(dataSpan[4..8]);
Address = Encoding.UTF8.GetString(dataSpan[8..72].TrimEnd((byte)'\0'));
Port = BinaryPrimitives.ReadUInt16BigEndian(dataSpan[72..74]);
}

public DiscordIpDiscoveryPacket(ushort type, ushort length, uint ssrc, string address, ushort port)
{
Type = type;
Length = length;
Ssrc = ssrc;
Address = address;
Port = port;
}

public static implicit operator DiscordIpDiscoveryPacket(byte[] ipDiscoveryData) => new(ipDiscoveryData);
public static implicit operator byte[](DiscordIpDiscoveryPacket ipDiscovery)
{
byte[] data = new byte[74];

Span<byte> dataSpan = data.AsSpan();
BinaryPrimitives.WriteUInt16BigEndian(dataSpan[0..2], ipDiscovery.Type);
BinaryPrimitives.WriteUInt16BigEndian(dataSpan[2..4], ipDiscovery.Length);
BinaryPrimitives.WriteUInt32BigEndian(dataSpan[4..8], ipDiscovery.Ssrc);
Encoding.UTF8.TryGetBytes(ipDiscovery.Address, dataSpan[8..72], out _);
BinaryPrimitives.WriteUInt16BigEndian(dataSpan[72..74], ipDiscovery.Port);

return data;
}

public override string ToString() => $"Type: {Type}, Length: {Length}, Ssrc: {Ssrc}, Address: {Address}, Port: {Port}";
}
}
33 changes: 0 additions & 33 deletions src/DSharpPlus.VoiceLink/IpDiscoveryPacket.cs

This file was deleted.

0 comments on commit db5e67c

Please sign in to comment.