Skip to content

Commit

Permalink
Properly dispose of enumerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Nov 11, 2024
1 parent ad5d966 commit 1cdd467
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
4 changes: 2 additions & 2 deletions HKMP/Networking/Packet/AddonPacketData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class AddonPacketData {
/// <summary>
/// Enumerator to go over each packet ID in the packet ID space of this addon.
/// </summary>
public IEnumerator<byte> PacketIdEnumerator {
public IEnumerable<byte> PacketIdEnumerable {
get {
if (_packetIdArray == null) {
// Create an array containing all possible IDs for this addon
Expand All @@ -31,7 +31,7 @@ public IEnumerator<byte> PacketIdEnumerator {
}

// Return a fresh enumerator for the ID space
return ((IEnumerable<byte>) _packetIdArray).GetEnumerator();
return _packetIdArray;
}
}

Expand Down
20 changes: 11 additions & 9 deletions HKMP/Networking/Packet/UpdatePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ private bool WritePacketData(
Dictionary<T, IPacketData> packetData
) {
var enumValues = (T[]) Enum.GetValues(typeof(T));
var enumerator = ((IEnumerable<T>) enumValues).GetEnumerator();
var packetIdSize = (byte) enumValues.Length;

return WritePacketData(
packet,
packetData,
enumerator,
enumValues,
packetIdSize
);
}
Expand All @@ -170,7 +169,7 @@ AddonPacketData addonPacketData
) => WritePacketData(
packet,
addonPacketData.PacketData,
addonPacketData.PacketIdEnumerator,
addonPacketData.PacketIdEnumerable,
addonPacketData.PacketIdSize
);

Expand All @@ -179,21 +178,22 @@ AddonPacketData addonPacketData
/// </summary>
/// <param name="packet">The packet to write into.</param>
/// <param name="packetData">The dictionary containing packet data to write in the packet.</param>
/// <param name="keyEnumerator">An enumerator that enumerates over all possible keys in the dictionary.</param>
/// <param name="keyEnumerable">An enumerator that enumerates over all possible keys in the dictionary.</param>
/// <param name="keySpaceSize">The exact size of the key space.</param>
/// <typeparam name="TKey">Dictionary key parameter and enumerator parameter.</typeparam>
/// <returns>true if any of the data written was reliable; otherwise false.</returns>
private bool WritePacketData<TKey>(
Packet packet,
Dictionary<TKey, IPacketData> packetData,
IEnumerator<TKey> keyEnumerator,
IEnumerable<TKey> keyEnumerable,
byte keySpaceSize
) {
// Keep track of the bit flag in an unsigned long, which is the largest integer implicit type allowed
ulong idFlag = 0;
// Also keep track of the value of the current bit in an unsigned long
ulong currentTypeValue = 1;

var keyEnumerator = keyEnumerable.GetEnumerator();
while (keyEnumerator.MoveNext()) {
var key = keyEnumerator.Current;

Expand Down Expand Up @@ -235,6 +235,8 @@ byte keySpaceSize
}
}
}

keyEnumerator.Dispose();

return containsReliableData;
}
Expand Down Expand Up @@ -282,7 +284,7 @@ Dictionary<byte, AddonPacketData> addonDataDict
// If the addon data writing throws an exception, we skip it entirely and since we
// wrote it in a separate packet, it has no impact on the regular packet
Logger.Debug($"Addon with ID {addonId} has thrown an exception while writing addon packet data:\n{e}");
// We decrease the count of addon packet datas we write, so we know how many are actually in
// We decrease the count of addon packet data's we write, so we know how many are actually in
// final packet
addonPacketDataCount--;
continue;
Expand Down Expand Up @@ -420,7 +422,7 @@ Dictionary<byte, IPacketData> packetData
/// </summary>
/// <param name="packet">The raw packet instance to read from.</param>
/// <param name="addonDataDict">The dictionary for all addon data to write the read data into.</param>
/// <exception cref="Exception">Thrown if the any part of reading the data throws.</exception>
/// <exception cref="Exception">Thrown if any part of reading the data throws.</exception>
private void ReadAddonDataDict(
Packet packet,
Dictionary<byte, AddonPacketData> addonDataDict
Expand Down Expand Up @@ -480,7 +482,7 @@ public Packet CreatePacket() {
// contains reliable data now
_containsReliableData = WritePacketData(packet, _normalPacketData);

// Put the length of the resend data as a ushort in the packet
// Put the length of the resend data as an ushort in the packet
var resendLength = (ushort) _resendPacketData.Count;
if (_resendPacketData.Count > ushort.MaxValue) {
resendLength = ushort.MaxValue;
Expand Down Expand Up @@ -510,7 +512,7 @@ public Packet CreatePacket() {

_containsReliableData |= WriteAddonDataDict(packet, _addonPacketData);

// Put the length of the addon resend data as a ushort in the packet
// Put the length of the addon resend data as an ushort in the packet
resendLength = (ushort) _resendAddonPacketData.Count;
if (_resendAddonPacketData.Count > ushort.MaxValue) {
resendLength = ushort.MaxValue;
Expand Down

0 comments on commit 1cdd467

Please sign in to comment.