From 58eec5e094a31d043d3d26e9cdcaceb2c96038e5 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Fri, 26 Apr 2024 17:51:55 +0800 Subject: [PATCH] GUI --- Client-Cli/Program.cs | 32 +- Client-GUI/Client-GUI.csproj | 20 + Client-GUI/LoginUI.Designer.cs | 85 ++++ Client-GUI/LoginUI.cs | 45 ++ Client-GUI/LoginUI.resx | 120 +++++ Client-GUI/MainUI.Designer.cs | 417 ++++++++++++++++++ Client-GUI/MainUI.cs | 347 +++++++++++++++ Client-GUI/MainUI.resx | 120 +++++ Client-GUI/Program.cs | 28 ++ .../PublishProfiles/FolderProfile.pubxml | 18 + Client-GUI/StaticComm.cs | 13 + ClientCore/App.cs | 17 +- ClientCore/Enum/CommEnum.cs | 8 + ClientCore/Event/EEvent.cs | 10 + ClientCore/Manager/ClientManager.cs | 84 +++- ClientCore/Manager/LogManager.cs | 35 +- ClientCore/Manager/P2PFile.cs | 29 +- ClientCore/Manager/P2PTcp.cs | 14 +- ClientCore/Manager/UserMgr.cs | 19 +- ClientCore/Network/NetworkHelper.cs | 3 +- ClientCore/Network/NetworkP2PHelper.cs | 4 +- .../PublishProfiles/FolderProfile.pubxml | 13 + HaoYueTunnel.sln | 8 +- NetLib/HaoYueNet.ClientNetwork.dll | Bin 20992 -> 20992 bytes .../PublishProfiles/FolderProfile.pubxml | 13 + 25 files changed, 1457 insertions(+), 45 deletions(-) create mode 100644 Client-GUI/Client-GUI.csproj create mode 100644 Client-GUI/LoginUI.Designer.cs create mode 100644 Client-GUI/LoginUI.cs create mode 100644 Client-GUI/LoginUI.resx create mode 100644 Client-GUI/MainUI.Designer.cs create mode 100644 Client-GUI/MainUI.cs create mode 100644 Client-GUI/MainUI.resx create mode 100644 Client-GUI/Program.cs create mode 100644 Client-GUI/Properties/PublishProfiles/FolderProfile.pubxml create mode 100644 Client-GUI/StaticComm.cs create mode 100644 ClientCore/Properties/PublishProfiles/FolderProfile.pubxml create mode 100644 Server/Properties/PublishProfiles/FolderProfile.pubxml diff --git a/Client-Cli/Program.cs b/Client-Cli/Program.cs index fbb1e2d..2ca7b63 100644 --- a/Client-Cli/Program.cs +++ b/Client-Cli/Program.cs @@ -1,17 +1,25 @@ using ClientCore; using ClientCore.Data; +using ClientCore.Enum; using ClientCore.Event; -App.Init("127.0.0.1", 23846, 23847); +App.Init("127.0.0.1", 23846, 23847, OnNetLog); +//App.Init("main.axibug.com", 23846, 23847, OnNetLog); //注册事件 EventSystem.Instance.RegisterEvent(EEvent.UserLogin, OnUserLogin); EventSystem.Instance.RegisterEvent(EEvent.TcpTunnelHelloResp, OnTcpTunnelHelloResp); -EventSystem.Instance.RegisterEvent(EEvent.UserJoin, OnUserJoin); -EventSystem.Instance.RegisterEvent(EEvent.UserLeave, OnUserLeave); +EventSystem.Instance.RegisterEvent(EEvent.TcpTunnelP2PStateUpdate, OnTcpTunnelP2PStateUpdate); +EventSystem.Instance.RegisterEvent(EEvent.UserJoin, OnUserJoin); +EventSystem.Instance.RegisterEvent(EEvent.UserLeave, OnUserLeave); EventSystem.Instance.RegisterEvent(EEvent.OnChatMsg, OnChatMsg); EventSystem.Instance.RegisterEvent(EEvent.OnP2PChatMsg, OnP2PChatMsg); +App.Connect(); + +//设置自动同意接收 +App.p2pFile.bAutoRecv = true; + while (true) { string CommandStr = Console.ReadLine(); @@ -91,6 +99,12 @@ while (true) } } + +static void OnNetLog(int LogLevel, string msg) +{ + Console.WriteLine(msg); +} + void OnUserLogin() { Console.WriteLine($"[User]登录成功"); @@ -104,12 +118,18 @@ void OnTcpTunnelHelloResp() Console.WriteLine($"[TcpTunnel]TcpTunnelHelloResp"); } -void OnUserJoin(long UID) + +void OnTcpTunnelP2PStateUpdate(long UID) { - Console.WriteLine($"[User]用户{UID}上线"); + Console.WriteLine($"[TcpTunnel]OnTcpTunnelP2PStateUpdate"); } -void OnUserLeave(long UID) +void OnUserJoin(long UID, string NickName) +{ + Console.WriteLine($"[User]用户{NickName}上线"); +} + +void OnUserLeave(long UID, string NickName) { Console.WriteLine($"[User]用户{UID}下线"); } diff --git a/Client-GUI/Client-GUI.csproj b/Client-GUI/Client-GUI.csproj new file mode 100644 index 0000000..1043fbe --- /dev/null +++ b/Client-GUI/Client-GUI.csproj @@ -0,0 +1,20 @@ + + + + WinExe + net8.0-windows + Client_GUI + enable + true + enable + + + + + + + + + + + \ No newline at end of file diff --git a/Client-GUI/LoginUI.Designer.cs b/Client-GUI/LoginUI.Designer.cs new file mode 100644 index 0000000..f59a524 --- /dev/null +++ b/Client-GUI/LoginUI.Designer.cs @@ -0,0 +1,85 @@ +namespace Client_GUI +{ + partial class LoginUI + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + tb_Name = new TextBox(); + label1 = new Label(); + btn_Login = new Button(); + SuspendLayout(); + // + // tb_Name + // + tb_Name.Location = new Point(50, 12); + tb_Name.Name = "tb_Name"; + tb_Name.Size = new Size(128, 23); + tb_Name.TabIndex = 0; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(12, 15); + label1.Name = "label1"; + label1.Size = new Size(32, 17); + label1.TabIndex = 1; + label1.Text = "昵称"; + // + // btn_Login + // + btn_Login.Location = new Point(12, 46); + btn_Login.Name = "btn_Login"; + btn_Login.Size = new Size(166, 23); + btn_Login.TabIndex = 2; + btn_Login.Text = "登录"; + btn_Login.UseVisualStyleBackColor = true; + btn_Login.Click += btn_Login_Click; + // + // LoginUI + // + AutoScaleDimensions = new SizeF(7F, 17F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(191, 81); + Controls.Add(btn_Login); + Controls.Add(label1); + Controls.Add(tb_Name); + FormBorderStyle = FormBorderStyle.FixedToolWindow; + Name = "LoginUI"; + Text = "Login"; + FormClosing += LoginUI_FormClosing; + Shown += LoginUI_Shown; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox tb_Name; + private Label label1; + private Button btn_Login; + } +} \ No newline at end of file diff --git a/Client-GUI/LoginUI.cs b/Client-GUI/LoginUI.cs new file mode 100644 index 0000000..0dc8a3a --- /dev/null +++ b/Client-GUI/LoginUI.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Client_GUI +{ + public partial class LoginUI : Form + { + public LoginUI() + { + InitializeComponent(); + } + + private void btn_Login_Click(object sender, EventArgs e) + { + StaticComm.bClickLogin = false; + + if (string.IsNullOrEmpty(tb_Name.Text)) + { + MessageBox.Show("不可以为空"); + return; + } + + StaticComm.bClickLogin = true; + StaticComm.LoginName = tb_Name.Text; + this.Close(); + } + + private void LoginUI_Shown(object sender, EventArgs e) + { + StaticComm.bClickLogin = false; + } + + private void LoginUI_FormClosing(object sender, FormClosingEventArgs e) + { + + } + } +} diff --git a/Client-GUI/LoginUI.resx b/Client-GUI/LoginUI.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Client-GUI/LoginUI.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Client-GUI/MainUI.Designer.cs b/Client-GUI/MainUI.Designer.cs new file mode 100644 index 0000000..c4d41f3 --- /dev/null +++ b/Client-GUI/MainUI.Designer.cs @@ -0,0 +1,417 @@ +namespace Client_GUI +{ + partial class MainUI + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + listbox_log = new ListBox(); + listBox_OnlineUser = new ListBox(); + textBox_chatsendmsg = new TextBox(); + btn_chatsend = new Button(); + tabControl_Main = new ReaLTaiizor.Controls.MetroTabControl(); + tabPage1 = new TabPage(); + listBox_AllChatMsg = new ListBox(); + tabPage2 = new TabPage(); + label_P2P_SendFile_Pr = new ReaLTaiizor.Controls.MaterialLabel(); + metroLabel5 = new ReaLTaiizor.Controls.MetroLabel(); + label_P2P_SendFile_State = new ReaLTaiizor.Controls.MaterialLabel(); + metroLabel4 = new ReaLTaiizor.Controls.MetroLabel(); + btn_P2P_SendFile = new ReaLTaiizor.Controls.MaterialButton(); + foxLabel2 = new ReaLTaiizor.Controls.FoxLabel(); + foxLabel1 = new ReaLTaiizor.Controls.FoxLabel(); + label_P2P_SendFilePath = new ReaLTaiizor.Controls.MaterialLabel(); + metroLabel3 = new ReaLTaiizor.Controls.MetroLabel(); + btn_P2P_ChangeSendFile = new ReaLTaiizor.Controls.MaterialButton(); + label_P2P_State = new ReaLTaiizor.Controls.MaterialLabel(); + metroLabel2 = new ReaLTaiizor.Controls.MetroLabel(); + label_P2P_TargetUName = new ReaLTaiizor.Controls.MaterialLabel(); + metroLabel1 = new ReaLTaiizor.Controls.MetroLabel(); + foxLabel3 = new ReaLTaiizor.Controls.FoxLabel(); + tabControl_Main.SuspendLayout(); + tabPage1.SuspendLayout(); + tabPage2.SuspendLayout(); + SuspendLayout(); + // + // listbox_log + // + listbox_log.FormattingEnabled = true; + listbox_log.ItemHeight = 17; + listbox_log.Location = new Point(12, 335); + listbox_log.Name = "listbox_log"; + listbox_log.Size = new Size(776, 106); + listbox_log.TabIndex = 0; + // + // listBox_OnlineUser + // + listBox_OnlineUser.FormattingEnabled = true; + listBox_OnlineUser.ItemHeight = 17; + listBox_OnlineUser.Location = new Point(12, 46); + listBox_OnlineUser.Name = "listBox_OnlineUser"; + listBox_OnlineUser.Size = new Size(160, 276); + listBox_OnlineUser.TabIndex = 1; + listBox_OnlineUser.DoubleClick += listBox_OnlineUser_DoubleClick; + // + // textBox_chatsendmsg + // + textBox_chatsendmsg.Location = new Point(3, 234); + textBox_chatsendmsg.Name = "textBox_chatsendmsg"; + textBox_chatsendmsg.Size = new Size(485, 23); + textBox_chatsendmsg.TabIndex = 3; + // + // btn_chatsend + // + btn_chatsend.Location = new Point(494, 234); + btn_chatsend.Name = "btn_chatsend"; + btn_chatsend.Size = new Size(102, 23); + btn_chatsend.TabIndex = 4; + btn_chatsend.Text = "发送"; + btn_chatsend.UseVisualStyleBackColor = true; + btn_chatsend.Click += btn_chatsend_Click; + // + // tabControl_Main + // + tabControl_Main.AnimateEasingType = ReaLTaiizor.Enum.Metro.EasingType.CubeOut; + tabControl_Main.AnimateTime = 200; + tabControl_Main.BackgroundColor = Color.White; + tabControl_Main.Controls.Add(tabPage1); + tabControl_Main.Controls.Add(tabPage2); + tabControl_Main.ControlsVisible = true; + tabControl_Main.IsDerivedStyle = true; + tabControl_Main.ItemSize = new Size(100, 38); + tabControl_Main.Location = new Point(178, 12); + tabControl_Main.MCursor = Cursors.Hand; + tabControl_Main.Name = "tabControl_Main"; + tabControl_Main.SelectedIndex = 0; + tabControl_Main.SelectedTextColor = Color.White; + tabControl_Main.Size = new Size(610, 310); + tabControl_Main.SizeMode = TabSizeMode.Fixed; + tabControl_Main.Speed = 100; + tabControl_Main.Style = ReaLTaiizor.Enum.Metro.Style.Light; + tabControl_Main.StyleManager = null; + tabControl_Main.TabIndex = 6; + tabControl_Main.ThemeAuthor = "Taiizor"; + tabControl_Main.ThemeName = "MetroLight"; + tabControl_Main.UnselectedTextColor = Color.Gray; + // + // tabPage1 + // + tabPage1.Controls.Add(listBox_AllChatMsg); + tabPage1.Controls.Add(btn_chatsend); + tabPage1.Controls.Add(textBox_chatsendmsg); + tabPage1.Location = new Point(4, 42); + tabPage1.Name = "tabPage1"; + tabPage1.Padding = new Padding(3); + tabPage1.Size = new Size(602, 264); + tabPage1.TabIndex = 0; + tabPage1.Text = "聊天室"; + tabPage1.UseVisualStyleBackColor = true; + // + // listBox_AllChatMsg + // + listBox_AllChatMsg.FormattingEnabled = true; + listBox_AllChatMsg.ItemHeight = 17; + listBox_AllChatMsg.Location = new Point(3, 3); + listBox_AllChatMsg.Name = "listBox_AllChatMsg"; + listBox_AllChatMsg.Size = new Size(593, 225); + listBox_AllChatMsg.TabIndex = 0; + // + // tabPage2 + // + tabPage2.Controls.Add(label_P2P_SendFile_Pr); + tabPage2.Controls.Add(metroLabel5); + tabPage2.Controls.Add(label_P2P_SendFile_State); + tabPage2.Controls.Add(metroLabel4); + tabPage2.Controls.Add(btn_P2P_SendFile); + tabPage2.Controls.Add(foxLabel2); + tabPage2.Controls.Add(foxLabel1); + tabPage2.Controls.Add(label_P2P_SendFilePath); + tabPage2.Controls.Add(metroLabel3); + tabPage2.Controls.Add(btn_P2P_ChangeSendFile); + tabPage2.Controls.Add(label_P2P_State); + tabPage2.Controls.Add(metroLabel2); + tabPage2.Controls.Add(label_P2P_TargetUName); + tabPage2.Controls.Add(metroLabel1); + tabPage2.Location = new Point(4, 42); + tabPage2.Name = "tabPage2"; + tabPage2.Padding = new Padding(3); + tabPage2.Size = new Size(602, 264); + tabPage2.TabIndex = 1; + tabPage2.Text = "P2P传文件"; + tabPage2.UseVisualStyleBackColor = true; + // + // label_P2P_SendFile_Pr + // + label_P2P_SendFile_Pr.AutoSize = true; + label_P2P_SendFile_Pr.Depth = 0; + label_P2P_SendFile_Pr.Font = new Font("Roboto", 14F, FontStyle.Regular, GraphicsUnit.Pixel); + label_P2P_SendFile_Pr.Location = new Point(269, 89); + label_P2P_SendFile_Pr.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + label_P2P_SendFile_Pr.Name = "label_P2P_SendFile_Pr"; + label_P2P_SendFile_Pr.Size = new Size(49, 19); + label_P2P_SendFile_Pr.TabIndex = 13; + label_P2P_SendFile_Pr.Text = "未发送"; + // + // metroLabel5 + // + metroLabel5.Font = new Font("Microsoft Sans Serif", 10F); + metroLabel5.IsDerivedStyle = true; + metroLabel5.Location = new Point(229, 89); + metroLabel5.Name = "metroLabel5"; + metroLabel5.Size = new Size(45, 23); + metroLabel5.Style = ReaLTaiizor.Enum.Metro.Style.Light; + metroLabel5.StyleManager = null; + metroLabel5.TabIndex = 12; + metroLabel5.Text = "进度:"; + metroLabel5.ThemeAuthor = "Taiizor"; + metroLabel5.ThemeName = "MetroLight"; + // + // label_P2P_SendFile_State + // + label_P2P_SendFile_State.AutoSize = true; + label_P2P_SendFile_State.Depth = 0; + label_P2P_SendFile_State.Font = new Font("Roboto", 14F, FontStyle.Regular, GraphicsUnit.Pixel); + label_P2P_SendFile_State.Location = new Point(139, 89); + label_P2P_SendFile_State.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + label_P2P_SendFile_State.Name = "label_P2P_SendFile_State"; + label_P2P_SendFile_State.Size = new Size(49, 19); + label_P2P_SendFile_State.TabIndex = 11; + label_P2P_SendFile_State.Text = "未发送"; + // + // metroLabel4 + // + metroLabel4.Font = new Font("Microsoft Sans Serif", 10F); + metroLabel4.IsDerivedStyle = true; + metroLabel4.Location = new Point(99, 89); + metroLabel4.Name = "metroLabel4"; + metroLabel4.Size = new Size(45, 23); + metroLabel4.Style = ReaLTaiizor.Enum.Metro.Style.Light; + metroLabel4.StyleManager = null; + metroLabel4.TabIndex = 10; + metroLabel4.Text = "状态:"; + metroLabel4.ThemeAuthor = "Taiizor"; + metroLabel4.ThemeName = "MetroLight"; + // + // btn_P2P_SendFile + // + btn_P2P_SendFile.AutoSizeMode = AutoSizeMode.GrowAndShrink; + btn_P2P_SendFile.Density = ReaLTaiizor.Controls.MaterialButton.MaterialButtonDensity.Default; + btn_P2P_SendFile.Depth = 0; + btn_P2P_SendFile.HighEmphasis = true; + btn_P2P_SendFile.Icon = null; + btn_P2P_SendFile.IconType = ReaLTaiizor.Controls.MaterialButton.MaterialIconType.Rebase; + btn_P2P_SendFile.Location = new Point(7, 89); + btn_P2P_SendFile.Margin = new Padding(4, 6, 4, 6); + btn_P2P_SendFile.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + btn_P2P_SendFile.Name = "btn_P2P_SendFile"; + btn_P2P_SendFile.NoAccentTextColor = Color.Empty; + btn_P2P_SendFile.Size = new Size(85, 36); + btn_P2P_SendFile.TabIndex = 9; + btn_P2P_SendFile.Text = "发送文件"; + btn_P2P_SendFile.Type = ReaLTaiizor.Controls.MaterialButton.MaterialButtonType.Contained; + btn_P2P_SendFile.UseAccentColor = false; + btn_P2P_SendFile.UseVisualStyleBackColor = true; + btn_P2P_SendFile.Click += btn_P2P_SendFile_Click; + // + // foxLabel2 + // + foxLabel2.BackColor = Color.Transparent; + foxLabel2.Font = new Font("Segoe UI", 10F, FontStyle.Bold); + foxLabel2.ForeColor = Color.FromArgb(76, 88, 100); + foxLabel2.Location = new Point(139, 131); + foxLabel2.Name = "foxLabel2"; + foxLabel2.Size = new Size(243, 19); + foxLabel2.TabIndex = 8; + foxLabel2.Text = "------------------收文件------------------"; + // + // foxLabel1 + // + foxLabel1.BackColor = Color.Transparent; + foxLabel1.Font = new Font("Segoe UI", 10F, FontStyle.Bold); + foxLabel1.ForeColor = Color.FromArgb(76, 88, 100); + foxLabel1.Location = new Point(139, 25); + foxLabel1.Name = "foxLabel1"; + foxLabel1.Size = new Size(243, 19); + foxLabel1.TabIndex = 7; + foxLabel1.Text = "------------------传文件------------------"; + // + // label_P2P_SendFilePath + // + label_P2P_SendFilePath.AutoSize = true; + label_P2P_SendFilePath.Depth = 0; + label_P2P_SendFilePath.Font = new Font("Roboto", 14F, FontStyle.Regular, GraphicsUnit.Pixel); + label_P2P_SendFilePath.Location = new Point(139, 57); + label_P2P_SendFilePath.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + label_P2P_SendFilePath.Name = "label_P2P_SendFilePath"; + label_P2P_SendFilePath.Size = new Size(104, 19); + label_P2P_SendFilePath.TabIndex = 6; + label_P2P_SendFilePath.Text = "C:\\abc\\123.txt"; + // + // metroLabel3 + // + metroLabel3.Font = new Font("Microsoft Sans Serif", 10F); + metroLabel3.IsDerivedStyle = true; + metroLabel3.Location = new Point(99, 57); + metroLabel3.Name = "metroLabel3"; + metroLabel3.Size = new Size(45, 23); + metroLabel3.Style = ReaLTaiizor.Enum.Metro.Style.Light; + metroLabel3.StyleManager = null; + metroLabel3.TabIndex = 5; + metroLabel3.Text = "文件:"; + metroLabel3.ThemeAuthor = "Taiizor"; + metroLabel3.ThemeName = "MetroLight"; + // + // btn_P2P_ChangeSendFile + // + btn_P2P_ChangeSendFile.AutoSizeMode = AutoSizeMode.GrowAndShrink; + btn_P2P_ChangeSendFile.Density = ReaLTaiizor.Controls.MaterialButton.MaterialButtonDensity.Default; + btn_P2P_ChangeSendFile.Depth = 0; + btn_P2P_ChangeSendFile.HighEmphasis = true; + btn_P2P_ChangeSendFile.Icon = null; + btn_P2P_ChangeSendFile.IconType = ReaLTaiizor.Controls.MaterialButton.MaterialIconType.Rebase; + btn_P2P_ChangeSendFile.Location = new Point(7, 47); + btn_P2P_ChangeSendFile.Margin = new Padding(4, 6, 4, 6); + btn_P2P_ChangeSendFile.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + btn_P2P_ChangeSendFile.Name = "btn_P2P_ChangeSendFile"; + btn_P2P_ChangeSendFile.NoAccentTextColor = Color.Empty; + btn_P2P_ChangeSendFile.Size = new Size(85, 36); + btn_P2P_ChangeSendFile.TabIndex = 4; + btn_P2P_ChangeSendFile.Text = "选择文件"; + btn_P2P_ChangeSendFile.Type = ReaLTaiizor.Controls.MaterialButton.MaterialButtonType.Contained; + btn_P2P_ChangeSendFile.UseAccentColor = false; + btn_P2P_ChangeSendFile.UseVisualStyleBackColor = true; + btn_P2P_ChangeSendFile.Click += btn_P2P_ChangeSendFile_Click; + // + // label_P2P_State + // + label_P2P_State.AutoSize = true; + label_P2P_State.Depth = 0; + label_P2P_State.Font = new Font("Roboto", 14F, FontStyle.Regular, GraphicsUnit.Pixel); + label_P2P_State.Location = new Point(269, 3); + label_P2P_State.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + label_P2P_State.Name = "label_P2P_State"; + label_P2P_State.Size = new Size(49, 19); + label_P2P_State.TabIndex = 3; + label_P2P_State.Text = "未连接"; + // + // metroLabel2 + // + metroLabel2.Font = new Font("Microsoft Sans Serif", 10F); + metroLabel2.IsDerivedStyle = true; + metroLabel2.Location = new Point(197, 3); + metroLabel2.Name = "metroLabel2"; + metroLabel2.Size = new Size(77, 23); + metroLabel2.Style = ReaLTaiizor.Enum.Metro.Style.Light; + metroLabel2.StyleManager = null; + metroLabel2.TabIndex = 2; + metroLabel2.Text = "P2P状态:"; + metroLabel2.ThemeAuthor = "Taiizor"; + metroLabel2.ThemeName = "MetroLight"; + // + // label_P2P_TargetUName + // + label_P2P_TargetUName.AutoSize = true; + label_P2P_TargetUName.Depth = 0; + label_P2P_TargetUName.Font = new Font("Roboto", 14F, FontStyle.Regular, GraphicsUnit.Pixel); + label_P2P_TargetUName.Location = new Point(78, 3); + label_P2P_TargetUName.MouseState = ReaLTaiizor.Helper.MaterialDrawHelper.MaterialMouseState.HOVER; + label_P2P_TargetUName.Name = "label_P2P_TargetUName"; + label_P2P_TargetUName.Size = new Size(49, 19); + label_P2P_TargetUName.TabIndex = 1; + label_P2P_TargetUName.Text = "用户名"; + // + // metroLabel1 + // + metroLabel1.Font = new Font("Microsoft Sans Serif", 10F); + metroLabel1.IsDerivedStyle = true; + metroLabel1.Location = new Point(6, 3); + metroLabel1.Name = "metroLabel1"; + metroLabel1.Size = new Size(77, 23); + metroLabel1.Style = ReaLTaiizor.Enum.Metro.Style.Light; + metroLabel1.StyleManager = null; + metroLabel1.TabIndex = 0; + metroLabel1.Text = "目标对象:"; + metroLabel1.ThemeAuthor = "Taiizor"; + metroLabel1.ThemeName = "MetroLight"; + // + // foxLabel3 + // + foxLabel3.BackColor = Color.Transparent; + foxLabel3.Font = new Font("Segoe UI", 10F, FontStyle.Bold); + foxLabel3.ForeColor = Color.FromArgb(76, 88, 100); + foxLabel3.Location = new Point(12, 21); + foxLabel3.Name = "foxLabel3"; + foxLabel3.Size = new Size(160, 19); + foxLabel3.TabIndex = 14; + foxLabel3.Text = "在线用户"; + // + // MainUI + // + AutoScaleDimensions = new SizeF(7F, 17F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(796, 450); + Controls.Add(foxLabel3); + Controls.Add(tabControl_Main); + Controls.Add(listBox_OnlineUser); + Controls.Add(listbox_log); + Name = "MainUI"; + Text = "HaoYueTunnel"; + Load += MainUI_Load; + tabControl_Main.ResumeLayout(false); + tabPage1.ResumeLayout(false); + tabPage1.PerformLayout(); + tabPage2.ResumeLayout(false); + tabPage2.PerformLayout(); + ResumeLayout(false); + } + + #endregion + + private ListBox listbox_log; + private ListBox listBox_OnlineUser; + private TextBox textBox_chatsendmsg; + private Button btn_chatsend; + private ReaLTaiizor.Controls.MetroTabControl tabControl_Main; + private TabPage tabPage1; + private TabPage tabPage2; + private ListBox listBox_AllChatMsg; + private ReaLTaiizor.Controls.MaterialLabel label_P2P_TargetUName; + private ReaLTaiizor.Controls.MetroLabel metroLabel1; + private ReaLTaiizor.Controls.MaterialLabel label_P2P_State; + private ReaLTaiizor.Controls.MetroLabel metroLabel2; + private ReaLTaiizor.Controls.MaterialButton btn_P2P_ChangeSendFile; + private ReaLTaiizor.Controls.MaterialLabel label_P2P_SendFilePath; + private ReaLTaiizor.Controls.MetroLabel metroLabel3; + private ReaLTaiizor.Controls.FoxLabel foxLabel2; + private ReaLTaiizor.Controls.FoxLabel foxLabel1; + private ReaLTaiizor.Controls.MaterialButton btn_P2P_SendFile; + private ReaLTaiizor.Controls.MaterialLabel label_P2P_SendFile_State; + private ReaLTaiizor.Controls.MetroLabel metroLabel4; + private ReaLTaiizor.Controls.MaterialLabel label_P2P_SendFile_Pr; + private ReaLTaiizor.Controls.MetroLabel metroLabel5; + private ReaLTaiizor.Controls.FoxLabel foxLabel3; + } +} \ No newline at end of file diff --git a/Client-GUI/MainUI.cs b/Client-GUI/MainUI.cs new file mode 100644 index 0000000..3b32f5f --- /dev/null +++ b/Client-GUI/MainUI.cs @@ -0,0 +1,347 @@ +using ClientCore; +using ClientCore.Data; +using ClientCore.Enum; +using ClientCore.Event; +using ClientCore.Network; +using System.Diagnostics; +using System.Threading.Tasks; +using System.Windows.Forms; +using System.Xml.Linq; + +namespace Client_GUI +{ + public partial class MainUI : Form + { + public delegate void DSetLogStr(List value); + public delegate void DSetStr(string value); + public delegate void DSetBool(bool value); + public delegate void DSetVoid(); + const int LogLimit = 200; + const int ChatShowLimit = 200; + + long mCurrUID = 0; + + public MainUI() + { + InitializeComponent(); + } + + private void MainUI_Load(object sender, EventArgs e) + { + //注册事件 + EventSystem.Instance.RegisterEvent(EEvent.UserLogin, OnUserLogin); + EventSystem.Instance.RegisterEvent(EEvent.TcpTunnelHelloResp, OnTcpTunnelHelloResp); + EventSystem.Instance.RegisterEvent(EEvent.TcpTunnelP2PStateUpdate, OnTcpTunnelP2PStateUpdate); + EventSystem.Instance.RegisterEvent(EEvent.UserJoin, OnUserJoin); + EventSystem.Instance.RegisterEvent(EEvent.UserLeave, OnUserLeave); + EventSystem.Instance.RegisterEvent(EEvent.OnChatMsg, OnChatMsg); + EventSystem.Instance.RegisterEvent(EEvent.OnP2PChatMsg, OnP2PChatMsg); + + EventSystem.Instance.RegisterEvent(EEvent.OnFileConfirmChoice, OnFileConfirmChoice); + + //EventSystem.Instance.RegisterEvent(EEvent.OnFilePushStart, ); + //EventSystem.Instance.RegisterEvent(EEvent.OnFilePushEnd, ); + + System.Timers.Timer bTimer = new System.Timers.Timer(); + bTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimeEvent_Log); + // 设置引发时间的时间间隔 此处设置为1秒(1000毫秒) + bTimer.Interval = 500; + bTimer.Enabled = true; + + App.Init("127.0.0.1", 23846, 23847, OnNetLog); + //App.Init("main.axibug.com", 23846, 23847, OnNetLog); + if (App.Connect()) + { + App.login.Login(StaticComm.LoginName); + } + UpdateP2PTargetState(); + } + + #region + + void OnNetLog(int LogLevel, string msg) + { + AddLog(msg); + } + + void OnUserLogin() + { + AddLog($"[User]登录成功"); + App.userMgr.Send_GetUserList(); + App.clientMgr.ConnectTcpTunnelServer(); + } + void OnTcpTunnelHelloResp() + { + AddLog($"[TcpTunnel]OnTcpTunnelHelloResp"); + } + void OnTcpTunnelP2PStateUpdate(long UID) + { + AddLog($"[TcpTunnel]OnTcpTunnelP2PStateUpdate"); + if (UID == mCurrUID) + { + UpdateP2PTargetState(); + } + else + { + mCurrUID = UID; + UpdateP2PTargetState(); + } + } + void OnUserJoin(long UID, string NickName) + { + AddLog($"[User]用户{NickName}上线"); + AddChat($"[{NickName}]上线"); + UpdateOnlineUser(); + } + void OnUserLeave(long UID, string NickName) + { + AddLog($"[User]用户{UID}下线"); + AddChat($"[{NickName}]离开"); + UpdateOnlineUser(); + } + void OnChatMsg(string str1, string str2) + { + AddLog($"[Chat]{str1}:{str2}"); + AddChat($"[{str1}]: {str2}"); + } + void OnP2PChatMsg(long uid, string str2) + { + AddLog($"[P2PChatMsg]{uid}:{str2}"); + } + void OnFileConfirmChoice(int TaskID) + { + DialogResult MsgBoxResult;//设置对话框的返回值 + MsgBoxResult = System.Windows.Forms.MessageBox.Show("是否接受文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2);//定义对话框的按钮式样 + if (MsgBoxResult.ToString() == "Yes")//如果对话框的返回值是YES(按"Y"按钮) + { + //选择了Yes,继续 + App.p2pFile.receiver_AgreeRecvFile(TaskID, 1); + } + if (MsgBoxResult.ToString() == "No")//如果对话框的返回值是NO(按"N"按钮) + { + //选择了No + App.p2pFile.receiver_AgreeRecvFile(TaskID, 0); + } + } + void OnFilePushStart(long TaskID) + { + AddLog($"[P2PFile]文件传输开始"); + } + + #endregion + + #region 委托操作界面 + + #endregion + + void AddChat(string str) + { + this.BeginInvoke(new DSetStr(AddChat_delegate), str); + } + + void AddChat_delegate(string str) + { + this.listBox_AllChatMsg.Items.Add(str); + while (listbox_log.Items.Count > ChatShowLimit) + { + listbox_log.Items.RemoveAt(listbox_log.Items.Count - ChatShowLimit); + } + } + + void UpdateOnlineUser() + { + this.BeginInvoke(new DSetVoid(UpdateOnlineUser_delegate)); + } + + Dictionary DictUserShowIdx2UID = new Dictionary(); + void UpdateOnlineUser_delegate() + { + DictUserShowIdx2UID.Clear(); + UserInfo[] ulist = App.userMgr.GetUserInfo(); + Console.WriteLine("User总数" + ulist.Length); + this.listBox_OnlineUser.Items.Clear(); + for (int i = 0; i < ulist.Length; i++) + { + DictUserShowIdx2UID[i] = ulist[i].UID; + this.listBox_OnlineUser.Items.Add(ulist[i].NickName); + } + } + + + void UpdateP2PTargetState() + { + this.BeginInvoke(new DSetVoid(UpdateP2PTargetState_delegate)); + } + + void UpdateP2PTargetState_delegate() + { + bool bCanUse = false; + string StateName = ""; + + if (mCurrUID <= 0) + { + this.label_P2P_TargetUName.Text = "未选择"; + } + else + { + App.clientMgr.GetP2PTargetState(mCurrUID, out E_P2P_STATE state); + switch (state) + { + case E_P2P_STATE.None: + StateName = "未连接"; + break; + case E_P2P_STATE.Doing: + StateName = "打洞中……"; + break; + case E_P2P_STATE.Fail: + StateName = "打洞失败"; + break; + case E_P2P_STATE.Success: + StateName = "隧道已连通"; + bCanUse = true; + break; + } + } + + this.label_P2P_State.Text = StateName; + + if (bCanUse) + { + btn_P2P_SendFile.Enabled = true; + btn_P2P_ChangeSendFile.Enabled = true; + } + else + { + btn_P2P_SendFile.Enabled = false; + btn_P2P_ChangeSendFile.Enabled = false; + } + } + + #region 日志 + + + static Queue logQueue = new Queue(); + public static void AddLog(string val) + { + logQueue.Enqueue($"{DateTime.Now.ToString("HH:mm:ss")}> {val}"); + } + + private void TimeEvent_Log(object source, System.Timers.ElapsedEventArgs e) + { + List input = new List(); + while (logQueue.Count > 0) + { + string str = logQueue.Dequeue(); + input.Add(str); + } + if (input.Count < 1) + return; + + this.BeginInvoke(new DSetLogStr(ShowLog), input); + } + + void ShowLog(List value) + { + foreach (var v in value) + { + this.listbox_log.Items.Add(v); + } + while (listbox_log.Items.Count > LogLimit) + { + listbox_log.Items.RemoveAt(listbox_log.Items.Count - LogLimit); + } + //TODO 最下方 + //listbox_log.ScrollIntoView(listbox_log.Items[listbox_log.Items.Count - 1]); + } + #endregion + + private void btn_chatsend_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBox_chatsendmsg.Text)) + return; + App.chat.SendChatMsg(textBox_chatsendmsg.Text); + textBox_chatsendmsg.Text = string.Empty; + } + + private void listBox_OnlineUser_DoubleClick(object sender, EventArgs e) + { + ListBox listBox = sender as ListBox; + if (listBox == null) + return; + + // 当ListBox有选中项时 + if (listBox.SelectedIndex <= -1) + return; + + if (!DictUserShowIdx2UID.ContainsKey(listBox.SelectedIndex)) + return; + + + long Uid = DictUserShowIdx2UID[listBox.SelectedIndex]; + + if (Uid == App.userMgr.MainPlayer.UID) + { + MessageBox.Show("不能和自己建立P2P隧道"); + return; + } + + if (mCurrUID == Uid) + return; + + UserInfo uinfo = App.userMgr.GetUserByUid(Uid); + + //已有 + if (App.clientMgr.GetP2PTargetState(Uid, out E_P2P_STATE state)) + { + mCurrUID = Uid; + this.label_P2P_TargetUName.Text = uinfo.NickName; + this.tabControl_Main.SelectedIndex = 1; + UpdateP2PTargetState(); + } + else + { + if (uinfo != null) + { + mCurrUID = Uid; + this.label_P2P_TargetUName.Text = uinfo.NickName; + this.tabControl_Main.SelectedIndex = 1; + App.p2ptcp.SendDoTunnel(Convert.ToInt64(uinfo.UID)); + } + } + } + + private void btn_P2P_ChangeSendFile_Click(object sender, EventArgs e) + { + // 1. 打开文件管理器选择文件 + OpenFileDialog openFileDialog1 = new OpenFileDialog(); //显示选择文件对话框 + openFileDialog1.InitialDirectory = "c:\\"; + openFileDialog1.Filter = "All files (*.*)|*.*"; //所有的文件格式 + openFileDialog1.FilterIndex = 2; + openFileDialog1.RestoreDirectory = true; + + // 2. 查看可执行文件路径 + if (openFileDialog1.ShowDialog() == DialogResult.OK) + { + this.label_P2P_SendFilePath.Text = openFileDialog1.FileName; + } + } + + private void btn_P2P_SendFile_Click(object sender, EventArgs e) + { + string filePath = this.label_P2P_SendFilePath.Text; + if (string.IsNullOrEmpty(filePath)) + { + MessageBox.Show("未选择文件"); + return; + } + + if (!File.Exists(filePath)) + { + MessageBox.Show("选择的文件不存在"); + return; + } + + App.p2pFile.sender_FilePushConfirmToTarget(mCurrUID, filePath); + } + } +} diff --git a/Client-GUI/MainUI.resx b/Client-GUI/MainUI.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Client-GUI/MainUI.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Client-GUI/Program.cs b/Client-GUI/Program.cs new file mode 100644 index 0000000..93aeabd --- /dev/null +++ b/Client-GUI/Program.cs @@ -0,0 +1,28 @@ +using ClientCore; + +namespace Client_GUI +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + + LoginUI loginUI = new LoginUI(); + loginUI.ShowDialog(); + + if (!StaticComm.bClickLogin) + { + Application.Exit(); + return; + } + Application.Run(new MainUI()); + } + } +} \ No newline at end of file diff --git a/Client-GUI/Properties/PublishProfiles/FolderProfile.pubxml b/Client-GUI/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..c9fe344 --- /dev/null +++ b/Client-GUI/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,18 @@ + + + + + Release + Any CPU + bin\Release\net8.0-windows\publish\win-x86\ + FileSystem + <_TargetId>Folder + net8.0-windows + win-x86 + true + true + true + + \ No newline at end of file diff --git a/Client-GUI/StaticComm.cs b/Client-GUI/StaticComm.cs new file mode 100644 index 0000000..81715ed --- /dev/null +++ b/Client-GUI/StaticComm.cs @@ -0,0 +1,13 @@ +using ClientCore; +using ClientCore.Event; + +namespace Client_GUI +{ + internal class StaticComm + { + public static bool bClickLogin = false; + public static string LoginName; + + + } +} diff --git a/ClientCore/App.cs b/ClientCore/App.cs index b4c3c28..cb35736 100644 --- a/ClientCore/App.cs +++ b/ClientCore/App.cs @@ -26,22 +26,33 @@ namespace ClientCore public static AppChat chat; public static UserMgr userMgr; - public static void Init(string IP, int port, int tcptunnelport) + static string mIP; + static int mPort; + + public static void Init(string IP, int port, int tcptunnelport, LogManager.OnLogHandler onLog = null) { + mIP = IP; + mPort = port; log = new LogManager(); + if (onLog != null) + LogManager.OnLog += onLog; login = new AppLogin(); chat = new AppChat(); userMgr = new UserMgr(); clientMgr = new ClientManager(); - clientMgr.SetIpData(IP, port, tcptunnelport); + clientMgr.SetIpData(mIP, mPort, tcptunnelport); p2ptcp = new P2PTcp(); p2pChat = new P2PChat(); p2pFile = new P2PFile(); networkMain = new NetworkHelper(Enum.ServerType.MainServer); - networkMain.Init(IP, port); //networkTcp2S = new NetworkHelper(Enum.ServerType.TcpTunnelServer); //networkTcp2S.Init(IP, tcptunnelport); } + + public static bool Connect() + { + return networkMain.Init(mIP, mPort); + } } diff --git a/ClientCore/Enum/CommEnum.cs b/ClientCore/Enum/CommEnum.cs index 1964474..22a6cb6 100644 --- a/ClientCore/Enum/CommEnum.cs +++ b/ClientCore/Enum/CommEnum.cs @@ -13,4 +13,12 @@ namespace ClientCore.Enum TcpP2PTarget } + + public enum E_P2P_STATE + { + None = 0, + Doing,// + Fail,// + Success,//进行成功 + } } diff --git a/ClientCore/Event/EEvent.cs b/ClientCore/Event/EEvent.cs index f12e3aa..7fd2cd6 100644 --- a/ClientCore/Event/EEvent.cs +++ b/ClientCore/Event/EEvent.cs @@ -17,11 +17,21 @@ namespace ClientCore.Event //打洞流程 TcpTunnelHelloResp, + TcpTunnelP2PStateUpdate,//打洞状态 //连接管理 OnSocketConnect, OnSocketDisconnect, //文件传输 + /// + /// 同意传输 + /// + OnFileConfirmChoice, + OnFilePushStart, + OnFilePushPr, + OnFilePushEnd, + + } } diff --git a/ClientCore/Manager/ClientManager.cs b/ClientCore/Manager/ClientManager.cs index 4cf0d02..f3df467 100644 --- a/ClientCore/Manager/ClientManager.cs +++ b/ClientCore/Manager/ClientManager.cs @@ -42,12 +42,12 @@ namespace ClientCore.Manager { if (IsOk) { - Console.WriteLine("MainServer连接成功"); + App.log.Info("MainServer连接成功"); } else { - Console.WriteLine("MainServer连接失败"); + App.log.Info("MainServer连接失败"); } } @@ -57,12 +57,12 @@ namespace ClientCore.Manager bTryReConTcpTunnelServer = false; if (IsOk) { - Console.WriteLine("TcpTunnelServer连接成功"); + App.log.Info("TcpTunnelServer连接成功"); App.p2ptcp.SendHellToSev(); } else { - Console.WriteLine("TcpTunnelServer连接失败"); + App.log.Info("TcpTunnelServer连接失败"); } } break; @@ -70,7 +70,7 @@ namespace ClientCore.Manager { if (IsOk) { - Console.WriteLine("TcpP2PTarget连接成功"); + App.log.Info("TcpP2PTarget连接成功"); } } break; @@ -82,20 +82,20 @@ namespace ClientCore.Manager { case ServerType.MainServer: { - Console.WriteLine("MainServer连接断开"); + App.log.Info("MainServer连接断开"); } break; case ServerType.TcpTunnelServer: { - Console.WriteLine("TcpTunnelServer连接断开"); - Console.WriteLine("TcpTunnelServer,尝试重连"); + App.log.Info("TcpTunnelServer连接断开"); + App.log.Info("TcpTunnelServer,尝试重连"); ReConnectTcpTunnelServer(); } break; case ServerType.TcpP2PTarget: { - Console.WriteLine("TcpP2PTarget连接断开"); - RemoveTargetSocket(uid); + App.log.Info("TcpP2PTarget连接断开"); + RemoveP2PTargetSocket(uid); } break; } @@ -132,12 +132,31 @@ namespace ClientCore.Manager #region P2PTarget 管理 - void AddTargetSocket(NetworkP2PHelper targetSocket) + public void AddP2PTargetSocket(NetworkP2PHelper targetSocket) { DictUID2TcpTaret[targetSocket.mUID] = targetSocket; } - void RemoveTargetSocket(long UID) + public void SetP2PTargetState(long UID,E_P2P_STATE state) + { + if (!DictUID2TcpTaret.ContainsKey(UID)) + return; + + DictUID2TcpTaret[UID].e_P2P_STATE = state; + } + + public bool GetP2PTargetState(long UID, out E_P2P_STATE state) + { + if (DictUID2TcpTaret.ContainsKey(UID)) + { + state = DictUID2TcpTaret[UID].e_P2P_STATE; + return true; + } + state = E_P2P_STATE.None; + return false; + } + + void RemoveP2PTargetSocket(long UID) { if (DictUID2TcpTaret.ContainsKey(UID)) { @@ -183,9 +202,39 @@ namespace ClientCore.Manager public void ConnectTCPTargetP2PThread(object obj) { Thread.Sleep(1000); + int userBindPort = LastlocalPort; Protobuf_TcpTunnel_DoTunnel_RESP msg = (Protobuf_TcpTunnel_DoTunnel_RESP)obj; - Console.WriteLine("LocalEndPoint Port:" + userBindPort); + App.log.Info("LocalEndPoint Port:" + userBindPort); + NetworkP2PHelper targetSocket = GetTargetSocket(msg.TargetUID); + targetSocket.Init(true, userBindPort); + //尝试5次连接 + for (int j = 0; j < 5; j++) + { + try + { + if (!targetSocket.Connect(msg.OtherIP, msg.OtherPort)) + continue; + + App.log.Info($"Connect:成功 目标用户UID:{msg.TargetUID},{msg.OtherIP},{msg.OtherPort}"); + //AddTargetSocket(targetSocket); + SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Success); + EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID); + return; + } + catch (Exception) + { + App.log.Info("Connect:失败"); + } + } + SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Fail); + EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID); + + + /* + int userBindPort = LastlocalPort; + Protobuf_TcpTunnel_DoTunnel_RESP msg = (Protobuf_TcpTunnel_DoTunnel_RESP)obj; + App.log.Info("LocalEndPoint Port:" + userBindPort); NetworkP2PHelper targetSocket = new NetworkP2PHelper(Enum.ServerType.TcpP2PTarget, msg.TargetUID); targetSocket.Init(msg.OtherIP, msg.OtherPort, true, userBindPort); //尝试5次连接 @@ -194,15 +243,18 @@ namespace ClientCore.Manager try { targetSocket.Connect(msg.OtherIP, msg.OtherPort); - Console.WriteLine("Connect:成功{0},{1}", msg.OtherIP, msg.OtherPort); + App.log.Info($"Connect:成功 目标用户UID:{msg.TargetUID},{msg.OtherIP},{msg.OtherPort}"); AddTargetSocket(targetSocket); - break; + return; } catch (Exception) { - Console.WriteLine("Connect:失败"); + App.log.Info("Connect:失败"); } } + + EventSystem.Instance.PostEvent(EEvent.TcpTunnelFail, msg.TargetUID); + */ } #endregion diff --git a/ClientCore/Manager/LogManager.cs b/ClientCore/Manager/LogManager.cs index 612ec1b..ea8bb11 100644 --- a/ClientCore/Manager/LogManager.cs +++ b/ClientCore/Manager/LogManager.cs @@ -2,19 +2,48 @@ { public class LogManager { + public enum E_LogType : byte + { + Info = 0, + Debug = 1, + Warning = 2, + Error = 3, + } + /// + /// 日志 + /// + /// + public delegate void OnLogHandler(int debuglv, string msg); + + /// + /// 内部输出 + /// + public static event OnLogHandler OnLog; + + + public void Info(string str) + { + Log(E_LogType.Info, str); + } + public void Debug(string str) { - Console.WriteLine(str); + Log(E_LogType.Debug, str); } public void Warning(string str) { - Console.WriteLine(str); + Log(E_LogType.Warning, str); } public void Error(string str) { - Console.WriteLine(str); + Log(E_LogType.Error, str); + } + + public void Log(E_LogType logtype, string str) + { + OnLog?.Invoke((int)logtype, str); } } } \ No newline at end of file diff --git a/ClientCore/Manager/P2PFile.cs b/ClientCore/Manager/P2PFile.cs index 6d69833..f00c0c3 100644 --- a/ClientCore/Manager/P2PFile.cs +++ b/ClientCore/Manager/P2PFile.cs @@ -1,5 +1,6 @@ using AxibugProtobuf; using ClientCore.Common; +using ClientCore.Enum; using ClientCore.Event; using ClientCore.Network; using Google.Protobuf; @@ -16,7 +17,7 @@ namespace ClientCore.Manager public int cPackSize = 5 * 1024; public int WaitSendPackCount = 100; //是否自动同意接收 - public bool bAutoRecv = true; + public bool bAutoRecv = false; public P2PFile() { //发送者协议注册 @@ -159,7 +160,11 @@ namespace ClientCore.Manager //开辟临时缓存内存 byte[] byteArrayRead = new byte[cPackSize]; // 1字节*1024 = 1k 1k*1024 = 1M内存 - App.log.Debug($"[文件发送者]开始发送文件!!"); + App.log.Info($"[文件发送者]开始发送文件!!"); + + //开始发送文件事件 + EventSystem.Instance.PostEvent(EEvent.OnFilePushStart, TaskID); + //通过死缓存去读文本中的内容 while (true) { @@ -187,9 +192,11 @@ namespace ClientCore.Manager //既然是死循环 那么什么时候我们停止读取文本内容 我们知道文本最后一行的大小肯定是小于缓存内存大小的 if (readCount < byteArrayRead.Length) { - App.log.Debug($"[文件发送者]文件发送完毕!! task.CurrIndex->{task.CurrIndex}"); + App.log.Info($"[文件发送者]文件发送完毕!! task.CurrIndex->{task.CurrIndex}"); DictTaskID2Task_SendDo.Remove(TaskID); fsRead.Close(); + //开始发送文件事件 + EventSystem.Instance.PostEvent(EEvent.OnFilePushEnd, TaskID); break; //结束循环 } } @@ -279,7 +286,11 @@ namespace ClientCore.Manager //自动接收文件 if (bAutoRecv) { - receiver_AgreeRecvFile(task.TaskID); + receiver_AgreeRecvFile(task.TaskID, 1); + } + else + { + EventSystem.Instance.PostEvent(EEvent.OnFileConfirmChoice, task.TaskID); } } @@ -287,7 +298,7 @@ namespace ClientCore.Manager /// 同意文件传输 /// /// - public void receiver_AgreeRecvFile(int TaskID) + public void receiver_AgreeRecvFile(int TaskID, int bAgree) { if (DictTaskID2Task_RecvReady.ContainsKey(TaskID)) { @@ -295,8 +306,8 @@ namespace ClientCore.Manager DictTaskID2Task_RecvDo[TaskID] = task; DictTaskID2Task_RecvReady.Remove(TaskID); receiver_StartRecvFile(TaskID); - App.log.Debug("[文件接收者]:发送同意接收"); - SendFilePushConfirm_Resp(task.fromUID, task.TaskID, 1); + App.log.Info("[文件接收者]:发送同意接收"); + SendFilePushConfirm_Resp(task.fromUID, task.TaskID, bAgree); } } @@ -317,7 +328,7 @@ namespace ClientCore.Manager Thread thread = new Thread(receiver_StartRecvFileThread); thread.IsBackground = true; thread.Start(TaskID); - App.log.Debug("[文件接收者]:准备接收线程"); + App.log.Info("[文件接收者]:准备接收线程"); } /// @@ -373,7 +384,7 @@ namespace ClientCore.Manager if (task.CurrIndex + 1 >= task.PackCount) { //文件接收完毕 - App.log.Debug($"[文件接收者]文件接收完毕!! task.CurrIndex->{task.CurrIndex} task.PackCount->{task.PackCount}"); + App.log.Info($"[文件接收者]文件接收完毕!! task.CurrIndex->{task.CurrIndex} task.PackCount->{task.PackCount}"); //删除进行中字典 DictTaskID2Task_RecvDo.Remove(TaskID); //删除进行中数据队列 diff --git a/ClientCore/Manager/P2PTcp.cs b/ClientCore/Manager/P2PTcp.cs index 5134619..b8be4cf 100644 --- a/ClientCore/Manager/P2PTcp.cs +++ b/ClientCore/Manager/P2PTcp.cs @@ -1,5 +1,6 @@ using AxibugProtobuf; using ClientCore.Common; +using ClientCore.Enum; using ClientCore.Event; using ClientCore.Network; using System.Net; @@ -38,7 +39,7 @@ namespace ClientCore.Manager Protobuf_TcpTunnel_DoTunnel msg = new Protobuf_TcpTunnel_DoTunnel() { UID = App.userMgr.MainPlayer.UID, - TargetUID = targetUID + TargetUID = targetUID, }; App.networkTcp2S.SendToServer((int)CommandID.CmdTcptunnelDo, ProtoBufHelper.Serizlize(msg)); } @@ -47,13 +48,22 @@ namespace ClientCore.Manager { Protobuf_TcpTunnel_DoTunnel_RESP msg = ProtoBufHelper.DeSerizlize(reqData); //TODO 打洞 - Console.WriteLine($"打洞目标信息 {msg.TargetUID} {msg.OtherIP} {msg.OtherPort}"); + App.log.Info($"打洞目标信息 {msg.TargetUID} {msg.OtherIP} {msg.OtherPort}"); int localPort = ((IPEndPoint)App.networkTcp2S.GetClientSocket().LocalEndPoint).Port; + NetworkP2PHelper targetSocket = new NetworkP2PHelper(Enum.ServerType.TcpP2PTarget, msg.TargetUID); + App.clientMgr.AddP2PTargetSocket(targetSocket); + App.clientMgr.SetP2PTargetState(msg.TargetUID, E_P2P_STATE.Doing); + //断开连接 App.networkTcp2S.CloseConntect(); + //TODO重新连接打洞服务器 + //进行打洞事件 + EventSystem.Instance.PostEvent(EEvent.TcpTunnelP2PStateUpdate, msg.TargetUID); + + //P2P连接尝试 App.clientMgr.ConnectTCPTargetP2P(msg, localPort); } diff --git a/ClientCore/Manager/UserMgr.cs b/ClientCore/Manager/UserMgr.cs index f58b0df..fb8bb22 100644 --- a/ClientCore/Manager/UserMgr.cs +++ b/ClientCore/Manager/UserMgr.cs @@ -73,6 +73,8 @@ namespace ClientCore.Manager State = minfo.State, NickName = minfo.NickName, }; + //抛出用户加入事件 + EventSystem.Instance.PostEvent(EEvent.UserJoin, minfo.UID, minfo.NickName); } else { @@ -80,17 +82,17 @@ namespace ClientCore.Manager DictUID2User[minfo.UID].NickName= minfo.NickName; } } - //抛出用户加入事件 - EventSystem.Instance.PostEvent(EEvent.UserJoin, minfo.UID); } public void RemoveUser(long UID) { bool bflag = false; + string UName = ""; lock (DictUID2User) { if (DictUID2User.ContainsKey(UID)) { + UName = DictUID2User[UID].NickName; DictUID2User.Remove(UID); bflag = true; } @@ -98,7 +100,18 @@ namespace ClientCore.Manager if (bflag) { //抛出用户离开事件 - EventSystem.Instance.PostEvent(EEvent.UserLeave, UID); + EventSystem.Instance.PostEvent(EEvent.UserLeave, UID, UName); + } + } + public UserInfo GetUserByUid(long UID) + { + lock (DictUID2User) + { + if (DictUID2User.ContainsKey(UID)) + { + return DictUID2User[UID]; + } + return null; } } diff --git a/ClientCore/Network/NetworkHelper.cs b/ClientCore/Network/NetworkHelper.cs index f21471b..6dc2932 100644 --- a/ClientCore/Network/NetworkHelper.cs +++ b/ClientCore/Network/NetworkHelper.cs @@ -51,7 +51,8 @@ namespace ClientCore.Network //用于Unity内的输出 //Debug.Log("NetCoreDebug >> "+str); - Console.WriteLine("NetCoreDebug >> " + str); + //App.log.Info("NetCoreDebug >> " + str); + App.log.Debug(str); } /// diff --git a/ClientCore/Network/NetworkP2PHelper.cs b/ClientCore/Network/NetworkP2PHelper.cs index db1bd3f..28cff60 100644 --- a/ClientCore/Network/NetworkP2PHelper.cs +++ b/ClientCore/Network/NetworkP2PHelper.cs @@ -17,6 +17,7 @@ namespace ClientCore.Network { ServerType mServerType; public long mUID { get; private set; } = -1; + public E_P2P_STATE e_P2P_STATE = E_P2P_STATE.None; public NetworkP2PHelper(ServerType serverType,long UID = -1) { //指定接收服务器数据事件 @@ -27,6 +28,7 @@ namespace ClientCore.Network OnLogOut += NetworkDeBugLog; OnConnected += NetworkConnected; mServerType = serverType; + e_P2P_STATE = E_P2P_STATE.None; mUID = UID; } @@ -51,7 +53,7 @@ namespace ClientCore.Network //用于Unity内的输出 //Debug.Log("NetCoreDebug >> "+str); - Console.WriteLine("NetCoreDebug >> " + str); + App.log.Info("NetCoreDebug >> " + str); } /// diff --git a/ClientCore/Properties/PublishProfiles/FolderProfile.pubxml b/ClientCore/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..df5633a --- /dev/null +++ b/ClientCore/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,13 @@ + + + + + Release + Any CPU + bin\Release\net8.0\publish\ + FileSystem + <_TargetId>Folder + + \ No newline at end of file diff --git a/HaoYueTunnel.sln b/HaoYueTunnel.sln index cb214ee..da91bb0 100644 --- a/HaoYueTunnel.sln +++ b/HaoYueTunnel.sln @@ -18,7 +18,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientCore", "ClientCore\Cl EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Protobuf", "Protobuf\Protobuf.csproj", "{07B4FB42-CA26-4C68-AFE8-47A50BABFB64}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client-Cli", "Client-Cli\Client-Cli.csproj", "{A363C9A2-FBFB-40D7-8B34-2BE384C49A29}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client-Cli", "Client-Cli\Client-Cli.csproj", "{A363C9A2-FBFB-40D7-8B34-2BE384C49A29}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client-GUI", "Client-GUI\Client-GUI.csproj", "{D2C54B2E-08F7-4F9B-909F-A62ED6B60CCB}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -46,6 +48,10 @@ Global {A363C9A2-FBFB-40D7-8B34-2BE384C49A29}.Debug|Any CPU.Build.0 = Debug|Any CPU {A363C9A2-FBFB-40D7-8B34-2BE384C49A29}.Release|Any CPU.ActiveCfg = Release|Any CPU {A363C9A2-FBFB-40D7-8B34-2BE384C49A29}.Release|Any CPU.Build.0 = Release|Any CPU + {D2C54B2E-08F7-4F9B-909F-A62ED6B60CCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2C54B2E-08F7-4F9B-909F-A62ED6B60CCB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2C54B2E-08F7-4F9B-909F-A62ED6B60CCB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2C54B2E-08F7-4F9B-909F-A62ED6B60CCB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/NetLib/HaoYueNet.ClientNetwork.dll b/NetLib/HaoYueNet.ClientNetwork.dll index bc3795c09dfe097d8b0fb589a2bb57a6e3f8e2cf..780d13cb5d8c0bc567b8b2d539b6f836999ef6d5 100644 GIT binary patch delta 3149 zcmY+Ge{fXQ7018tz5CXe4S|>_iPA_iiDR0?uq+ALNNhI}A~r!$eyG(#Hk)Q5Os9cD zMJ()wFpIWUpbu?29h}6(j8YNn;@DO?vrH^x0@JOq3U->ZsI>XRnYRAXiDM_zd(OLD zW|P_Pp3gb=d+)pF-F@@oV@7<;IP{#D+}rc@;TPVt!%qS0-AHGP`;1Xy-0L1Tp0-0P z04G^sb~wgn_i_I+%c@)A9`u*oCaQ}8|Br5w;y0fy0)+2}w@RoSb z&)rs5TvmD)P;SV~hII&41BH zz*^gfO7t-e$Th~TN(?EQ$n&8J=M}Z*k6TrkVM@r6m#sVD)bgCp-|ehbjj*EsNctvr zDcWZJ!>YmCF@<|1)?!A{S>p?<4&`;;G?ygZjmH!%&i~S?$00>^l9phSX#n5P_aT6) zyV-|-$_ovkj%fhf<%I^&uc*@AjiuO_?0F)LBZ@+jT5(>{e~lt> zA4Jge<(WldC0ZoO^%KqsyHvDchbo?uv! z(8uH*%XJu1M8|SH&MTs0xgOV;5{NqE)&~6BBiX|{#70y%(VVe_Y~mf_QFJjToM}{v z$1tSmUy>fjUleV#YQ%Tpgvgk1_DFgHVMUAc1L8^SQdB4DdpOSI9m`Gl$gA$ye#$V(ZU0tIczNCqEIHZYuM{|6y=o($fk2YqY~RHKolFaaT zNlj#YFUR;hUCWqr)*m#b&6DDkCi0~ec^f>fYqZ|P^@DN7pannBMKYevF;40l?QlvH z?eLr?^8Go-m)5n6Ic|NZDQ%|21%Cd@<8{U$<3~BhkM&Tx6_+*9t@uO}`TmyUo7OeD z_g6I0y}v=^y9&^g<@)LVR1c*cey)jjcuf--XW;hNE_8R)zLHm1XkfQg(>+>yr#n~g zPj0Mx-51+~#g-$VtreDuW}{5%y#{4kjS8zATQO!=AS(4PJb`!ZdgNnD)T0;|r7psk zQY-Pb)LJyl>N0GUx&l$DE732tgY)a8hIzKA(kM>YgV-9AQxD1oPS~Sd80C0^;|Y#e z8e3(x%Xk*Y>?p@^jt4m&=6Hm4lrt%gPqR*NCd=_v*3UQtlV&rd#*W#RNeh&7s*>X{ z$16FGvJSG2vQDsm#%fvQt*~giN{(AOj<61}b_uG+S%+CutXa~S4V$lpwVbtrHO$(| z8etvr(a6)RS6PjlDAUCnXB}ouv1VD}(3o=8u%~&H3HwQ7UHlbi9cE3jW?7NXX4ddL zIt^W{an=-TmNi^J)%^u@R`$;)P0goFcmd}ZaHfzmHbi zI?79cwjF&}V60hd%AeOh`8gtA$d9<&I!f$?TY!b`&pRp$v#VQw(|&f*n&R4)zEuX% uEk6OtQT}g2qhzWC5aLfrzL*AOO!bKT>2=G81bIU8pGG&M*M0xtRsRPMRHLr| delta 3213 zcmY+G4Qx}_703VYz586pF2M`wpk)z`!k36iox~6aP~|Lv!s2E?2&3!Ak~m2rY^xg5 zRxPx4nqX0lZOuH|YTeXy@zP4m+JQx+BxSv zF0w8EeSYWM|Gn>>ci*#9StFG-o;#$5ZeQ~8@ZoWL*B)R;rXju|^MtX`Xv+*6kK0Y_ zfzsSOv)OCx$=vI!wd_zh^KIY4WyNac0>HPgl*DK5F9DS2;oAsIORX#eJj+)Bxwm~k zD5>`@1}dHg`sw?peu`WB`}TbOp?#p&(iK3^pj=xknpOe<%2nLmwxx-_cVv!~tv5c* zoh|!|ojgNDAdQPfBUS}sG&yhzTWt@PpgA?wmqkq65$RR5c+`<__YM0FwFxP=c!KC|92er2Ubi-5 z)~QiTuUlL2fm`dN1N{P2El)ZCZfP7Tp)|OM_aNhtbgcz12{~PB!6Z|Xj0QYtK~qGI z4ZXe>tQMRR8jx+-ir8ANIc;l0zmT&n+mLmMx8+XExWwCXC$0&lk?@XLU&8+!q8lj zmqJ8e!Gw^rE!%O)smZo%#|KU=Df_Y=s-7oxn%IHELQWGq@v=+25fgaTA!%s>ZwWaq zP2i%?^Z3LYRSC>#x=VH8)^)tX^H9cSwHtAlk|oAAwHHTR{KVU#9>%yH+N=5yphp!O zON>6%&mk zJx%O;xxhD~YkZ(T)WipRlgO7;*#hH_^w2zY=2cDHnb$P&f{F%~AD+ zCiWdG@FjD)m^ZIm$2CoxOHJ%MS>T(}HD2#sdH&MI zIBoEPr*)Byzb-KTM%VZZ&uHQ^{GBHD{l36=R@d_8nDs|Z)83_%ul*%WeC=-%`!0iiW^UdVSM*Rm!+&bxGkjkY8$Zl!t2~}5 zT^+EWED8ivX1H=$E)qKCNgl)!%Zq1GZkcE>%1AdFoY`WOTS4r_m|czp=?dJ3pV?LL zE|>yGYlgo3s&qq|K6VC5_2!30D(1Y7b&>lBOP{1&-RI zQW%wZO5!Pr8;!kGZ8!E~+)hZGl6X+!5s6#&x$T&y&`&Q5y#ovS%>bB{O3q>xi!C-A2@jR>BqKj{cppI z{zFG6(pE_>web&@=a!%{ioLm=Te3#7r=~kxQ&->9RbL&6boNAQd#V%Bny%XJHQ~;+ zbe~I{Ss3?hH7W57ea7j}`*`z>D%eW* z3$TJdRlW3yOD(g%xhybr^`o&JOIzcEZyy|e=w$C)=C$TWgKqNvMmNa|e7gF9W0!xj z?`xCsiD0fFK4IjFTOPE&*h7C;yAdX>Lp^%XMOuvry*r5_bU*eGClI9(T}0hjL!&$C WAyP*<9@Rl_u1Cq4$ozcU#{UBf(4kZS diff --git a/Server/Properties/PublishProfiles/FolderProfile.pubxml b/Server/Properties/PublishProfiles/FolderProfile.pubxml new file mode 100644 index 0000000..df5633a --- /dev/null +++ b/Server/Properties/PublishProfiles/FolderProfile.pubxml @@ -0,0 +1,13 @@ + + + + + Release + Any CPU + bin\Release\net8.0\publish\ + FileSystem + <_TargetId>Folder + + \ No newline at end of file