MemoryStream合理使用

This commit is contained in:
sin365 2024-04-07 17:00:19 +08:00
parent f2e0279fd9
commit 2efb0771fd
4 changed files with 68 additions and 65 deletions

View File

@ -9,7 +9,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PublishDir>bin\Release\net7.0\publish\win-x64\</PublishDir> <PublishDir>bin\Release\net7.0\publish\win-x64\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol> <PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId> <_TargetId>Folder</_TargetId>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<SelfContained>false</SelfContained> <SelfContained>false</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier> <RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>false</PublishSingleFile> <PublishSingleFile>false</PublishSingleFile>

View File

@ -33,14 +33,14 @@ namespace HaoYueNet.ServerNetwork
/// <summary> /// <summary>
/// 数据缓存区 /// 数据缓存区
/// </summary> /// </summary>
public List<byte> Buffer { get; set; } //public List<byte> Buffer { get; set; }
//public MemoryStream memoryStream { get; set; } public MemoryStream memoryStream { get; set; }
public AsyncUserToken() public AsyncUserToken()
{ {
this.Buffer = new List<byte>(); //this.Buffer = new List<byte>();
//this.memoryStream = new MemoryStream(); this.memoryStream = new MemoryStream();
} }
/// <summary> /// <summary>
/// 响应倒计时计数 /// 响应倒计时计数

View File

@ -378,34 +378,32 @@ namespace HaoYueNet.ServerNetwork
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{ {
//读取数据 //读取数据
//byte[] data = new byte[e.BytesTransferred];
//读取数据 //Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred);
byte[] data = new byte[e.BytesTransferred]; //lock (token.Buffer)
Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); lock(token.memoryStream)
lock (token.Buffer)
//lock(token.memoryStream)
{ {
token.Buffer.AddRange(data); //token.Buffer.AddRange(data);
//token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred); token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred);
do do
{ {
DataCallBackReady(token, data); //DataCallBackReady(token, data);
//从数据池中移除这组数据 ////从数据池中移除这组数据
lock (token.Buffer) //lock (token.Buffer)
{ //{
token.Buffer.Clear(); // token.Buffer.Clear();
} //}
//DataCallBackReady(token, token.memoryStream.ToArray()); DataCallBackReady(token, token.memoryStream.ToArray());
////流复用的方式 不用重新new申请 //流复用的方式 不用重新new申请
//token.memoryStream.Position = 0; token.memoryStream.Position = 0;
//token.memoryStream.SetLength(0); token.memoryStream.SetLength(0);
//这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收. //这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收.
//若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了. //若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了.
//} while (token.memoryStream.Length > 0); } while (token.memoryStream.Length > 0);
} while (token.Buffer.Count > 4); //} while (token.Buffer.Count > 4);
} }
//继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明 //继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明

View File

@ -1,4 +1,5 @@
//using HunterProtobufCore; //using HunterProtobufCore;
using System.IO;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using static HaoYueNet.ServerNetwork.BaseData; using static HaoYueNet.ServerNetwork.BaseData;
@ -378,67 +379,71 @@ namespace HaoYueNet.ServerNetwork
if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success) if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
{ {
//读取数据 //读取数据
byte[] data = new byte[e.BytesTransferred]; //byte[] data = new byte[e.BytesTransferred];
Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); //Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred);
lock (token.Buffer) //lock (token.Buffer)
//lock(token.memoryStream) lock(token.memoryStream)
{ {
token.Buffer.AddRange(data); //token.Buffer.AddRange(data);
//token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred); token.memoryStream.Write(e.Buffer, e.Offset, e.BytesTransferred);
do do
{ {
//如果包头不完整 //如果包头不完整
if (token.Buffer.Count < 4) //if (token.Buffer.Count < 4)
//if (token.memoryStream.Length < 4) if (token.memoryStream.Length < 4)
break; break;
//判断包的长度 ////判断包的长度
byte[] lenBytes = token.Buffer.GetRange(0, 4).ToArray(); //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);
//int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; //int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4;
//if (packageLen > token.memoryStream.Length - 4) //if (packageLen > token.Buffer.Count - 4)
//{ //长度不够时,退出循环,让程序继续接收 //{ //长度不够时,退出循环,让程序继续接收
// break; // break;
//} //}
//包够长时,则提取出来,交给后面的程序去处理 long FristBeginPos = token.memoryStream.Position;
byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray(); byte[] lenBytes = new byte[4];
token.memoryStream.Seek(0, SeekOrigin.Begin);
//byte[] rev = new byte[packageLen]; token.memoryStream.Read(lenBytes, 0, 4);
//token.memoryStream.Seek(4, SeekOrigin.Begin); int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4;
//token.memoryStream.Read(rev, 0, packageLen); if (packageLen > token.memoryStream.Length - 4)
//从数据池中移除这组数据
lock (token.Buffer)
{ {
token.Buffer.RemoveRange(0, packageLen + 4); token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin);
//长度不够时,退出循环,让程序继续接收
break;
} }
//token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin); ////包够长时,则提取出来,交给后面的程序去处理
////从数据池中移除这组数据 //byte[] rev = token.Buffer.GetRange(4, packageLen).ToArray();
//lock (token.memoryStream)
byte[] rev = new byte[packageLen];
token.memoryStream.Seek(4, SeekOrigin.Begin);
token.memoryStream.Read(rev, 0, packageLen);
//从数据池中移除这组数据
//lock (token.Buffer)
//{ //{
// int numberOfBytesToRemove = packageLen + 4; // token.Buffer.RemoveRange(0, 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.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); DataCallBackReady(token, rev);
//这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收. //这里API处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收.
//若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了. //若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了.
} while (token.Buffer.Count > 4); //} while (token.Buffer.Count > 4);
//} while (token.memoryStream.Length > 4); } while (token.memoryStream.Length > 4);
} }
//继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明 //继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明