Skip to content

Commit 5d8370c

Browse files
authored
Add SendDatagram and SendDatagramAsync to VoiceClient (#373)
* payload -> message for websocket * Add SendDatagram and SendDatagramAsync to VoiceClient * Normalize naming
1 parent 0717df4 commit 5d8370c

22 files changed

Lines changed: 201 additions & 171 deletions

Hosting/NetCord.Hosting/Gateway/GatewayClientOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ internal partial class Validator : IValidateOptions<GatewayClientOptions>
3131
/// <inheritdoc cref="GatewayClientConfiguration.RateLimiterProvider" />
3232
public IRateLimiterProvider? RateLimiterProvider { get; set; }
3333

34-
/// <inheritdoc cref="GatewayClientConfiguration.DefaultPayloadProperties" />
35-
public WebSocketPayloadProperties? DefaultPayloadProperties { get; set; }
34+
/// <inheritdoc cref="GatewayClientConfiguration.DefaultMessageProperties" />
35+
public WebSocketMessageProperties? DefaultMessageProperties { get; set; }
3636

3737
/// <inheritdoc cref="GatewayClientConfiguration.ReconnectStrategy" />
3838
public IReconnectStrategy? ReconnectStrategy { get; set; }
@@ -74,7 +74,7 @@ internal GatewayClientConfiguration CreateConfiguration(IServiceProvider service
7474
{
7575
return GatewayClientConfigurationFactory.Create(WebSocketConnectionProvider,
7676
RateLimiterProvider,
77-
DefaultPayloadProperties,
77+
DefaultMessageProperties,
7878
ReconnectStrategy,
7979
LatencyTimer,
8080
Version,

Hosting/NetCord.Hosting/Gateway/ShardedGatewayClientOptions.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ internal partial class Validator : IValidateOptions<ShardedGatewayClientOptions>
3434
/// <inheritdoc cref="ShardedGatewayClientConfiguration.RateLimiterProviderFactory" />
3535
public Func<Shard, IRateLimiterProvider?>? RateLimiterProviderFactory { get; set; }
3636

37-
/// <inheritdoc cref="ShardedGatewayClientConfiguration.DefaultPayloadPropertiesFactory" />
38-
public Func<Shard, WebSocketPayloadProperties?>? DefaultPayloadPropertiesFactory { get; set; }
37+
/// <inheritdoc cref="ShardedGatewayClientConfiguration.DefaultMessagePropertiesFactory" />
38+
public Func<Shard, WebSocketMessageProperties?>? DefaultMessagePropertiesFactory { get; set; }
3939

4040
/// <inheritdoc cref="ShardedGatewayClientConfiguration.ReconnectStrategyFactory" />
4141
public Func<Shard, IReconnectStrategy?>? ReconnectStrategyFactory { get; set; }
@@ -87,8 +87,8 @@ internal partial class Validator : IValidateOptions<ShardedGatewayClientOptions>
8787
/// <inheritdoc cref="ShardedGatewayClientConfiguration.RateLimiterProviderFactory" />
8888
public IRateLimiterProvider? RateLimiterProvider { get; set; }
8989

90-
/// <inheritdoc cref="ShardedGatewayClientConfiguration.DefaultPayloadPropertiesFactory" />
91-
public WebSocketPayloadProperties? DefaultPayloadProperties { get; set; }
90+
/// <inheritdoc cref="ShardedGatewayClientConfiguration.DefaultMessagePropertiesFactory" />
91+
public WebSocketMessageProperties? DefaultMessageProperties { get; set; }
9292

9393
/// <inheritdoc cref="ShardedGatewayClientConfiguration.ReconnectStrategyFactory" />
9494
public IReconnectStrategy? ReconnectStrategy { get; set; }
@@ -121,7 +121,7 @@ internal ShardedGatewayClientConfiguration CreateConfiguration(IServiceProvider
121121
{
122122
return ShardedGatewayClientConfigurationFactory.Create(CreateFactory(WebSocketConnectionProvider, WebSocketConnectionProviderFactory),
123123
CreateFactory(RateLimiterProvider, RateLimiterProviderFactory),
124-
CreateFactory(DefaultPayloadProperties, DefaultPayloadPropertiesFactory),
124+
CreateFactory(DefaultMessageProperties, DefaultMessagePropertiesFactory),
125125
CreateFactory(ReconnectStrategy, ReconnectStrategyFactory),
126126
CreateFactory(LatencyTimer, LatencyTimerFactory),
127127
CreateFactory(Version, VersionFactory),

NetCord/Gateway/GatewayClient.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public sealed partial class GatewayClient : WebSocketClient, IEntity
382382
public partial event Func<GuildUser, ValueTask>? GuildUserUpdate;
383383

384384
/// <summary>
385-
/// Sent in response to <see cref="RequestGuildUsersAsync(GuildUsersRequestProperties, WebSocketPayloadProperties, CancellationToken)"/>. You can use the <see cref="GuildUserChunkEventArgs.ChunkIndex"/> and <see cref="GuildUserChunkEventArgs.ChunkCount"/> to calculate how many chunks are left for your request.<br/>
385+
/// Sent in response to <see cref="RequestGuildUsersAsync(GuildUsersRequestProperties, WebSocketMessageProperties, CancellationToken)"/>. You can use the <see cref="GuildUserChunkEventArgs.ChunkIndex"/> and <see cref="GuildUserChunkEventArgs.ChunkCount"/> to calculate how many chunks are left for your request.<br/>
386386
/// </summary>
387387
/// <remarks>
388388
/// <br/> Required Intents: None
@@ -896,7 +896,7 @@ private protected override void OnConnected()
896896

897897
private ValueTask SendIdentifyAsync(ConnectionState connectionState, PresenceProperties? presence = null, CancellationToken cancellationToken = default)
898898
{
899-
GatewayPayloadProperties<GatewayIdentifyProperties> payload = new(GatewayOpcode.Identify, new(Token.RawToken)
899+
GatewayMessageProperties<GatewayIdentifyProperties> message = new(GatewayOpcode.Identify, new(Token.RawToken)
900900
{
901901
ConnectionProperties = _connectionProperties,
902902
LargeThreshold = _largeThreshold,
@@ -907,7 +907,7 @@ private ValueTask SendIdentifyAsync(ConnectionState connectionState, PresencePro
907907

908908
_latencyTimer.Start();
909909

910-
return SendConnectionObjectAsync(connectionState, payload, Serialization.Default.GatewayPayloadPropertiesGatewayIdentifyProperties, _internalTextPayloadProperties, cancellationToken);
910+
return SendConnectionObjectAsync(connectionState, message, Serialization.Default.GatewayMessagePropertiesGatewayIdentifyProperties, _internalTextMessageProperties, cancellationToken);
911911
}
912912

913913
/// <summary>
@@ -926,7 +926,7 @@ public async ValueTask StartAsync(PresenceProperties? presence = null, Cancellat
926926
/// Resumes the session specified by <paramref name="sessionId"/>.
927927
/// </summary>
928928
/// <param name="sessionId">The session to resume.</param>
929-
/// <param name="sequenceNumber">The sequence number of the payload to resume from.</param>
929+
/// <param name="sequenceNumber">The sequence number of the message to resume from.</param>
930930
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
931931
/// <returns></returns>
932932
public async ValueTask ResumeAsync(string sessionId, int sequenceNumber, CancellationToken cancellationToken = default)
@@ -950,48 +950,48 @@ private protected override ValueTask TryResumeAsync(ConnectionState connectionSt
950950

951951
private ValueTask TryResumeAsync(ConnectionState connectionState, string sessionId, int sequenceNumber, CancellationToken cancellationToken = default)
952952
{
953-
GatewayPayloadProperties<GatewayResumeProperties> payload = new(GatewayOpcode.Resume, new(Token.RawToken, sessionId, sequenceNumber));
953+
GatewayMessageProperties<GatewayResumeProperties> message = new(GatewayOpcode.Resume, new(Token.RawToken, sessionId, sequenceNumber));
954954

955955
_latencyTimer.Start();
956956

957-
return SendConnectionObjectAsync(connectionState, payload, Serialization.Default.GatewayPayloadPropertiesGatewayResumeProperties, _internalTextPayloadProperties, cancellationToken);
957+
return SendConnectionObjectAsync(connectionState, message, Serialization.Default.GatewayMessagePropertiesGatewayResumeProperties, _internalTextMessageProperties, cancellationToken);
958958
}
959959

960960
private protected override ValueTask HeartbeatAsync(ConnectionState connectionState, CancellationToken cancellationToken = default)
961961
{
962-
GatewayPayloadProperties<int> payload = new(GatewayOpcode.Heartbeat, SequenceNumber);
962+
GatewayMessageProperties<int> message = new(GatewayOpcode.Heartbeat, SequenceNumber);
963963

964964
_latencyTimer.Start();
965965

966-
return SendConnectionObjectAsync(connectionState, payload, Serialization.Default.GatewayPayloadPropertiesInt32, _internalTextPayloadProperties, cancellationToken);
966+
return SendConnectionObjectAsync(connectionState, message, Serialization.Default.GatewayMessagePropertiesInt32, _internalTextMessageProperties, cancellationToken);
967967
}
968968

969-
private protected override ValueTask ProcessPayloadAsync(State state, ConnectionState connectionState, WebSocketMessageType messageType, ReadOnlySpan<byte> payload)
969+
private protected override ValueTask ProcessMessageAsync(State state, ConnectionState connectionState, WebSocketMessageType messageType, ReadOnlySpan<byte> message)
970970
{
971-
var rawPayload = _compression.Decompress(payload);
972-
var jsonPayload = JsonSerializer.Deserialize(rawPayload, Serialization.Default.JsonGatewayPayload)!;
973-
return HandlePayloadAsync(state, connectionState, jsonPayload);
971+
var rawMessage = _compression.Decompress(message);
972+
var jsonMessage = JsonSerializer.Deserialize(rawMessage, Serialization.Default.JsonGatewayMessage)!;
973+
return HandleMessageAsync(state, connectionState, jsonMessage);
974974
}
975975

976-
private async ValueTask HandlePayloadAsync(State state, ConnectionState connectionState, JsonGatewayPayload payload)
976+
private async ValueTask HandleMessageAsync(State state, ConnectionState connectionState, JsonGatewayMessage message)
977977
{
978-
switch (payload.Opcode)
978+
switch (message.Opcode)
979979
{
980980
case GatewayOpcode.Dispatch:
981-
SequenceNumber = payload.SequenceNumber.GetValueOrDefault();
981+
SequenceNumber = message.SequenceNumber.GetValueOrDefault();
982982

983-
Log(LogLevel.Debug, payload, null, static (s, e) =>
983+
Log(LogLevel.Debug, message, null, static (s, e) =>
984984
{
985985
return $"'{s.Event}' event received.";
986986
});
987987

988988
try
989989
{
990-
await ProcessEventAsync(state, connectionState, payload).ConfigureAwait(false);
990+
await ProcessEventAsync(state, connectionState, message).ConfigureAwait(false);
991991
}
992992
catch (Exception ex)
993993
{
994-
Log(LogLevel.Error, payload, ex, static (s, e) =>
994+
Log(LogLevel.Error, message, ex, static (s, e) =>
995995
{
996996
return $"An error occurred while processing '{s.Event}' event.{Environment.NewLine}{e}";
997997
});
@@ -1015,14 +1015,14 @@ private async ValueTask HandlePayloadAsync(State state, ConnectionState connecti
10151015
{
10161016
Log<object?>(LogLevel.Error, null, ex, static (s, e) =>
10171017
{
1018-
return $"An error occurred while sending the identify payload.{Environment.NewLine}{e}";
1018+
return $"An error occurred while sending the identify message.{Environment.NewLine}{e}";
10191019
});
10201020
}
10211021
break;
10221022
case GatewayOpcode.Hello:
10231023
Log<object?>(LogLevel.Debug, null, null, static (s, e) => "Hello received.");
10241024

1025-
StartHeartbeating(connectionState, payload.Data.GetValueOrDefault().ToObject(Serialization.Default.JsonHello).HeartbeatInterval);
1025+
StartHeartbeating(connectionState, message.Data.GetValueOrDefault().ToObject(Serialization.Default.JsonHello).HeartbeatInterval);
10261026
break;
10271027
case GatewayOpcode.HeartbeatACK:
10281028
var latency = _latencyTimer.Elapsed;
@@ -1040,11 +1040,11 @@ private async ValueTask HandlePayloadAsync(State state, ConnectionState connecti
10401040
/// <summary>
10411041
/// Joins, moves, or disconnects the app from a voice channel.
10421042
/// </summary>
1043-
public ValueTask UpdateVoiceStateAsync(VoiceStateProperties voiceState, WebSocketPayloadProperties? properties = null, CancellationToken cancellationToken = default)
1043+
public ValueTask UpdateVoiceStateAsync(VoiceStateProperties voiceState, WebSocketMessageProperties? properties = null, CancellationToken cancellationToken = default)
10441044
{
1045-
GatewayPayloadProperties<VoiceStateProperties> payload = new(GatewayOpcode.VoiceStateUpdate, voiceState);
1045+
GatewayMessageProperties<VoiceStateProperties> message = new(GatewayOpcode.VoiceStateUpdate, voiceState);
10461046

1047-
return SendObjectAsync(payload, Serialization.Default.GatewayPayloadPropertiesVoiceStateProperties, properties, cancellationToken);
1047+
return SendObjectAsync(message, Serialization.Default.GatewayMessagePropertiesVoiceStateProperties, properties, cancellationToken);
10481048
}
10491049

10501050
/// <summary>
@@ -1053,27 +1053,27 @@ public ValueTask UpdateVoiceStateAsync(VoiceStateProperties voiceState, WebSocke
10531053
/// <param name="presence">The presence to set.</param>
10541054
/// <param name="properties"></param>
10551055
/// <param name="cancellationToken">The cancellation token to cancel the operation.</param>
1056-
public ValueTask UpdatePresenceAsync(PresenceProperties presence, WebSocketPayloadProperties? properties = null, CancellationToken cancellationToken = default)
1056+
public ValueTask UpdatePresenceAsync(PresenceProperties presence, WebSocketMessageProperties? properties = null, CancellationToken cancellationToken = default)
10571057
{
1058-
GatewayPayloadProperties<PresenceProperties> payload = new(GatewayOpcode.PresenceUpdate, presence);
1058+
GatewayMessageProperties<PresenceProperties> message = new(GatewayOpcode.PresenceUpdate, presence);
10591059

1060-
return SendObjectAsync(payload, Serialization.Default.GatewayPayloadPropertiesPresenceProperties, properties, cancellationToken);
1060+
return SendObjectAsync(message, Serialization.Default.GatewayMessagePropertiesPresenceProperties, properties, cancellationToken);
10611061
}
10621062

10631063
/// <summary>
10641064
/// Requests users for a guild.
10651065
/// </summary>
1066-
public ValueTask RequestGuildUsersAsync(GuildUsersRequestProperties requestProperties, WebSocketPayloadProperties? properties = null, CancellationToken cancellationToken = default)
1066+
public ValueTask RequestGuildUsersAsync(GuildUsersRequestProperties requestProperties, WebSocketMessageProperties? properties = null, CancellationToken cancellationToken = default)
10671067
{
1068-
GatewayPayloadProperties<GuildUsersRequestProperties> payload = new(GatewayOpcode.RequestGuildUsers, requestProperties);
1068+
GatewayMessageProperties<GuildUsersRequestProperties> message = new(GatewayOpcode.RequestGuildUsers, requestProperties);
10691069

1070-
return SendObjectAsync(payload, Serialization.Default.GatewayPayloadPropertiesGuildUsersRequestProperties, properties, cancellationToken);
1070+
return SendObjectAsync(message, Serialization.Default.GatewayMessagePropertiesGuildUsersRequestProperties, properties, cancellationToken);
10711071
}
10721072

1073-
private async Task ProcessEventAsync(State state, ConnectionState connectionState, JsonGatewayPayload payload)
1073+
private async Task ProcessEventAsync(State state, ConnectionState connectionState, JsonGatewayMessage message)
10741074
{
1075-
var data = payload.Data.GetValueOrDefault();
1076-
var name = payload.Event!;
1075+
var data = message.Data.GetValueOrDefault();
1076+
var name = message.Event!;
10771077
switch (name)
10781078
{
10791079
case "READY":

NetCord/Gateway/GatewayClientConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class GatewayClientConfiguration : IWebSocketClientConfiguration, IRestCl
2020
public IRateLimiterProvider? RateLimiterProvider { get; init; }
2121

2222
/// <inheritdoc/>
23-
public WebSocketPayloadProperties? DefaultPayloadProperties { get; init; }
23+
public WebSocketMessageProperties? DefaultMessageProperties { get; init; }
2424

2525
/// <inheritdoc/>
2626
public IReconnectStrategy? ReconnectStrategy { get; init; }

NetCord/Gateway/GatewayClientConfigurationFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal static class GatewayClientConfigurationFactory
1111
{
1212
public static GatewayClientConfiguration Create(IWebSocketConnectionProvider? webSocketConnectionProvider,
1313
IRateLimiterProvider? rateLimiterProvider,
14-
WebSocketPayloadProperties? defaultPayloadProperties,
14+
WebSocketMessageProperties? defaultMessageProperties,
1515
IReconnectStrategy? reconnectStrategy,
1616
ILatencyTimer? latencyTimer,
1717
ApiVersion? version,
@@ -30,7 +30,7 @@ public static GatewayClientConfiguration Create(IWebSocketConnectionProvider? we
3030
{
3131
WebSocketConnectionProvider = webSocketConnectionProvider,
3232
RateLimiterProvider = rateLimiterProvider,
33-
DefaultPayloadProperties = defaultPayloadProperties,
33+
DefaultMessageProperties = defaultMessageProperties,
3434
ReconnectStrategy = reconnectStrategy,
3535
LatencyTimer = latencyTimer,
3636
Version = version,

NetCord/Gateway/GatewayPayloadProperties.cs renamed to NetCord/Gateway/GatewayMessageProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace NetCord.Gateway;
44

5-
internal class GatewayPayloadProperties<T>(GatewayOpcode opcode, T d)
5+
internal class GatewayMessageProperties<T>(GatewayOpcode opcode, T d)
66
{
77
[JsonPropertyName("op")]
88
public GatewayOpcode Opcode { get; set; } = opcode;

NetCord/Gateway/IWebSocketClientConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ internal interface IWebSocketClientConfiguration
2828
public IRateLimiterProvider? RateLimiterProvider { get; }
2929

3030
/// <summary>
31-
/// The default payload properties for WebSocket payloads. Defaults to <see langword="null"/>.
31+
/// The default properties for WebSocket messages. Defaults to <see langword="null"/>.
3232
/// </summary>
33-
public WebSocketPayloadProperties? DefaultPayloadProperties { get; }
33+
public WebSocketMessageProperties? DefaultMessageProperties { get; }
3434

3535
/// <summary>
3636
/// The logger for the <see cref="WebSocketClient"/>. Defaults to <see cref="NullLogger"/>.

NetCord/Gateway/JsonModels/JsonGatewayPayload.cs renamed to NetCord/Gateway/JsonModels/JsonGatewayMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace NetCord.Gateway.JsonModels;
55

6-
internal class JsonGatewayPayload
6+
internal class JsonGatewayMessage
77
{
88
[JsonPropertyName("op")]
99
public GatewayOpcode Opcode { get; set; }

NetCord/Gateway/ShardedGatewayClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private static ShardedGatewayClientConfiguration CreateConfiguration(ShardedGate
8181

8282
return ShardedGatewayClientConfigurationFactory.Create(configuration.WebSocketConnectionProviderFactory ?? (_ => null),
8383
configuration.RateLimiterProviderFactory ?? (_ => null),
84-
configuration.DefaultPayloadPropertiesFactory ?? (_ => null),
84+
configuration.DefaultMessagePropertiesFactory ?? (_ => null),
8585
configuration.ReconnectStrategyFactory ?? (_ => null),
8686
configuration.LatencyTimerFactory ?? (_ => null),
8787
configuration.VersionFactory ?? (_ => ApiVersion.V10),
@@ -311,7 +311,7 @@ private static GatewayClientConfiguration GetGatewayClientConfiguration(ShardedG
311311
{
312312
return GatewayClientConfigurationFactory.Create(configuration.WebSocketConnectionProviderFactory!(shard),
313313
configuration.RateLimiterProviderFactory!(shard),
314-
configuration.DefaultPayloadPropertiesFactory!(shard),
314+
configuration.DefaultMessagePropertiesFactory!(shard),
315315
configuration.ReconnectStrategyFactory!(shard),
316316
configuration.LatencyTimerFactory!(shard),
317317
configuration.VersionFactory!(shard),

0 commit comments

Comments
 (0)