private void _Unpack(byte[] data) { if (_preBuffer == null) { _preBuffer = data; } else { _preBuffer = ByteTool.Concat(_preBuffer, data); } var head = ByteTool.Cut(_preBuffer, 0, 4); if (head == null) { return; } var size = System.BitConverter.ToInt32(head, 0); var body = ByteTool.Cut(_preBuffer, 4, size); if (body == null) { return; } _DealData(body); var more = ByteTool.Cut(_preBuffer, size, _preBuffer.Length); _preBuffer = null; if (more != null) { _Unpack(more); } }
private void _OnReadResult(System.IAsyncResult ar) { if (_tcpClient == null || !_tcpClient.Connected) { return; } var stream = _tcpClient.GetStream(); int index = stream.EndRead(ar); _Unpack(ByteTool.Cut(_buffer, 0, index)); stream.BeginRead(_buffer, 0, 1024, _OnReadResult, null); }