Skip to content

Commit

Permalink
Cleanup and improved server move handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WamWooWam committed Sep 22, 2019
1 parent 6ccd940 commit 719d7ce
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 46 deletions.
14 changes: 8 additions & 6 deletions Unicord.Universal.Voice/ServiceBackgroundTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ namespace winrt::Unicord::Universal::Voice::Background::implementation
taskInstance.Canceled({ this, &ServiceBackgroundTask::OnCancelled });

auto details = taskInstance.TriggerDetails().try_as<AppServiceTriggerDetails>();

this->appServiceConnection = details.AppServiceConnection();
this->appServiceConnected = true;
this->appServiceConnection.ServiceClosed({ this, &ServiceBackgroundTask::OnServiceClosed });
this->appServiceConnection.RequestReceived({ this, &ServiceBackgroundTask::OnServiceMessage });
this->appServiceConnected = true;
}

void ServiceBackgroundTask::OnUdpPing(IInspectable sender, uint32_t ping)
{
if (this->appServiceConnected) {
if (appServiceConnected && appServiceConnection != nullptr) {
ValueSet values;
values.Insert(L"ping", box_value(ping));
RaiseEvent(VoiceServiceEvent::UdpPing, values);
Expand All @@ -45,7 +46,7 @@ namespace winrt::Unicord::Universal::Voice::Background::implementation

void ServiceBackgroundTask::OnWsPing(IInspectable sender, uint32_t ping)
{
if (this->appServiceConnected) {
if (appServiceConnected && appServiceConnection != nullptr) {
ValueSet values;
values.Insert(L"ping", box_value(ping));
RaiseEvent(VoiceServiceEvent::WebSocketPing, values);
Expand All @@ -55,7 +56,7 @@ namespace winrt::Unicord::Universal::Voice::Background::implementation
void ServiceBackgroundTask::OnDisconnected(IInspectable sender, bool args)
{
ValueSet valueSet;
if (this->appServiceConnected) {
if (appServiceConnected && appServiceConnection != nullptr) {
if (args) { // we're attempting to reconnect
RaiseEvent(VoiceServiceEvent::Reconnecting, valueSet);
}
Expand All @@ -68,14 +69,14 @@ namespace winrt::Unicord::Universal::Voice::Background::implementation
void ServiceBackgroundTask::OnConnected(Windows::Foundation::IInspectable sender, bool args)
{
ValueSet valueSet;
if (this->appServiceConnected) {
if (appServiceConnected && appServiceConnection != nullptr) {
RaiseEvent(VoiceServiceEvent::Connected, valueSet);
}
}

void ServiceBackgroundTask::RaiseEvent(VoiceServiceEvent ev, ValueSet data)
{
if (appServiceConnected) {
if (appServiceConnected && appServiceConnection != nullptr) {
data.Insert(L"ev", box_value((uint32_t)ev));
this->appServiceConnection.SendMessageAsync(data);
}
Expand Down Expand Up @@ -256,6 +257,7 @@ namespace winrt::Unicord::Universal::Voice::Background::implementation
void ServiceBackgroundTask::OnServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
{
this->appServiceConnected = false;
this->appServiceConnection = nullptr;
}

void ServiceBackgroundTask::OnCancelled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
Expand Down
30 changes: 2 additions & 28 deletions Unicord.Universal/Controls/VoiceConnectionControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,12 @@ public VoiceConnectionModel ConnectionModel
}

public static readonly DependencyProperty ConnectionModelProperty =
DependencyProperty.Register("ConnectionModel", typeof(VoiceConnectionModel), typeof(VoiceConnectionControl), new PropertyMetadata(null, OnConnectionModelChanged));
DependencyProperty.Register("ConnectionModel", typeof(VoiceConnectionModel), typeof(VoiceConnectionControl), new PropertyMetadata(null));

public VoiceConnectionControl()
{
this.InitializeComponent();
Visibility = Visibility.Collapsed;
}

private static void OnConnectionModelChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is VoiceConnectionControl control)
{
if (e.NewValue == null)
{
control.Visibility = Visibility.Collapsed;
if(e.OldValue is VoiceConnectionModel old)
{
old.Disconnected -= control.Model_Disconnected;
}
}
else if (e.NewValue is VoiceConnectionModel model)
{
control.Visibility = Visibility.Visible;
model.Disconnected += control.Model_Disconnected;
}
}
}

private async void Model_Disconnected(object sender, EventArgs e)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Visibility = Visibility.Collapsed);
}
}

private async void DisconnectButton_Click(object sender, RoutedEventArgs e)
{
Expand Down
2 changes: 1 addition & 1 deletion Unicord.Universal/Pages/DiscordPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@
Width="220" />

<Grid Grid.Row="3" Background="{ThemeResource SidebarPrimaryAcrylicWindowBrush}">
<controls:VoiceConnectionControl ConnectionModel="{Binding VoiceModel}"/>
<controls:VoiceConnectionControl ConnectionModel="{Binding VoiceModel}" Visibility="{Binding VoiceModel, Converter={StaticResource HideOnNullConverter}}"/>
</Grid>

<Grid Grid.Row="4" Background="{ThemeResource SidebarPrimaryAcrylicWindowBrush}">
Expand Down
19 changes: 9 additions & 10 deletions Unicord.Universal/Pages/DiscordPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,29 +410,28 @@ internal async void Navigate(DiscordChannel channel, NavigationTransitionInfo in

if (channel.Type == ChannelType.Voice)
{
if (!channel.PermissionsFor(channel.Guild.CurrentMember).HasPermission(Permissions.UseVoice))
return;

var model = DataContext as DiscordPageModel;
var voiceModel = model.VoiceModel;

try
{
if (voiceModel == null)
if (voiceModel == null || voiceModel.Channel.Guild != channel.Guild || voiceModel.IsDisposed)
{
await (voiceModel?.DisconnectAsync() ?? Task.CompletedTask);
voiceModel?.Dispose();

voiceModel = new VoiceConnectionModel(channel);
voiceModel.Disconnected += (o, ev) => model.VoiceModel = null;
model.VoiceModel = voiceModel;
await voiceModel.ConnectAsync();
}
else if (voiceModel.Channel.Guild == channel.Guild)
else
{
await voiceModel.MoveAsync(channel);
}
else
{
await voiceModel.DisconnectAsync();

voiceModel = new VoiceConnectionModel(channel);
model.VoiceModel = voiceModel;
await voiceModel.ConnectAsync();
}
}
catch (Exception ex)
{
Expand Down
17 changes: 16 additions & 1 deletion Unicord.Universal/Voice/VoiceConnectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public bool Deafened

public uint WebSocketPing { get => _webSocketPing; set => OnPropertySet(ref _webSocketPing, value); }
public uint UdpPing { get => _udpPing; set => OnPropertySet(ref _udpPing, value); }
public bool IsDisposed { get; private set; }

public event EventHandler<EventArgs> Disconnected;

/// <summary>
Expand Down Expand Up @@ -281,10 +283,18 @@ public async Task DisconnectAsync()
{
var set = new ValueSet() { ["req"] = (uint)VoiceServiceRequest.DisconnectRequest };
await SendRequestAsync(set);

await PlayCueAsync("self_disconnected");
ConnectionStatus = _strings.GetString("DisconnectedState");
Disconnected?.Invoke(this, null);
SendVoiceStateUpdate(null);
}

private async Task<ValueSet> SendRequestAsync(ValueSet request)
{
if (IsDisposed)
return null;

var response = await _appServiceConnection.SendMessageAsync(request);
if (response.Message != null)
{
Expand Down Expand Up @@ -338,7 +348,6 @@ private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequ
ConnectionStatus = _strings.GetString("DisconnectedState");
Disconnected?.Invoke(this, null);
SendVoiceStateUpdate(null);
Dispose();
break;
case VoiceServiceEvent.Muted:
await PlayCueAsync(_muted ? "mute" : "unmute");
Expand All @@ -363,6 +372,7 @@ private async void OnRequestReceived(AppServiceConnection sender, AppServiceRequ
private void OnServiceClosed(AppServiceConnection sender, AppServiceClosedEventArgs args)
{
_appServiceConnected = false;
_appServiceConnection = null;
}

private Task OnVoiceStateUpdated(VoiceStateUpdateEventArgs e)
Expand Down Expand Up @@ -421,7 +431,12 @@ await _mediaPlayer.Dispatcher.RunAsync(CoreDispatcherPriority.High, () =>

public void Dispose()
{
IsDisposed = true;

_appServiceConnected = false;

_appServiceConnection?.Dispose();
_appServiceConnection = null;
_mediaPlayer.MediaPlayer?.Dispose();
App.Discord.VoiceStateUpdated -= OnVoiceStateUpdated;
}
Expand Down

0 comments on commit 719d7ce

Please sign in to comment.