Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Receiving voice packets (use system-assigned port) #2857

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/Discord.Net.WebSocket/Audio/AudioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,9 @@ private async Task ProcessPacketAsync(byte[] packet)
return;
}
string ip;
int port;
try
{
ip = Encoding.UTF8.GetString(packet, 8, 74 - 10).TrimEnd('\0');
port = (packet[73] << 8) | packet[72];
}
catch (Exception ex)
{
Expand All @@ -334,7 +332,7 @@ private async Task ProcessPacketAsync(byte[] packet)
}

await _audioLogger.DebugAsync("Received Discovery").ConfigureAwait(false);
await ApiClient.SendSelectProtocol(ip, port).ConfigureAwait(false);
await ApiClient.SendSelectProtocol(ip).ConfigureAwait(false);
}
else if (_connection.State == ConnectionState.Connected)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Discord.Net.WebSocket/DiscordVoiceApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ public Task SendIdentityAsync(ulong userId, string sessionId, string token)
});
}

public Task SendSelectProtocol(string externalIp, int externalPort)
public Task SendSelectProtocol(string externalIp)
{
return SendAsync(VoiceOpCode.SelectProtocol, new SelectProtocolParams
{
Protocol = "udp",
Data = new UdpProtocolInfo
{
Address = externalIp,
Port = externalPort,
Port = UdpPort,
Mode = Mode
}
});
Expand Down
12 changes: 3 additions & 9 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1744,11 +1744,9 @@ internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDe

if (external)
{
#pragma warning disable IDISP001
var _ = promise.TrySetResultAsync(null);
_ = promise.TrySetResultAsync(null);
await Discord.ApiClient.SendVoiceStateUpdateAsync(_voiceStateUpdateParams).ConfigureAwait(false);
return null;
#pragma warning restore IDISP001
}

if (_audioClient == null)
Expand All @@ -1766,19 +1764,15 @@ internal async Task<IAudioClient> ConnectAudioAsync(ulong channelId, bool selfDe
await promise.TrySetExceptionAsync(ex);
else
await promise.TrySetCanceledAsync();
return;
}
};
audioClient.Connected += () =>
{
#pragma warning disable IDISP001
var _ = promise.TrySetResultAsync(_audioClient);
#pragma warning restore IDISP001
_ = promise.TrySetResultAsync(_audioClient);
return Task.Delay(0);
};
#pragma warning disable IDISP003

_audioClient = audioClient;
#pragma warning restore IDISP003
}

await Discord.ApiClient.SendVoiceStateUpdateAsync(_voiceStateUpdateParams).ConfigureAwait(false);
Expand Down
Loading