/// <summary> /// Applies token setting. /// </summary> private void Apply() { try { string token = this.TextBoxTokenInfo.Text; if (token != null) { token = token.Trim(new char[] { '"', ' ' }); App.Settings.Token = token; if (ZokmaApi.CheckTokenValid(token)) { this.DialogResult = true; } } } finally { this.Close(); } }
/// <summary> /// Stops the service. /// </summary> public async Task Stop() { try { this.isStopProcessing = true; try { this.listener?.Stop(); } catch (Exception ex) { Log.Warning(ex, "Error on stopping listener."); } finally { this.listener?.Close(); } try { this.cancellationTokenSource?.Cancel(); } catch (Exception ex) { Log.Warning(ex, "Error on cancel."); } finally { this.cancellationTokenSource?.Dispose(); } this.audioManager?.Dispose(); this.autoCloseTimer?.Stop(); if (this.zokmaApi != null && this.SoundId != null) { try { await this.zokmaApi?.DeleteSound(this.SoundId, CancellationToken.None); } catch (Exception ex) { Log.Warning(ex, "Error on deleting sound."); } } } catch (Exception ex) { Log.Warning(ex, "Error on stopping service."); } finally { this.cancellationTokenSource = null; this.listener = null; this.audioManager = null; this.autoCloseTimer = null; this.zokmaApi = null; this.IsRunning = false; } }
/// <summary> /// Starts service. /// </summary> /// <param name="config">Service config.</param> /// <returns>Service info.</returns> public async Task <ServiceInfo> StartService(ServiceConfig config) { if (this.IsRunning) { await this.Stop(); } this.isStopProcessing = false; string streamingId = null; if (!String.IsNullOrWhiteSpace(config.LiveUrl)) { streamingId = GetStreamingId(config.LiveUrl.Trim()); if (streamingId == null) { this.notification.ShowNotification(LocalizedInfo.MessageLiveUrlNotSupportedWarning, Notification.NotificationLevel.Warn); } } var settings = App.Settings; this.cancellationTokenSource = new CancellationTokenSource(); this.cancellationToken = this.cancellationTokenSource.Token; var tunnelInfo = await this.ngrokManager.FindTunnel(config.ForwardingPort.Value, settings.NgrokRegion, this.cancellationToken); TimeSpan validityPeriod = TimeSpan.Zero; if (tunnelInfo == null) { this.notification.ShowNotification(LocalizedInfo.MessageNoNgrokDetected, Notification.NotificationLevel.Error); } else { this.audioManager = AudioManager.CreateAudioManager(config.AudioItems, config.AudioDataDirectory, notification); if (this.audioManager.AudioItems == null || this.audioManager.AudioItems.Count <= 0) { notification.ShowNotification(LocalizedInfo.MessageValidAudioFileNotFound, Notification.NotificationLevel.Error); } else { this.zokmaApi = new ZokmaApi(settings.Token); var secret = new byte[SECRET_LENGTH]; random.GetBytes(secret); this.secretString = Convert.ToBase64String(secret, Base64FormattingOptions.None); string guid = Guid.NewGuid().ToString("N"); try { var sound = await zokmaApi.CreateSound( (tunnelInfo.PublicUrl + String.Format(AUDIO_RENDERINGS_PATH_PATTERN, guid)), this.audioManager.AudioItems.ToArray(), this.secretString, settings.ServiceValiditySeconds, null, streamingId, this.cancellationToken); if (sound != null) { this.audioPlayer = config.AudioPlayer; this.SoundId = sound.Id; validityPeriod = TimeSpan.FromSeconds(sound.ValiditySeconds); this.autoCloseTimer = new DispatcherTimer(DispatcherPriority.Background, this.dispatcher) { Interval = validityPeriod, }; this.autoCloseTimer.Tick += AutoCloseTimer_Tick; this.autoCloseTimer.Start(); StartListener(tunnelInfo.ForwardingInfo.Port, guid); this.IsRunning = true; } } catch (AuthenticationException) { this.notification.ShowNotification(LocalizedInfo.MessageTokenInvalidError, Notification.NotificationLevel.Error); } catch (HttpRequestException hre) { Log.Warning(hre, "Error on creating sound."); this.notification.ShowNotification(LocalizedInfo.MessageHttpRequestFailed, Notification.NotificationLevel.Error); } catch (JsonException jex) { Log.Warning(jex, "Error on creating sound."); } catch (HttpPostSizeTooLargeException hpstlex) { Log.Error(hpstlex, "HTTP POST size is too large."); this.notification.ShowNotification(LocalizedInfo.MessageHttpPostSizeTooLarge, Notification.NotificationLevel.Error); } } } return(new ServiceInfo(this, tunnelInfo, validityPeriod)); }