From 3a1e7eb416c529d42fd403ec816cdd0ed2f43f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=88=E7=BA=BE?= Date: Fri, 1 Dec 2023 17:24:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client/Client.csproj | 8 +- Client/Packet/Message.cs | 10 +- Client/Program.cs | 82 +++--- Client/Utils/{Command.cs => CommandHelper.cs} | 18 +- Client/Utils/SimpleLogger.cs | 240 ------------------ ClientUI/MainWindow.xaml.cs | 12 +- ClientUI/Packet/Message.cs | 2 +- Server/Packet/Message.cs | 10 +- Server/Program.cs | 37 +-- Server/Server.csproj | 3 +- 10 files changed, 104 insertions(+), 318 deletions(-) rename Client/Utils/{Command.cs => CommandHelper.cs} (81%) delete mode 100644 Client/Utils/SimpleLogger.cs diff --git a/Client/Client.csproj b/Client/Client.csproj index 222957a..52ca3d7 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -4,12 +4,16 @@ Exe net8.0 enable - true - true + true ChatRoom 聊天室 ../Icon/favicon.ico true + + + + + diff --git a/Client/Packet/Message.cs b/Client/Packet/Message.cs index 972bd7e..2f921c4 100644 --- a/Client/Packet/Message.cs +++ b/Client/Packet/Message.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ChatRoom.Packet; public record struct Request @@ -11,4 +13,10 @@ public record struct Response public DateTime DateTime { get; set; } public string UserName { get; set; } public string Message { get; set; } -} \ No newline at end of file +} +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(Request))] +[JsonSerializable(typeof(Response))] +internal partial class SourceGenerationContext : JsonSerializerContext +{ +} diff --git a/Client/Program.cs b/Client/Program.cs index 32e606d..2c89a3b 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -1,5 +1,7 @@ using ChatRoom.Packet; using ChatRoom.Utils; +using Microsoft.Extensions.Logging; +using System.CommandLine; using System.Net.Sockets; using System.Reflection; using System.Text; @@ -13,33 +15,53 @@ internal class Client internal static string UserName { get; set; } - private static void Main(string[] args) + private static int Main(string[] args) { - // 进程互斥 - _ = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out bool isNotRunning); - if (!isNotRunning && !args.Contains("--multi") && !args.Contains("-m")) + Option multiOption = new( + name: "--multi", + description: "运行重复运行实例。"); + RootCommand rootCommand = new("Sample app for System.CommandLine"); + rootCommand.AddOption(multiOption); + rootCommand.SetHandler((multi) => { - throw new EntryPointNotFoundException("你只能同时运行一个聊天室实例!"); - } + // 进程互斥 + _ = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out bool isNotRunning); + if (!isNotRunning && !multi) + { + throw new EntryPointNotFoundException("你只能同时运行一个聊天室实例!"); + } + ChatMain(); + }, multiOption); + return rootCommand.Invoke(args); + } + + private static void ChatMain() + { // 修复中文输入输出 Console.InputEncoding = Encoding.GetEncoding(936); Console.OutputEncoding = Encoding.GetEncoding(936); - start: + using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole()); + ILogger logger = factory.CreateLogger("聊天室"); Console.Title = "聊天室"; - TcpClient = new(); - Console.Write("请输入服务器地址:"); - string ip = Console.ReadLine(); - try - { - TcpClient.Connect(ip, 19132); - } - catch (SocketException ex) + string ip; + while (true) { - SimpleLogger.Error($"连接至{ip}失败:{ex.Message}"); - goto start; + TcpClient = new(); + Console.Write("请输入服务器地址:"); + ip = Console.ReadLine(); + try + { + TcpClient.Connect(ip, 19132); + } + catch (SocketException ex) + { + logger.LogError("连接至{IP}失败:{Message}", ip, ex.Message); + continue; + } + break; } Console.Title = $"聊天室:{ip}"; - Dictionary lastMessage = new(); + Dictionary lastMessage = []; int lastOne = 0; Console.Clear(); ThreadPool.QueueUserWorkItem((_) => @@ -54,26 +76,26 @@ private static void Main(string[] args) { continue; } - stream.Read(bytes, 0, bytes.Length); + _ = stream.Read(bytes, 0, bytes.Length); } catch (IOException ex) { - SimpleLogger.Error($"已断开连接:{ex.Message}"); + logger.LogError("已断开连接:{Message}", ex.Message); int count = 0; while (!TcpClient.Connected) { TcpClient = new(); - SimpleLogger.Info($"重连中:{++count}"); + logger.LogInformation("重连中:{Count}", ++count); try { TcpClient.Connect(ip, 19132); } catch (SocketException ex1) { - SimpleLogger.Error($"重连至{ip}失败:{ex1.Message}"); + logger.LogError("重连至{IP}失败:{Message}", ip, ex1.Message); } } - SimpleLogger.Info($"已重连至{ip}"); + logger.LogInformation("已重连至{IP}", ip); continue; } string receivedString = Encoding.UTF8.GetString(bytes).Replace("\0", string.Empty); @@ -81,7 +103,7 @@ private static void Main(string[] args) { continue; } - Response data = JsonSerializer.Deserialize(receivedString); + Response data = JsonSerializer.Deserialize(receivedString, SourceGenerationContext.Default.Response); if (lastMessage.TryGetValue(data.UUID, out string value) && value == data.Message) { continue; @@ -100,7 +122,7 @@ private static void Main(string[] args) lastOne = data.UUID; } }); - SimpleLogger.Info($"已连接至{ip}"); + logger.LogInformation("已连接至{IP}", ip); while (true) { string line = Console.ReadLine(); @@ -108,15 +130,15 @@ private static void Main(string[] args) { continue; } - if (line.StartsWith("/")) + if (line.StartsWith('/')) { try { - Command.Process(line[1..].Split(' '), Command.Commands); + CommandHelper.Process(line[1..].Split(' '), CommandHelper.Commands); } catch (ArgumentException ex) { - SimpleLogger.Error($"命令运行失败:{ex.Message}"); + logger.LogError("命令运行失败:{Message}", ex.Message); } continue; } @@ -129,8 +151,8 @@ private static void Main(string[] args) { Message = line, UserName = UserName - })); + }, SourceGenerationContext.Default.Request)); stream.Write(bytes, 0, bytes.Length); } } -} \ No newline at end of file +} diff --git a/Client/Utils/Command.cs b/Client/Utils/CommandHelper.cs similarity index 81% rename from Client/Utils/Command.cs rename to Client/Utils/CommandHelper.cs index b2f459b..362a929 100644 --- a/Client/Utils/Command.cs +++ b/Client/Utils/CommandHelper.cs @@ -1,6 +1,6 @@ namespace ChatRoom.Utils; -internal static class Command +internal static class CommandHelper { internal static Dictionary Commands { get; } = new() { @@ -47,30 +47,30 @@ internal static class Command internal static void Process(IList args, Dictionary commands, int deep = 1) { string mainCommand = args[deep - 1].ToUpper(); - if (!commands.ContainsKey(mainCommand)) + if (!commands.TryGetValue(mainCommand, out CommandData command)) { throw new ArgumentException($"未知的命令:{args[deep - 1]}"); } - if (commands[mainCommand].Action is not null) + if (command.Action is not null) { List newArgs = new(args); if (args.Count - deep > 0) { newArgs.RemoveRange(0, deep); } - commands[mainCommand].Action(newArgs); + command.Action(newArgs); } - if (commands[mainCommand].SubCommands is not null) + if (command.SubCommands is not null) { if (args.Count <= deep) { - foreach ((string command, CommandData commandData) in commands[mainCommand].SubCommands) + foreach ((string commandName, CommandData commandData) in command.SubCommands) { - Console.WriteLine($" {command.ToLower()}\t{commandData.Description}"); + Console.WriteLine($" {commandName.ToLower()}\t{commandData.Description}"); } return; } - Process(args, commands[mainCommand].SubCommands, deep + 1); + Process(args, command.SubCommands, deep + 1); } } } @@ -79,4 +79,4 @@ internal record struct CommandData public string Description { get; set; } public Action> Action { get; set; } public Dictionary SubCommands { get; set; } -} \ No newline at end of file +} diff --git a/Client/Utils/SimpleLogger.cs b/Client/Utils/SimpleLogger.cs deleted file mode 100644 index c22e610..0000000 --- a/Client/Utils/SimpleLogger.cs +++ /dev/null @@ -1,240 +0,0 @@ -namespace ChatRoom.Utils; - -internal static class SimpleLogger -{ - #region Info - public static void Info() - { - Console.WriteLine(); - } - public static void Info(bool value) - { - Console.WriteLine(value); - } - public static void Info(char value) - { - Console.WriteLine(value); - } - public static void Info(char[] buffer) - { - Console.WriteLine(buffer); - } - public static void Info(char[] buffer, int index, int count) - { - Console.WriteLine(buffer, index, count); - } - public static void Info(decimal value) - { - Console.WriteLine(value); - } - public static void Info(double value) - { - Console.WriteLine(value); - } - public static void Info(float value) - { - Console.WriteLine(value); - } - public static void Info(int value) - { - Console.WriteLine(value); - } - public static void Info(uint value) - { - Console.WriteLine(value); - } - public static void Info(long value) - { - Console.WriteLine(value); - } - public static void Info(ulong value) - { - Console.WriteLine(value); - } - public static void Info(object value) - { - Console.WriteLine(value); - } - public static void Info(string value) - { - Console.WriteLine(value); - } - public static void Info(string format, object arg0) - { - Console.WriteLine(format, arg0); - } - public static void Info(string format, object arg0, object arg1) - { - Console.WriteLine(format, arg0, arg1); - } - public static void Info(string format, object arg0, object arg1, object arg2) - { - Console.WriteLine(format, arg0, arg1, arg2); - } - public static void Info(string format, object arg0, object arg1, object arg2, object arg3, __arglist) - { - ArgIterator argIterator = new ArgIterator(__arglist); - int num = argIterator.GetRemainingCount() + 4; - object[] array = new object[num]; - array[0] = arg0; - array[1] = arg1; - array[2] = arg2; - array[3] = arg3; - for (int i = 4; i < num; i++) - { - array[i] = TypedReference.ToObject(argIterator.GetNextArg()); - } - Console.WriteLine(format, array); - } - public static void Info(string format, params object[] arg) - { - Console.WriteLine(format, arg); - } - #endregion - #region Error - public static void Error() - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(); - Console.ForegroundColor = temp; - } - public static void Error(bool value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(char value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(char[] buffer) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(buffer); - Console.ForegroundColor = temp; - } - public static void Error(char[] buffer, int index, int count) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(buffer, index, count); - Console.ForegroundColor = temp; - } - public static void Error(decimal value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(double value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(float value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(int value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(uint value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(long value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(ulong value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(object value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(string value) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(value); - Console.ForegroundColor = temp; - } - public static void Error(string format, object arg0) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(format, arg0); - Console.ForegroundColor = temp; - } - public static void Error(string format, object arg0, object arg1) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(format, arg0, arg1); - Console.ForegroundColor = temp; - } - public static void Error(string format, object arg0, object arg1, object arg2) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(format, arg0, arg1, arg2); - Console.ForegroundColor = temp; - } - public static void Error(string format, object arg0, object arg1, object arg2, object arg3, __arglist) - { - ArgIterator argIterator = new ArgIterator(__arglist); - int num = argIterator.GetRemainingCount() + 4; - object[] array = new object[num]; - array[0] = arg0; - array[1] = arg1; - array[2] = arg2; - array[3] = arg3; - for (int i = 4; i < num; i++) - { - array[i] = TypedReference.ToObject(argIterator.GetNextArg()); - } - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(format, array); - Console.ForegroundColor = temp; - } - public static void Error(string format, params object[] arg) - { - ConsoleColor temp = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(format, arg); - Console.ForegroundColor = temp; - } - #endregion -} \ No newline at end of file diff --git a/ClientUI/MainWindow.xaml.cs b/ClientUI/MainWindow.xaml.cs index f273b2e..8148da1 100644 --- a/ClientUI/MainWindow.xaml.cs +++ b/ClientUI/MainWindow.xaml.cs @@ -21,7 +21,7 @@ public MainWindow() _ = new Mutex(true, Assembly.GetExecutingAssembly().GetName().Name, out bool isNotRunning); if (!isNotRunning) { - MessageBox.Show("你只能同时运行一个聊天室实例!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); + _ = MessageBox.Show("你只能同时运行一个聊天室实例!", "错误", MessageBoxButton.OK, MessageBoxImage.Error); throw new EntryPointNotFoundException("你只能同时运行一个聊天室实例!"); } InitializeComponent(); @@ -38,16 +38,16 @@ private void Connect(object sender, RoutedEventArgs e) } catch (SocketException ex) { - MessageBox.Show($"连接失败:{ex.Message}", "警告", MessageBoxButton.OK, MessageBoxImage.Warning); + _ = MessageBox.Show($"连接失败:{ex.Message}", "警告", MessageBoxButton.OK, MessageBoxImage.Warning); LoginGrid.IsEnabled = true; return; } LoginGrid.Visibility = Visibility.Hidden; RoomGrid.IsEnabled = true; RoomGrid.Visibility = Visibility.Visible; - Dictionary lastMessage = new(); + Dictionary lastMessage = []; int lastOne = 0; - ThreadPool.QueueUserWorkItem((_) => + _ = ThreadPool.QueueUserWorkItem((_) => { while (true) { @@ -59,7 +59,7 @@ private void Connect(object sender, RoutedEventArgs e) { continue; } - stream.Read(bytes, 0, bytes.Length); + _ = stream.Read(bytes, 0, bytes.Length); } catch (IOException ex) { @@ -174,4 +174,4 @@ private void WindowSizeChanged(object sender, SizeChangedEventArgs e) ChatBox.Height = e.NewSize.Height - 140 < 0 ? 0 : e.NewSize.Height - 140; } } -} \ No newline at end of file +} diff --git a/ClientUI/Packet/Message.cs b/ClientUI/Packet/Message.cs index 972bd7e..6ddcb5e 100644 --- a/ClientUI/Packet/Message.cs +++ b/ClientUI/Packet/Message.cs @@ -11,4 +11,4 @@ public record struct Response public DateTime DateTime { get; set; } public string UserName { get; set; } public string Message { get; set; } -} \ No newline at end of file +} diff --git a/Server/Packet/Message.cs b/Server/Packet/Message.cs index 972bd7e..2f921c4 100644 --- a/Server/Packet/Message.cs +++ b/Server/Packet/Message.cs @@ -1,3 +1,5 @@ +using System.Text.Json.Serialization; + namespace ChatRoom.Packet; public record struct Request @@ -11,4 +13,10 @@ public record struct Response public DateTime DateTime { get; set; } public string UserName { get; set; } public string Message { get; set; } -} \ No newline at end of file +} +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(Request))] +[JsonSerializable(typeof(Response))] +internal partial class SourceGenerationContext : JsonSerializerContext +{ +} diff --git a/Server/Program.cs b/Server/Program.cs index 2d1426e..c26edd6 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -4,24 +4,15 @@ using System.Text; using System.Text.Json; -Dictionary clients = new(); -int argCount = 0, maxCount = 0; -System.Timers.Timer timer = new(1000); -timer.Elapsed += (_, _) => -{ - Console.Title = $"聊天室服务器  {argCount}/{maxCount}"; - argCount = 0; -}; -timer.Start(); +Dictionary clients = []; TcpListener listenner = new(IPAddress.Any, 19132); listenner.Start(); -Console.WriteLine($"开始监听{listenner.LocalEndpoint}"); while (true) { TcpClient client = listenner.AcceptTcpClient(); string clientIP = client.Client.RemoteEndPoint.ToString()[..client.Client.RemoteEndPoint.ToString().LastIndexOf(':')]; clients.Add(client, clientIP.GetHashCode()); - ThreadPool.QueueUserWorkItem((_) => + _ = ThreadPool.QueueUserWorkItem((_) => { while (true) { @@ -33,11 +24,11 @@ { continue; } - stream.Read(bytes, 0, bytes.Length); - } catch (IOException ex) + _ = stream.Read(bytes, 0, bytes.Length); + } + catch (IOException) { - Console.WriteLine($"{client.Client.RemoteEndPoint}已断开连接:{ex}"); - clients.Remove(client); + _ = clients.Remove(client); return; } string receivedString = Encoding.UTF8.GetString(bytes).Replace("\0", string.Empty); @@ -45,14 +36,14 @@ { continue; } - Request data = JsonSerializer.Deserialize(receivedString); + Request data = JsonSerializer.Deserialize(receivedString, SourceGenerationContext.Default.Request); byte[] packetBytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(new Response() { UUID = clients[client], DateTime = DateTime.Now, Message = data.Message, UserName = data.UserName - })); + }, SourceGenerationContext.Default.Response)); foreach (TcpClient otherClient in clients.Keys) { try @@ -63,19 +54,13 @@ continue; } stream.Write(packetBytes, 0, packetBytes.Length); - } catch (IOException ex) + } + catch (IOException) { - Console.WriteLine($"{client.Client.RemoteEndPoint}已断开连接:{ex}"); - clients.Remove(client); + _ = clients.Remove(client); continue; } } - argCount++; - if (argCount > maxCount) - { - maxCount = argCount; - } } }); - Console.WriteLine($"{client.Client.RemoteEndPoint}已连接"); } diff --git a/Server/Server.csproj b/Server/Server.csproj index 6bff58a..1a99ace 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -4,8 +4,7 @@ Exe net8.0 enable - true - true + true ChatRoom ChatRoom.$(MSBuildProjectName) true