From 620aeecfa9e1a1021fde1f2075eddc1081c3a9ba Mon Sep 17 00:00:00 2001 From: KirisameMarisa Date: Sun, 18 Feb 2024 23:09:25 +0900 Subject: [PATCH] Add try-catch when processing data received --- .../Runtime/BocchiTrackerTcpSocket.cs | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/Plugins/Unity/project/Assets/BocchiTracker/Runtime/BocchiTrackerTcpSocket.cs b/Plugins/Unity/project/Assets/BocchiTracker/Runtime/BocchiTrackerTcpSocket.cs index 15283a0..68b7670 100644 --- a/Plugins/Unity/project/Assets/BocchiTracker/Runtime/BocchiTrackerTcpSocket.cs +++ b/Plugins/Unity/project/Assets/BocchiTracker/Runtime/BocchiTrackerTcpSocket.cs @@ -46,13 +46,13 @@ private IEnumerator Connect(string inIPAddress, int inPort) yield break; } - /// - /// Disconnects the socket. - /// - public void DisConnect() + + private void OnDestroy() { - if(IsConnect()) + if (IsConnect()) + { socket.Disconnect(true); + } } /// @@ -94,14 +94,18 @@ private IEnumerator ProcessReceiveData() var task = socket.ReceiveAsync(receivedData, SocketFlags.None); yield return new WaitUntil(() => task.IsCompleted); - int bytesRead = task.Result; - if (task.Result > 0) + try { - Console.WriteLine("ProcessReceiveData::Success, size=" + bytesRead); - List receivedDataList = new List(receivedData); - receivedDataList.RemoveRange(bytesRead, receivedData.Length - bytesRead); - ReciveCallback?.Invoke(receivedDataList); + int bytesRead = task.Result; + if (task.Result > 0) + { + Console.WriteLine("ProcessReceiveData::Success, size=" + bytesRead); + List receivedDataList = new List(receivedData); + receivedDataList.RemoveRange(bytesRead, receivedData.Length - bytesRead); + ReciveCallback?.Invoke(receivedDataList); + } } + catch { } } ///