Skip to content

Commit eefcb5b

Browse files
committed
发现有一个线程没有被释放
1 parent bcc5d45 commit eefcb5b

File tree

5 files changed

+49
-45
lines changed

5 files changed

+49
-45
lines changed

MultiSEngine/Core/Adapter/BaseAdapter.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections;
44
using System.IO;
55
using System.Net.Sockets;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using TrProtocol;
89

@@ -44,7 +45,6 @@ public BaseAdapter(ClientData client, Socket connection)
4445
public abstract void SendPacket(Packet packet);
4546
public virtual BaseAdapter Start()
4647
{
47-
//Connection.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveMessage), null);
4848
Task.Run(RecieveLoop);
4949
Task.Run(ProcessPacketLoop);
5050
return this;
@@ -76,8 +76,11 @@ protected void ProcessPacketLoop()
7676
{
7777
while (!ShouldStop)
7878
{
79-
while (PacketPool.Count < 1)
80-
Task.Delay(1).Wait();
79+
if (PacketPool.Count < 1)
80+
{
81+
Thread.Sleep(1);
82+
continue;
83+
}
8184
var packet = PacketPool.Dequeue() as Packet;
8285
try
8386
{

MultiSEngine/Core/Net.cs

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public static void WatchConnecting()
5151
catch (Exception ex)
5252
{
5353
Logs.Error(ex);
54-
continue;
5554
}
5655
}
5756
}

MultiSEngine/Logs.cs

+25-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using MultiSEngine.DataStruct;
2+
using System;
23
using System.Collections;
34
using System.IO;
45
using System.Threading.Tasks;
@@ -10,52 +11,45 @@ public class Logs
1011
public static string LogPath => Path.Combine(Environment.CurrentDirectory, "Logs");
1112
public static string LogName => Path.Combine(LogPath, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
1213
public const ConsoleColor DefaultColor = ConsoleColor.Gray;
13-
public static void Text(object text, bool save = true)
14+
public static async void Text(object text, bool save = true)
1415
{
15-
LogAndSave(text, "[Log]", DefaultColor, save);
16+
await LogAndSave(text, "[Log]", DefaultColor, save);
1617
}
17-
public static void Info(object text, bool save = true)
18+
public static async void Info(object text, bool save = true)
1819
{
19-
LogAndSave(text, "[Info]", ConsoleColor.Yellow, save);
20+
await LogAndSave(text, "[Info]", ConsoleColor.Yellow, save);
2021
}
21-
public static void Error(object text, bool save = true)
22+
public static async void Error(object text, bool save = true)
2223
{
23-
LogAndSave(text, "[Error]", ConsoleColor.Red, save);
24+
await LogAndSave(text, "[Error]", ConsoleColor.Red, save);
2425
}
25-
public static void Warn(object text, bool save = true)
26+
public static async void Warn(object text, bool save = true)
2627
{
27-
LogAndSave(text, "[Warn]", ConsoleColor.DarkYellow, save);
28+
await LogAndSave(text, "[Warn]", ConsoleColor.DarkYellow, save);
2829
}
29-
public static void Success(object text, bool save = true)
30+
public static async void Success(object text, bool save = true)
3031
{
31-
LogAndSave(text, "[Success]", ConsoleColor.Green, save);
32+
await LogAndSave(text, "[Success]", ConsoleColor.Green, save);
3233
}
33-
private static Queue LogQueue;
34-
internal static void SaveLogTask()
34+
private static StreamWriter logSW;
35+
internal static void Init()
3536
{
36-
LogQueue ??= new();
37-
using var sw = File.AppendText(LogName);
38-
sw.AutoFlush = true;
39-
while (LogQueue != null)
40-
{
41-
while (LogQueue.Count < 1)
42-
Task.Delay(1).Wait();
43-
sw.WriteLine(LogQueue.Dequeue());
44-
if (!File.Exists(LogName))
45-
{
46-
LogQueue = null;
47-
return;
48-
}
49-
}
37+
if (!Directory.Exists(LogPath))
38+
Directory.CreateDirectory(LogPath);
39+
logSW = new(new FileStream(LogName, FileMode.OpenOrCreate));
5040
}
51-
public static void LogAndSave(object message, string prefix = "[Log]", ConsoleColor color = DefaultColor, bool save = true)
41+
public static async Task LogAndSave(object message, string prefix = "[Log]", ConsoleColor color = DefaultColor, bool save = true)
5242
{
53-
if (LogQueue is null)
54-
Task.Run(SaveLogTask);
43+
if (!File.Exists(LogName))
44+
{
45+
logSW?.Dispose();
46+
logSW = new(new FileStream(LogName, FileMode.OpenOrCreate)); ;
47+
}
5548
Console.ForegroundColor = color;
5649
Console.WriteLine($"{prefix} {message}");
57-
if (save) LogQueue.Enqueue($"{DateTime.Now:yyyy-MM-dd-HH:mm:ss} - {prefix} {message}");
5850
Console.ForegroundColor = DefaultColor;
51+
if (save)
52+
await logSW.WriteLineAsync($"{DateTime.Now:yyyy-MM-dd-HH:mm:ss} - {prefix} {message}");
5953
}
6054
}
6155
}
+13-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
using MultiSEngine.DataStruct;
22
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
35
using System.Reflection;
46
using System.Threading.Tasks;
7+
using System.Timers;
58

69
namespace MultiSEngine.Modules
710
{
811
internal class ConsoleManager
912
{
1013
public static readonly string Title = "[MultiSEngine]";
14+
private static readonly Timer ConsoleUpdateTimer = new()
15+
{
16+
Interval = 1000,
17+
AutoReset = true
18+
};
1119
[AutoInit]
1220
public static void Init()
1321
{
14-
Task.Run(Loop);
22+
ConsoleUpdateTimer.Elapsed += Loop;
23+
ConsoleUpdateTimer.Start();
1524
}
16-
private static void Loop()
25+
private static void Loop(object sender, ElapsedEventArgs e)
1726
{
18-
while (true)
19-
{
20-
Console.Title = $"{Title} {Data.Clients.Count} Online @{Config.Instance.ListenIP}:{Config.Instance.ListenPort} <V{Assembly.GetExecutingAssembly().GetName().Version}, for Terraria-1.4.3>";
21-
Task.Delay(1000).Wait();
22-
}
27+
Console.Title = $"{Title} {Data.Clients.Count} Online @{Config.Instance.ListenIP}:{Config.Instance.ListenPort} <V{Assembly.GetExecutingAssembly().GetName().Version}, for Terraria-1.4.3>";
28+
Task.Delay(1000).Wait();
2329
}
2430
}
2531
}

MultiSEngine/Program.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Linq;
55
using System.Reflection;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
namespace MultiSEngine
@@ -11,14 +12,15 @@ internal class Program
1112
{
1213
static void Main(string[] args)
1314
{
14-
Init();
15+
Logs.Init();
16+
AutoInit();
1517
while (!(Core.Command.HandleCommand(null, Console.ReadLine(), out var c, true) && !c))
16-
Task.Delay(1).Wait();
18+
Thread.Sleep(1);
1719
Close();
1820
Console.WriteLine("Bye!");
1921
Task.Delay(1000).Wait();
2022
}
21-
public static void Init()
23+
public static void AutoInit()
2224
{
2325
#if DEBUG
2426
Logs.Warn($"> MultiSEngine IS IN DEBUG MODE <");

0 commit comments

Comments
 (0)