| | |
| | | if (m_packageThread != null) |
| | | { |
| | | m_packageThread.Abort(); |
| | | m_packageThread = null; |
| | | } |
| | | } |
| | | catch (System.Exception ex) |
| | | { |
| | | Debug.Log(ex); |
| | | m_packageThread = null; |
| | | } |
| | | |
| | | try |
| | |
| | | { |
| | | if (m_packageThread != null) |
| | | { |
| | | m_packageThread.Abort(); |
| | | m_packageThread = null; |
| | | } |
| | | |
| | | m_LastPackageTime = DateTime.Now; |
| | | m_packageThread = new Thread(new ThreadStart(ReceiveInfo)); // 启动线程接收信息 |
| | | m_packageThread.IsBackground = true; |
| | | m_packageThread.Start(); |
| | | isStopTreading = false; |
| | | Debug.unityLogger.logEnabled = true; |
| | | Debug.Log("[ClientSocket][OnConnectSuccess] 连接成功,启动接收线程"); |
| | | if (m_packageThread != null) |
| | | { |
| | | Debug.LogWarning("[ClientSocket][OnConnectSuccess] m_packageThread已存在,先Abort"); |
| | | m_packageThread.Abort(); |
| | | m_packageThread = null; |
| | | } |
| | | |
| | | fragmentBytes = null; // 清除上次连接残留的碎片缓存 |
| | | m_LastPackageTime = DateTime.Now; |
| | | isStopTreading = false; |
| | | m_packageThread = new Thread(new ThreadStart(ReceiveInfo)); // 启动线程接收信息 |
| | | m_packageThread.IsBackground = true; |
| | | m_packageThread.Start(); |
| | | isStopTreading = false; |
| | | Debug.unityLogger.logEnabled = true; |
| | | Debug.Log("[ClientSocket][OnConnectSuccess] 连接成功,启动接收线程"); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | private void ReceiveInfo() |
| | | { |
| | | while (!isStopTreading) |
| | | { |
| | | try |
| | | { |
| | | var shutdown = false; |
| | | if (!m_Socket.Connected) |
| | | { |
| | | shutdown = true; |
| | | } |
| | | |
| | | if (!shutdown) |
| | | { |
| | | var dataLength = m_Socket.Receive(bufferBytes); |
| | | if (dataLength <= 0) |
| | | { |
| | | shutdown = true; |
| | | } |
| | | else |
| | | { |
| | | getBytesTotal += dataLength; |
| | | var bytes = new byte[dataLength]; |
| | | Array.Copy(bufferBytes, 0, bytes, 0, dataLength); |
| | | ReadInfo(bytes); |
| | | } |
| | | } |
| | | |
| | | if (shutdown) |
| | | { |
| | | isStopTreading = true; |
| | | m_Socket.Shutdown(SocketShutdown.Both); |
| | | m_Socket.Close(); |
| | | } |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | Debug.Log(e); |
| | | } |
| | | } |
| | | Debug.unityLogger.logEnabled = true; |
| | | Debug.Log("[ClientSocket][ReceiveInfo] 接收线程启动"); |
| | | while (!isStopTreading) |
| | |
| | | Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng); |
| | | break; |
| | | } |
| | | // 打印包头原始字节,便于排查 OverflowException |
| | | byte h0 = fixBytes[vReadIndex], h1 = fixBytes[vReadIndex + 1], |
| | | h2 = fixBytes[vReadIndex + 2], h3 = fixBytes[vReadIndex + 3], |
| | | h4 = fixBytes[vReadIndex + 4], h5 = fixBytes[vReadIndex + 5]; |
| | | vBodyLeng = BitConverter.ToInt32(fixBytes, vReadIndex + 2); |
| | | Debug.Log($"[ClientSocket][ReadInfo] vReadIndex={vReadIndex} vTotalLeng={vTotalLeng} vLeavingLeng={vLeavingLeng} header=[{h0:X2} {h1:X2} {h2:X2} {h3:X2} {h4:X2} {h5:X2}] vBodyLeng={vBodyLeng}"); |
| | | if (vBodyLeng < 0) |
| | | { |
| | | Debug.LogError($"[ClientSocket][ReadInfo] vBodyLeng异常({vBodyLeng}),丢弃剩余数据!fragmentBytes总长={fixBytes.Length} vReadIndex={vReadIndex}"); |
| | | fragmentBytes = null; |
| | | break; |
| | | } |
| | | if (vBodyLeng > vLeavingLeng - 6)// 未完整的包则留包 |
| | | { |
| | | Debug.Log($"[ClientSocket][ReadInfo] 包不完整,留包: vBodyLeng={vBodyLeng} vLeavingLeng={vLeavingLeng}"); |
| | | fragmentBytes = new byte[vLeavingLeng]; |
| | | Array.Copy(fixBytes, vReadIndex, fragmentBytes, 0, vLeavingLeng); |
| | | break; |