From e8b7b8408c8f451cc0b9b01c87f963bac5758119 Mon Sep 17 00:00:00 2001 From: Sakurai Date: Sat, 1 Jun 2024 20:40:56 +0900 Subject: [PATCH] Fix: memory usage --- ProtocolLib/PacketIO/PacketFile.cs | 8 +++++++- ProtocolLib/PacketIO/TempFile.cs | 21 +++++++++++++++++++++ ProtocolLib/ProtocolLib.sln | 25 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 ProtocolLib/PacketIO/TempFile.cs create mode 100644 ProtocolLib/ProtocolLib.sln diff --git a/ProtocolLib/PacketIO/PacketFile.cs b/ProtocolLib/PacketIO/PacketFile.cs index ad6791b..ce82d89 100644 --- a/ProtocolLib/PacketIO/PacketFile.cs +++ b/ProtocolLib/PacketIO/PacketFile.cs @@ -3,12 +3,14 @@ namespace PacketIO; public class PacketFile : IPacketFile { private string fileName; + private TempFile? tempFile; private Stream stream; public PacketFile(string fileName) { this.fileName = fileName; - this.stream = new MemoryStream(); + this.tempFile = new TempFile(); + this.stream = tempFile.Open(FileMode.Open); } public PacketFile(string fileName, Stream stream) @@ -56,5 +58,9 @@ public class PacketFile : IPacketFile public void Dispose() { this.stream.Dispose(); + if (this.tempFile is object) + { + this.tempFile.Dispose(); + } } } diff --git a/ProtocolLib/PacketIO/TempFile.cs b/ProtocolLib/PacketIO/TempFile.cs new file mode 100644 index 0000000..688ab8a --- /dev/null +++ b/ProtocolLib/PacketIO/TempFile.cs @@ -0,0 +1,21 @@ +namespace PacketIO; + +public class TempFile : IDisposable +{ + public string FileName { get; } + + public TempFile() + { + FileName = Path.GetTempFileName(); + } + + public Stream Open(FileMode mode) + { + return new FileStream(FileName, mode); + } + + public void Dispose() + { + File.Delete(FileName); + } +} diff --git a/ProtocolLib/ProtocolLib.sln b/ProtocolLib/ProtocolLib.sln new file mode 100644 index 0000000..1939546 --- /dev/null +++ b/ProtocolLib/ProtocolLib.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProtocolLib", "ProtocolLib.csproj", "{BB1F2133-E83C-4975-A685-D2DCE2FC24C9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BB1F2133-E83C-4975-A685-D2DCE2FC24C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BB1F2133-E83C-4975-A685-D2DCE2FC24C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BB1F2133-E83C-4975-A685-D2DCE2FC24C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BB1F2133-E83C-4975-A685-D2DCE2FC24C9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {AD8CE391-A853-498C-A2E6-7A83701164D4} + EndGlobalSection +EndGlobal