Skip to content

Commit 47223cd

Browse files
committed
Add VoiceConnectionMaintenance example project
1 parent 1e5b82f commit 47223cd

12 files changed

Lines changed: 622 additions & 0 deletions

File tree

Documentation/guides/voice/VoiceConnectionMaintenance/Program.cs

Lines changed: 423 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<ProjectReference Include="..\..\..\..\NetCord\NetCord.csproj" />
9+
<ProjectReference Include="..\..\..\..\NetCord.Services\NetCord.Services.csproj" />
10+
<ProjectReference Include="..\..\..\..\Hosting\NetCord.Hosting\NetCord.Hosting.csproj" />
11+
<ProjectReference Include="..\..\..\..\Hosting\NetCord.Hosting.Services\NetCord.Hosting.Services.csproj" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<PackageReference Include="libsodium" />
16+
<PackageReference Include="Microsoft.Extensions.Hosting" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<None Update="runtimes\**">
21+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
22+
</None>
23+
</ItemGroup>
24+
25+
</Project>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using NetCord.Gateway.Voice;
2+
3+
internal sealed class VoiceInstance(VoiceClient client) : IDisposable
4+
{
5+
private static readonly int JobTypeCount = Enum.GetValues<VoiceJobType>().Length;
6+
7+
public VoiceClient Client => client;
8+
9+
private readonly CancellationTokenSource _cancellationTokenSource = new();
10+
11+
private readonly byte[] _jobStatuses = new byte[JobTypeCount];
12+
13+
public Job? TryEnterJob(VoiceJobType type)
14+
{
15+
return Interlocked.CompareExchange(ref _jobStatuses[(int)type], 1, 0) is 0
16+
? new(this, type, _cancellationTokenSource.Token)
17+
: null;
18+
}
19+
20+
public void Dispose()
21+
{
22+
var tokenSource = _cancellationTokenSource;
23+
tokenSource.Cancel();
24+
tokenSource.Dispose();
25+
client.Dispose();
26+
}
27+
28+
public readonly record struct Job(VoiceInstance Instance,
29+
VoiceJobType JobType,
30+
CancellationToken CancellationToken) : IDisposable
31+
{
32+
public void Dispose()
33+
{
34+
Interlocked.Exchange(ref Instance._jobStatuses[(int)JobType], 0);
35+
}
36+
}
37+
}
38+
39+
internal enum VoiceJobType
40+
{
41+
Playing = 0,
42+
Recording = 1,
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Discord
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Binary file not shown.
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
Copyright 2001-2023 Xiph.Org, Skype Limited, Octasic,
2+
Jean-Marc Valin, Timothy B. Terriberry,
3+
CSIRO, Gregory Maxwell, Mark Borgerding,
4+
Erik de Castro Lopo, Mozilla, Amazon
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions
8+
are met:
9+
10+
- Redistributions of source code must retain the above copyright
11+
notice, this list of conditions and the following disclaimer.
12+
13+
- Redistributions in binary form must reproduce the above copyright
14+
notice, this list of conditions and the following disclaimer in the
15+
documentation and/or other materials provided with the distribution.
16+
17+
- Neither the name of Internet Society, IETF or IETF Trust, nor the
18+
names of specific contributors, may be used to endorse or promote
19+
products derived from this software without specific prior written
20+
permission.
21+
22+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
34+
Opus is subject to the royalty-free patent licenses which are
35+
specified at:
36+
37+
Xiph.Org Foundation:
38+
https://datatracker.ietf.org/ipr/1524/
39+
40+
Microsoft Corporation:
41+
https://datatracker.ietf.org/ipr/1914/
42+
43+
Broadcom Corporation:
44+
https://datatracker.ietf.org/ipr/1526/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Discord
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)