From 7ef44762a0b7de7c580d3e0f8622ca06d24de0c3 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Fri, 12 Jun 2020 11:13:51 -0500 Subject: [PATCH] Don't crash when no tcp connection can be created. --- .../Classes/Communication/QClient.cs | 4 +- .../Classes/Communication/QParser.cs | 104 ++---------------- .../Communication/SharpOSC/TCPClient.cs | 40 +++++-- .../Pages/ControlPage.xaml.cs | 8 +- qController.Standard/qController.csproj | 2 - 5 files changed, 49 insertions(+), 109 deletions(-) diff --git a/qController.Standard/Classes/Communication/QClient.cs b/qController.Standard/Classes/Communication/QClient.cs index 388e380..3b1a2e7 100644 --- a/qController.Standard/Classes/Communication/QClient.cs +++ b/qController.Standard/Classes/Communication/QClient.cs @@ -13,6 +13,7 @@ namespace qController.Communication public QParser qParser; string Address; int Port; + public bool connected; public QClient(string address, int port) { @@ -20,8 +21,7 @@ namespace qController.Communication Port = port; qParser = new QParser(); tcpClient = new TCPClient(Address, Port); - tcpClient.Connect(); - + connected = tcpClient.Connect(); tcpClient.MessageReceived += OnMessageReceived; } diff --git a/qController.Standard/Classes/Communication/QParser.cs b/qController.Standard/Classes/Communication/QParser.cs index cb9d479..7941aa9 100644 --- a/qController.Standard/Classes/Communication/QParser.cs +++ b/qController.Standard/Classes/Communication/QParser.cs @@ -8,70 +8,7 @@ using Serilog; namespace qController.Communication { - public class CueEventArgs : EventArgs - { - public QCue Cue - { - get; - set; - } - } - - public class WorkspaceEventArgs : EventArgs - { - public QWorkspace UpdatedWorkspace - { - get; - set; - } - } - - public class PlaybackPositionArgs : EventArgs - { - public string PlaybackPosition - { - get; - set; - } - } - - public class ConnectEventArgs : EventArgs - { - public string Status - { - get; - set; - } - - public string WorkspaceId - { - get; - set; - } - } - - public class WorkspaceInfoArgs : EventArgs - { - public List WorkspaceInfo - { - get; - set; - } - } - - public class ChildrenEventArgs : EventArgs - { - public string cue_id - { - get; - set; - } - public List children - { - get; - set; - } - } + public class QParser { @@ -80,15 +17,7 @@ namespace qController.Communication } - public delegate void SelectedCueUpdatedHandler(object source, CueEventArgs args); - public delegate void WorkspaceUpdatedHandler(object source, WorkspaceEventArgs args); - public delegate void CueInfoUpdatedHandler(object source, CueEventArgs args); - public delegate void PlaybackPositionUpdatedHandler(object source, PlaybackPositionArgs args); - public delegate void ConnectionStatusHandler(object source, ConnectEventArgs args); - public delegate void ChildrenUpdateHandler(object source, ChildrenEventArgs args); - public delegate void WorkspaceDisconnectHandler(object source, EventArgs args); - public delegate void WorkspaceInfoHandler(object source, WorkspaceInfoArgs args); - public delegate void WorkspaceLoadErrorHandler(object source, WorkspaceEventArgs args); + public event WorkspaceLoadErrorHandler WorkspaceLoadError; public event WorkspaceInfoHandler WorkspaceInfoReceived; public event WorkspaceDisconnectHandler WorkspaceDisconnect; @@ -214,50 +143,41 @@ namespace qController.Communication protected virtual void OnWorkspaceUpdated(QWorkspace workspace) { - if (WorkspaceUpdated != null) - WorkspaceUpdated(this, new WorkspaceEventArgs() { UpdatedWorkspace = workspace }); + WorkspaceUpdated?.Invoke(this, new WorkspaceEventArgs() { UpdatedWorkspace = workspace }); } protected virtual void OnSelectedCueUpdated(QCue cue) { - if (SelectedCueUpdated != null) - SelectedCueUpdated(this, new CueEventArgs() { Cue = cue }); + SelectedCueUpdated?.Invoke(this, new CueEventArgs() { Cue = cue }); } protected virtual void OnCueInfoUpdated(QCue cue) - { - if (CueInfoUpdated != null) - CueInfoUpdated(this, new CueEventArgs() { Cue = cue }); + { + CueInfoUpdated?.Invoke(this, new CueEventArgs() { Cue = cue }); } protected virtual void OnPlaybackPositionUpdated(string id) { - if (PlaybackPositionUpdated != null) - PlaybackPositionUpdated(this, new PlaybackPositionArgs() { PlaybackPosition = id }); + PlaybackPositionUpdated?.Invoke(this, new PlaybackPositionArgs() { PlaybackPosition = id }); } protected virtual void OnConnectionStatusChanged(string status, string workspace_id) { - if (ConnectionStatusChanged != null) - ConnectionStatusChanged(this, new ConnectEventArgs() { Status = status, WorkspaceId = workspace_id }); + ConnectionStatusChanged?.Invoke(this, new ConnectEventArgs() { Status = status, WorkspaceId = workspace_id }); } protected virtual void OnChildrenUpdated(string id, List cues) { - if (ChildrenUpdated != null) - ChildrenUpdated(this, new ChildrenEventArgs() { cue_id = id, children = cues }); + ChildrenUpdated?.Invoke(this, new ChildrenEventArgs() { cue_id = id, children = cues }); } protected virtual void OnWorkspaceDisconnect() { - if (WorkspaceDisconnect != null) - WorkspaceDisconnect(this, new EventArgs()); + WorkspaceDisconnect?.Invoke(this, new EventArgs()); } protected virtual void OnWorkspaceInfoReceived(List workspaces) { - if (WorkspaceInfoReceived != null) - WorkspaceInfoReceived(this, new WorkspaceInfoArgs() { WorkspaceInfo = workspaces }); + WorkspaceInfoReceived?.Invoke(this, new WorkspaceInfoArgs() { WorkspaceInfo = workspaces }); } protected virtual void OnWorkspaceLoadError(string id) { - if (WorkspaceLoadError != null) - WorkspaceLoadError(this, new WorkspaceEventArgs { UpdatedWorkspace = new QWorkspace(id) }); + WorkspaceLoadError?.Invoke(this, new WorkspaceEventArgs { UpdatedWorkspace = new QWorkspace(id) }); } } diff --git a/qController.Standard/Classes/Communication/SharpOSC/TCPClient.cs b/qController.Standard/Classes/Communication/SharpOSC/TCPClient.cs index 790ecfc..cf11ca1 100644 --- a/qController.Standard/Classes/Communication/SharpOSC/TCPClient.cs +++ b/qController.Standard/Classes/Communication/SharpOSC/TCPClient.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Text; using System.Net.Sockets; using System.Threading; -using Serilog; namespace SharpOSC { @@ -47,12 +46,22 @@ namespace SharpOSC _address = address; } - public void Connect() + public bool Connect() { - client = new TcpClient(Address, Port); - Log.Debug($"TCPClient - connect called for <{Address}:{Port}>"); - Thread receivingThread = new Thread(ReceiveLoop); - receivingThread.Start(); + try + { + client = new TcpClient(Address, Port); + Thread receivingThread = new Thread(ReceiveLoop); + receivingThread.Start(); + //Console.WriteLine($"TCPClient - connected to <{Address}:{Port}>"); + return true; + } + catch (Exception e) + { + Console.WriteLine(e.Message); + return false; + } + } public void Send(byte[] message) @@ -68,13 +77,21 @@ namespace SharpOSC Send(data); } + public bool IsConnected + { + get + { + return client.Connected; + } + } + public void ReceiveLoop() { while (client.Connected) { Receive(); } - Log.Debug("TCPClient - Receive Loop has exited for some reason"); + Console.WriteLine("TCPClient - Receive Loop has exited for some reason"); } public void Receive() @@ -102,15 +119,15 @@ namespace SharpOSC //Log.Debug("Thread " + num + ": Bytes read: " + bytesRead + " - " + Encoding.UTF8.GetString(buffer)); } while (netStream.DataAvailable); - //Log.Debug("Raw TCP In: " + System.Text.Encoding.UTF8.GetString(responseData.ToArray())); + //Console.WriteLine("Raw TCP In: " + System.Text.Encoding.UTF8.GetString(responseData.ToArray())); OscMessage response = (OscMessage)OscPacket.GetPacket(responseData.Skip(1).ToArray()); //watch.Stop(); - //Log.Debug($"TCPCLient - message receive took {watch.ElapsedMilliseconds}ms and {reads} reads"); + //Console.WriteLine($"TCPCLient - message receive took {watch.ElapsedMilliseconds}ms and {reads} reads"); OnMessageReceived(response); } } catch(Exception e) { - //Log.Debug("TCPSENDER - Receive Exception: " + e.ToString()); + //Console.WriteLine("TCPSENDER - Receive Exception: " + e.ToString()); } } @@ -150,8 +167,7 @@ namespace SharpOSC protected virtual void OnMessageReceived(OscMessage msg) { - if (MessageReceived != null) - MessageReceived(this, new MessageEventArgs() { Message = msg }); + MessageReceived?.Invoke(this, new MessageEventArgs() { Message = msg }); } } } diff --git a/qController.Standard/Pages/ControlPage.xaml.cs b/qController.Standard/Pages/ControlPage.xaml.cs index f3878f0..6f79b29 100644 --- a/qController.Standard/Pages/ControlPage.xaml.cs +++ b/qController.Standard/Pages/ControlPage.xaml.cs @@ -40,6 +40,11 @@ namespace qController InitGUI(); + if (!qController.qClient.connected) + { + App.showToast("Error connecting...make sure QLab is running"); + Back(); + } qController.KickOff(); } @@ -220,7 +225,8 @@ namespace qController void Back() { - qController.Kill(); + if(qController.qClient.connected) + qController.Kill(); App.rootPage.MenuItemSelected -= OnMenuItemSelected; Device.BeginInvokeOnMainThread(() => { diff --git a/qController.Standard/qController.csproj b/qController.Standard/qController.csproj index c424aa7..ba9b0c3 100644 --- a/qController.Standard/qController.csproj +++ b/qController.Standard/qController.csproj @@ -83,11 +83,9 @@ --> - -