This commit is contained in:
sin365 2024-01-22 15:22:47 +08:00
parent 4330dd92a5
commit feb9a305b9
2 changed files with 21 additions and 67 deletions

View File

@ -217,11 +217,9 @@ namespace HaoYueNet.ClientNetwork.OtherMode
return; return;
//断开连接 //断开连接
} }
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
while (true)
{
if (effective > 0)//如果接受到的消息不为0不为空 if (effective > 0)//如果接受到的消息不为0不为空
{ {
memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
DataCallBackReady(memoryStream.ToArray()); DataCallBackReady(memoryStream.ToArray());
//流复用的方式 不用重新new申请 //流复用的方式 不用重新new申请
memoryStream.Position = 0; memoryStream.Position = 0;
@ -229,7 +227,6 @@ namespace HaoYueNet.ClientNetwork.OtherMode
} }
} }
} }
}
public void LogOut(string Msg) public void LogOut(string Msg)

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;
@ -11,7 +12,7 @@ namespace HaoYueNet.ServerNetwork
protected int MaxRevIndexNum = 50;//响应倒计时计数最大值 protected int MaxRevIndexNum = 50;//响应倒计时计数最大值
protected int MaxSendIndexNum = 3;//发送倒计时计数最大值 protected int MaxSendIndexNum = 3;//发送倒计时计数最大值
protected static int TimerInterval = 3000;//计时器间隔 protected static int TimerInterval = 3000;//计时器间隔
protected System.Timers.Timer _heartTimer;//心跳包计数器 //protected System.Timers.Timer _heartTimer;//心跳包计数器
public int m_maxConnectNum; //最大连接数 public int m_maxConnectNum; //最大连接数
public int m_revBufferSize; //最大接收字节数 public int m_revBufferSize; //最大接收字节数
protected BufferManager m_bufferManager; protected BufferManager m_bufferManager;
@ -156,12 +157,12 @@ namespace HaoYueNet.ServerNetwork
OutNetLog("监听:" + listenSocket.LocalEndPoint.ToString()); OutNetLog("监听:" + listenSocket.LocalEndPoint.ToString());
_heartTimer = new System.Timers.Timer(); //_heartTimer = new System.Timers.Timer();
_heartTimer.Interval = TimerInterval; //_heartTimer.Interval = TimerInterval;
_heartTimer.Elapsed += CheckUpdatetimer_Elapsed; //_heartTimer.Elapsed += CheckUpdatetimer_Elapsed;
_heartTimer.AutoReset = true; //_heartTimer.AutoReset = true;
_heartTimer.Enabled = true; //_heartTimer.Enabled = true;
OutNetLog("开启定时心跳包"); //OutNetLog("开启定时心跳包");
return true; return true;
} }
@ -377,9 +378,6 @@ 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);
//lock (token.Buffer)
lock(token.memoryStream) lock(token.memoryStream)
{ {
//token.Buffer.AddRange(data); //token.Buffer.AddRange(data);
@ -387,58 +385,15 @@ namespace HaoYueNet.ServerNetwork
} }
do do
{ {
//如果包头不完整
//if (token.Buffer.Count < 4)
if (token.memoryStream.Length < 4)
break;
//判断包的长度 DataCallBackReady(token,token.memoryStream.ToArray());
//byte[] lenBytes = token.Buffer.GetRange(0, 4).ToArray(); //流复用的方式 不用重新new申请
//int packageLen = BitConverter.ToInt32(lenBytes, 0) - 4; token.memoryStream.Position = 0;
//if (packageLen > token.Buffer.Count - 4) token.memoryStream.SetLength(0);
//{ //长度不够时,退出循环,让程序继续接收
// 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;
if (packageLen > token.memoryStream.Length - 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)
//{
// token.Buffer.RemoveRange(0, packageLen + 4);
//}
token.memoryStream.Seek(FristBeginPos, SeekOrigin.Begin);
//从数据池中移除这组数据
lock (token.memoryStream)
{
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处理完后,并没有返回结果,当然结果是要返回的,却不是在这里, 这里的代码只管接收.
//若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了. //若要返回结果,可在API处理中调用此类对象的SendMessage方法,统一打包发送.不要被微软的示例给迷惑了.
//} while (token.Buffer.Count > 4); } while (token.memoryStream.Length > 0);
} while (token.memoryStream.Length > 4);
//继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明 //继续接收. 为什么要这么写,请看Socket.ReceiveAsync方法的说明
if (!token.Socket.ReceiveAsync(e)) if (!token.Socket.ReceiveAsync(e))
@ -656,6 +611,7 @@ namespace HaoYueNet.ServerNetwork
#endregion #endregion
#region #region
/*
/// <summary> /// <summary>
/// 发送心跳包 /// 发送心跳包
/// </summary> /// </summary>
@ -704,6 +660,7 @@ namespace HaoYueNet.ServerNetwork
} }
} }
} }
*/
#endregion #endregion
} }
} }