diff --git a/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml b/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml index 2fe19f4..5c21b3d 100644 --- a/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml +++ b/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml @@ -9,7 +9,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121. bin\Release\net7.0\publish\win-x64\ FileSystem <_TargetId>Folder - net7.0 + net8.0 false win-x64 false diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs index 9215aca..1b6df2c 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/AsyncUserToken.cs @@ -33,14 +33,14 @@ namespace HaoYueNet.ServerNetwork /// /// 数据缓存区 /// - public List Buffer { get; set; } + //public List Buffer { get; set; } - //public MemoryStream memoryStream { get; set; } + public MemoryStream memoryStream { get; set; } public AsyncUserToken() { - this.Buffer = new List(); - //this.memoryStream = new MemoryStream(); + //this.Buffer = new List(); + this.memoryStream = new MemoryStream(); } /// /// 响应倒计时计数 diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs index e760ef7..fc30b71 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs @@ -378,34 +378,32 @@ namespace HaoYueNet.ServerNetwork if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { //读取数据 - - //读取数据 - byte[] data = new byte[e.BytesTransferred]; - Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); - lock (token.Buffer) - //lock(token.memoryStream) + //byte[] data = new byte[e.BytesTransferred]; + //Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); + //lock (token.Buffer) + lock(token.memoryStream) { - token.Buffer.AddRange(data); - //token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred); + //token.Buffer.AddRange(data); + token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred); do { - DataCallBackReady(token, data); - //从数据池中移除这组数据 - lock (token.Buffer) - { - token.Buffer.Clear(); - } + //DataCallBackReady(token, data); + ////从数据池中移除这组数据 + //lock (token.Buffer) + //{ + // token.Buffer.Clear(); + //} - //DataCallBackReady(token, token.memoryStream.ToArray()); - ////流复用的方式 不用重新new申请 - //token.memoryStream.Position = 0; - //token.memoryStream.SetLength(0); + DataCallBackReady(token, token.memoryStream.ToArray()); + //流复用的方式 不用重新new申请 + token.memoryStream.Position = 0; + token.memoryStream.SetLength(0); //这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收. //若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了. - //} while (token.memoryStream.Length > 0); - } while (token.Buffer.Count > 4); + } while (token.memoryStream.Length > 0); + //} while (token.Buffer.Count > 4); } //继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明 diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs index 56f473e..039aad7 100644 --- a/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs +++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/TcpSaeaServer.cs @@ -1,4 +1,5 @@ //using HunterProtobufCore; +using System.IO; using System.Net; using System.Net.Sockets; using static HaoYueNet.ServerNetwork.BaseData; @@ -378,67 +379,71 @@ namespace HaoYueNet.ServerNetwork if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) { //读取数据 - byte[] data = new byte[e.BytesTransferred]; - Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); - lock (token.Buffer) - //lock(token.memoryStream) + //byte[] data = new byte[e.BytesTransferred]; + //Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); + //lock (token.Buffer) + lock(token.memoryStream) { - token.Buffer.AddRange(data); - //token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred); + //token.Buffer.AddRange(data); + token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred); do { //如果包头不完整 - if (token.Buffer.Count < 4) - //if (token.memoryStream.Length < 4) + //if (token.Buffer.Count < 4) + if (token.memoryStream.Length < 4) break; - //判断包的长度 - byte[] lenBytes = token.Buffer.GetRange(0, 4).ToArray(); - int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; - if (packageLen > token.Buffer.Count - 4) - { //长度不够时,退出循环,让程序继续接收 - break; - } - - //long FristBeginPos = token.memoryStream.Position; - //byte[] lenBytes = new byte[4]; - //token.memoryStream.Seek(0, SeekOrigin.Begin); - //token.memoryStream.Read(lenBytes, 0, 4); + ////判断包的长度 + //byte[] lenBytes = token.Buffer.GetRange(0, 4).ToArray(); //int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; - //if (packageLen > token.memoryStream.Length - 4) + //if (packageLen > token.Buffer.Count - 4) //{ //长度不够时,退出循环,让程序继续接收 // break; //} - //包够长时,则提取出来,交给后面的程序去处理 - byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray(); - - //byte[] rev = new byte[packageLen]; - //token.memoryStream.Seek(4, SeekOrigin.Begin); - //token.memoryStream.Read(rev, 0, packageLen); - - //从数据池中移除这组数据 - lock (token.Buffer) + long FristBeginPos = token.memoryStream.Position; + byte[] lenBytes = new byte[4]; + token.memoryStream.Seek(0, SeekOrigin.Begin); + token.memoryStream.Read(lenBytes, 0, 4); + int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; + if (packageLen > token.memoryStream.Length - 4) { - token.Buffer.RemoveRange(0, packageLen + 4); + token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin); + //长度不够时,退出循环,让程序继续接收 + break; } - //token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin); - ////从数据池中移除这组数据 - //lock (token.memoryStream) + ////包够长时,则提取出来,交给后面的程序去处理 + //byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray(); + + byte[] rev = new byte[packageLen]; + token.memoryStream.Seek(4, SeekOrigin.Begin); + token.memoryStream.Read(rev, 0, packageLen); + + //从数据池中移除这组数据 + //lock (token.Buffer) //{ - // int numberOfBytesToRemove = packageLen + 4; - // byte[] buf = token.memoryStream.GetBuffer(); - // Buffer.BlockCopy(buf, numberOfBytesToRemove, buf, 0, (int)token.memoryStream.Length - numberOfBytesToRemove); - // token.memoryStream.SetLength(token.memoryStream.Length - numberOfBytesToRemove); + // token.Buffer.RemoveRange(0, packageLen + 4); //} + token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin); + //从数据池中移除这组数据 + lock (token.memoryStream) + { + //token.memoryStream.Position = 0; + //token.memoryStream.SetLength(0); + int numberOfBytesToRemove = packageLen + 4; + byte[] buf = token.memoryStream.GetBuffer(); + Buffer.BlockCopy(buf, numberOfBytesToRemove, buf, 0, (int)token.memoryStream.Length - numberOfBytesToRemove); + token.memoryStream.SetLength(token.memoryStream.Length - numberOfBytesToRemove); + } + DataCallBackReady(token, rev); //这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收. //若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了. - } while (token.Buffer.Count > 4); - //} while (token.memoryStream.Length > 4); + //} while (token.Buffer.Count > 4); + } while (token.memoryStream.Length > 4); } //继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明