From 67f9c51d6fdf69a5d78fed4255fad2826c4ad0f5 Mon Sep 17 00:00:00 2001 From: sin365 <353374337@qq.com> Date: Wed, 5 Nov 2025 11:52:07 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E6=95=B4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HaoYueNet.ClientNetwork.Standard2.csproj | 3 ++ .../NetworkHelperCore_ListenerMode.cs | 29 +++++++++++++------ .../OtherMode/NetworkHelperCore_SourceMode.cs | 12 ++++---- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetwork.Standard2.csproj b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetwork.Standard2.csproj index 0fc00d7..6730259 100644 --- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetwork.Standard2.csproj +++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/HaoYueNet.ClientNetwork.Standard2.csproj @@ -6,4 +6,7 @@ 8.0 + + + \ No newline at end of file diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs index 29d6bf3..0e5ee1f 100644 --- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs +++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_ListenerMode.cs @@ -1,4 +1,5 @@ using System; +using System.Buffers; using System.Collections.Generic; using System.IO; using System.Linq; @@ -6,13 +7,14 @@ using System.Net; using System.Net.Sockets; using System.Threading; using System.Threading.Tasks; +using static HaoYueNet.ClientNetwork.BaseData; -namespace HaoYueNet.ClientNetwork.Standard2.OtherMode +namespace HaoYueNet.ClientNetwork.OtherMode { public class NetworkHelperCore_ListenerMode { private Socket serversocket; - private Dictionary mDictHandleClient; + private Dictionary mDictHandleClient; //响应倒计时计数最大值 private static int MaxRevIndexNum = 50; @@ -31,12 +33,12 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode public static int LastConnectPort; public bool bDetailedLog = false; - public void Init(int port) + public void Init(int port, AddressFamily addressFamily = AddressFamily.InterNetwork) { - mDictHandleClient = new Dictionary(); + mDictHandleClient = new Dictionary(); LogOut("==>初始化NetworkHelperCore_ListenerMode"); - serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + serversocket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp); IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port); serversocket.Bind(endPoint); // 绑定 serversocket.Listen(1); @@ -101,7 +103,7 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode ~NetworkHelperCore_ListenerMode() { - nint[] keys = mDictHandleClient.Keys.ToArray(); + IntPtr[] keys = mDictHandleClient.Keys.ToArray(); for (uint i = 0; i < keys.Length; i++) { mDictHandleClient[keys[i]].Close(); @@ -195,17 +197,20 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode } MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流 - byte[] reciveBuffer = new byte[1024 * 1024 * 2]; + //byte[] reciveBuffer = new byte[1024 * 1024 * 2]; private void Recive(object o) { var client = o as Socket; while (true) { + //申请byte池 一定要记得回收!! + byte[] rev_fromArrayPool = ArrayPool.Shared.Rent(1024 * 1024 * 2); + int effective = 0; try { - effective = client.Receive(reciveBuffer); + effective = client.Receive(rev_fromArrayPool); if (effective == 0)//为0表示已经断开连接,放到后面处理 { //清理数据 @@ -213,6 +218,8 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode reciveMemoryStream.Seek(0, SeekOrigin.Begin); //远程主机强迫关闭了一个现有的连接 OnCloseReady(client); + //回收 + ArrayPool.Shared.Return(rev_fromArrayPool); return; } } @@ -228,7 +235,11 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode //断开连接 } - reciveMemoryStream.Write(reciveBuffer, 0, effective);//将接受到的数据写入内存流中 + reciveMemoryStream.Write(rev_fromArrayPool, 0, effective);//将接受到的数据写入内存流中 + + //回收 + ArrayPool.Shared.Return(rev_fromArrayPool); + DataCallBackReady(client, reciveMemoryStream.ToArray()); //流复用的方式 不用重新new申请 reciveMemoryStream.Position = 0; diff --git a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs index 861224e..c3b2716 100644 --- a/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs +++ b/NetLib_Standard2/HaoYueNet.ClientNetworkNet.Standard2/OtherMode/NetworkHelperCore_SourceMode.cs @@ -3,9 +3,9 @@ using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; -using static HaoYueNet.ClientNetwork.Standard2.BaseData; +using static HaoYueNet.ClientNetwork.BaseData; -namespace HaoYueNet.ClientNetwork.Standard2.OtherMode +namespace HaoYueNet.ClientNetwork.OtherMode { public class NetworkHelperCore_SourceMode { @@ -35,7 +35,7 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode public static int LastConnectPort; public bool bDetailedLog = false; - public bool Init(string IP, int port, bool isHadDetailedLog = true, bool bBindReuseAddress = false, int bBindport = 0) + public bool Init(string IP, int port, bool isHadDetailedLog = true, bool bBindReuseAddress = false, int bBindport = 0, AddressFamily addressFamily = AddressFamily.InterNetwork) { LogOut("==>初始化网络核心"); @@ -43,7 +43,7 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode RevIndex = MaxRevIndexNum; SendIndex = MaxSendIndexNum; - client = new Socket(SocketType.Stream, ProtocolType.Tcp); + client = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp); if (bBindReuseAddress) { client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); @@ -195,8 +195,8 @@ namespace HaoYueNet.ClientNetwork.Standard2.OtherMode OnReceiveData(data); } - MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个内存流 - byte[] reciveBuffer = new byte[1024 * 1024 * 2]; + MemoryStream reciveMemoryStream = new MemoryStream();//开辟一个反复使用的内存流 + byte[] reciveBuffer = new byte[1024 * 1024 * 2];//开辟一个反复使用的byte[] private void Recive(object o) { var client = o as Socket;