public void Dispose() { listenerSubscription.Dispose(); peer.Value.Dispose(); KillPhotonProcess.Dispose(); StartPhotonProcess.Dispose(); }
public MainWindowViewModel() { // Load LoadDefaultConfiguration(); var useConnectionType = UseConnectionTypeSelectedIndex .Select(x => (ConnectionProtocol)Enum.Parse(typeof(ConnectionProtocol), UseConnectionType[x])) .ToReactiveProperty(); // Setup peer peer = new ReactiveProperty <ObservablePhotonPeer>(new UseJsonObservablePhotonPeer(useConnectionType.Value)); peer.Select(x => x.ObserveStatusChanged()) .Switch() .Subscribe(x => { if (x == StatusCode.Connect) { CurrentConnectionStatus.Value = "Connecting : " + Address.Value + " " + AppName.Value; } else { CurrentConnectionStatus.Value = x.ToString(); } Log.WriteLine(CurrentConnectionStatus.Value); }); // Setup Properties HubInfoListSelectedIndex.Subscribe(x => { foreach (var item in OperationInfoList) { item.Dispose(); } OperationInfoList.Clear(); if (x == -1) { return; } if (HubInfoList.Count - 1 < x) { return; } var hub = HubInfoList[x]; foreach (var item in hub.Operations) { OperationInfoList.Add(new OperationItemViewModel(peer, Log, item)); } }); // Setup Commands var photonProcessExists = Observable.Interval(TimeSpan.FromSeconds(1)).Select(x => Process.GetProcessesByName("PhotonSocketserver").Any()); KillPhotonProcess = photonProcessExists.ToReactiveCommand(); KillPhotonProcess.Subscribe(_ => { var processes = Process.GetProcessesByName("PhotonSocketServer"); foreach (var item in processes) { item.Kill(); } }); StartPhotonProcess = ProcessPath.CombineLatest(WorkingDir, (processPath, workingDir) => new { processPath, workingDir }) .Select(x => !string.IsNullOrWhiteSpace(x.processPath + x.workingDir)) .CombineLatest(photonProcessExists, (x, y) => x && !y) .ToReactiveCommand(); StartPhotonProcess.Subscribe(_ => { try { var processPath = ProcessPath.Value; var workingDir = WorkingDir.Value; var pi = new ProcessStartInfo { FileName = ProcessPath.Value, Arguments = ProcessArgument.Value, WorkingDirectory = workingDir }; System.Diagnostics.Process.Start(pi); SaveConfiguration(); // can start, save path } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }); ReloadDll = DllPath.Select(x => File.Exists(x.Trim('\"'))).ToReactiveCommand(ImmediateScheduler.Instance); // needs Immediate check for InitialLoad(see:bottom code) ReloadDll.Subscribe(_ => { try { HubInfoList.Clear(); var hubInfos = HubAnalyzer.LoadHubInfos(DllPath.Value.Trim('\"')); SaveConfiguration(); // can load, save path hubInfoLookup = hubInfos.ToDictionary(x => x.HubId); foreach (var hub in hubInfos) { HubInfoList.Add(hub); } HubInfoListSelectedIndex.Value = 0; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }); Connect = peer.Select(x => x.ObserveStatusChanged()) .Switch() .CombineLatest(Address, (x, y) => x != StatusCode.Connect && !string.IsNullOrEmpty(y)) .ToReactiveCommand(); Connect.Subscribe(async _ => { try { peer.Value.Dispose(); peer.Value = new UseJsonObservablePhotonPeer(useConnectionType.Value); var b = await peer.Value.ConnectAsync(Address.Value, AppName.Value); Log.WriteLine("Connect:" + b); if (b) { SaveConfiguration(); // can connect, save path // Register Listener listenerSubscription.Disposable = peer.Value.ObserveReceiveEventData().Subscribe(ReceiveEvent); } else { listenerSubscription.Disposable = System.Reactive.Disposables.Disposable.Empty; } } catch (Exception ex) { Log.WriteLine("Can't connect:" + ex.ToString()); listenerSubscription.Disposable = System.Reactive.Disposables.Disposable.Empty; } }); Disconnect = peer.Select(x => x.ObserveStatusChanged()) .Switch() .Select(x => x == StatusCode.Connect) .ToReactiveCommand(); Disconnect.Subscribe(_ => { try { peer.Value.Disconnect(); } catch (Exception ex) { Log.WriteLine("Can't disconnect:" + ex.ToString()); } }); LogClear = new ReactiveCommand(); LogClear.Subscribe(_ => { Log.Value = ""; }); // Initial Load if (ReloadDll.CanExecute()) { ReloadDll.Execute(); } // Initial VersionInfo VersionInfo = Assembly.GetExecutingAssembly().GetCustomAttribute <AssemblyFileVersionAttribute>().Version.ToString(); if (peer.Value.ObserveStatusChanged().FirstAsync().GetAwaiter().GetResult() == StatusCode.Disconnect) { CurrentConnectionStatus.Value = "PhotonWire.HubInvoker " + VersionInfo; } }