private void ResponseFragment(EndPoint endPoint, IReadStream readStream, PacketHead packetHead) { if (!_connections.TryGetValue(endPoint, out var socketService)) { Logger.Error($"{endPoint}连接没有添加!"); return; } using (var writeStream = PoolAllocator <IWriteStream> .GetObject()) { writeStream.ShiftRight(readStream.RightPeek(_nextHeadOffset)); Acknowledge(socketService, SendOption.Acknowledge | SendOption.Fragment, packetHead, writeStream); } SocketStatistics.LogAcknowledgementSend(); SocketStatistics.LogFragmentedReceive(); lock (socketService) { // 过滤已处理的之前包 if (socketService.RecvCounter >= packetHead.PacketId) { Logger.Warn($"重复包messageId:{packetHead.PacketId} fragmentId:{packetHead.FragmentId}丢弃!"); return; } var byteFragment = readStream.ToByteFragment(); if (!socketService.Packets.TryGetValue(packetHead.PacketId, out var result)) { // 分包第一次处理 result = new Recorder() { Count = packetHead.TotalFragments, Flag = 0, ReadStream = PoolAllocator <IReadStream> .GetObject(), }; socketService.Packets.Add(packetHead.PacketId, result); } var readBuffer = result.ReadStream.GetReadBuffer(); // 过滤已经处理的分包 if (packetHead.TotalFragments != 1 && (result.Flag & (((ulong)1) << packetHead.FragmentId)) > 0) { Logger.Warn("过滤已经处理的分包!"); return; } Array.Copy(byteFragment.Buffer, byteFragment.Offset, readBuffer.Buffer, readBuffer.Offset + packetHead.FragmentId * packetHead.TotalBytes, byteFragment.Count); result.Flag |= ((ulong)1) << packetHead.FragmentId; socketService.Packets[packetHead.PacketId] = result; Logger.Debug($"MessageID:{packetHead.PacketId} 段数:{packetHead.FragmentId} 总段数:{packetHead.TotalFragments} 当前全部包数:" + socketService.Packets.Count); while (socketService.Packets.Count != 0) { var next = socketService.RecvCounter + 1; if (!socketService.Packets.TryGetValue(next, out var first)) { break; } if (!first.IsComplete()) { break; } Logger.Debug("MessageID:" + next + " 包数:" + socketService.Packets.Count + " 完成!"); socketService.RecvIncrement(); socketService.Packets.Remove(next); socketService.OnRead(first.ReadStream); first.ReadStream.Dispose(); } } SocketStatistics.LogDataBytesReceived(packetHead.TotalBytes); SocketStatistics.LogTotalBytesReceived(packetHead.Length); SocketStatistics.LogReliableReceive(); }
private void ResponseFragment(IPEndPoint endPoint, IReadStream readStream, PacketHead packetHead) { if (!_connections.TryGetValue(endPoint, out var socketService)) { Logger.Error($"{endPoint}连接没有添加!"); return; } // 应答包,可能会耗流量 using (var writeStream = PoolAllocator <IWriteStream> .GetObject()) { writeStream.ShiftRight(readStream.RightPeek(_nextHeadOffset)); Acknowledge(socketService, SendOption.Acknowledge | SendOption.Fragment, packetHead, writeStream); } lock (socketService) { // 过滤已处理的之前包 if (socketService.RecvCounter >= packetHead.PacketId) { Logger.Warn($"重复包messageId:{packetHead.PacketId} fragmentId:{packetHead.FragmentId}丢弃!"); return; } var byteFragment = readStream.ToByteFragment(); if (!socketService.Packets.TryGetValue(packetHead.PacketId, out var result)) { // 分包第一次处理 result = new Recorder() { Count = packetHead.TotalFragments, Flag = 0, ReadStream = PoolAllocator <IReadStream> .GetObject(), }; socketService.Packets.Add(packetHead.PacketId, result); } var readBuffer = result.ReadStream.GetReadBuffer(); Logger.Debug(packetHead.PacketId + " " + packetHead.FragmentId); // 过滤已经处理的分包 if (packetHead.TotalFragments != 1 && (result.Flag & (((ulong)1) << packetHead.FragmentId)) > 0) { Logger.Warn("过滤已经处理的分包!"); return; } Array.Copy(byteFragment.Buffer, byteFragment.Offset, readBuffer.Buffer, readBuffer.Offset + packetHead.FragmentId * packetHead.TotalBytes, byteFragment.Count); result.Flag |= ((ulong)1) << packetHead.FragmentId; socketService.Packets[packetHead.PacketId] = result; Logger.Debug("------------------------------------"); while (socketService.Packets.Count != 0) { var next = socketService.RecvCounter + 1; if (!socketService.Packets.TryGetValue(next, out var first)) { Logger.Warn($"跳包!期望ID:{next} 实际ID:{packetHead.PacketId}"); break; } if (!first.IsComplete()) { break; } socketService.RecvIncrement(); socketService.Packets.Remove(next); socketService.OnRead(first.ReadStream); first.ReadStream.Dispose(); } } }