diff --git a/NetLib/HaoYueNet.ClientNetwork/OtherMode/NetworkHelperCore_ListenerMode.cs b/NetLib/HaoYueNet.ClientNetwork/OtherMode/NetworkHelperCore_ListenerMode.cs
new file mode 100644
index 0000000..d09d385
--- /dev/null
+++ b/NetLib/HaoYueNet.ClientNetwork/OtherMode/NetworkHelperCore_ListenerMode.cs
@@ -0,0 +1,194 @@
+using System.Net;
+using System.Net.Sockets;
+using static HaoYueNet.ClientNetwork.BaseData;
+
+namespace HaoYueNet.ClientNetwork.OtherMode
+{
+ public class NetworkHelperCore_ListenerMode
+ {
+ private Socket serversocket;
+ private Socket client;
+
+ //响应倒计时计数最大值
+ private static int MaxRevIndexNum = 50;
+
+ //发送倒计时计数最大值
+ private static int MaxSendIndexNum = 3;
+
+ //响应倒计时计数
+ private static int RevIndex = 0;
+ //发送倒计时计数
+ private static int SendIndex = 0;
+ //计时器间隔
+ private static int TimerInterval = 3000;
+
+ public static string LastConnectIP;
+ public static int LastConnectPort;
+ public bool bDetailedLog = false;
+
+ public void Init(int port)
+ {
+ LogOut("==>初始化NetworkHelperCore_ListenerMode");
+ serversocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
+ serversocket.Bind(endPoint); // 绑定
+ serversocket.Listen(1);
+ client = serversocket.Accept(); // 接收客户端连接
+ OnConnected?.Invoke(true);
+ Console.WriteLine("客户端连接成功 信息: " + client.AddressFamily.ToString());
+ Thread revThread = new Thread(Recive);
+ revThread.Start(client);
+ }
+
+ ~NetworkHelperCore_ListenerMode()
+ {
+ client.Close();
+ }
+
+ private void SendToSocket(byte[] data)
+ {
+ //已拼接包长度,这里不再需要拼接长度
+ //data = SendDataWithHead(data);
+ try
+ {
+ SendWithIndex(data);
+ }
+ catch (Exception ex)
+ {
+ //连接断开
+ OnCloseReady();
+ return;
+ }
+ //LogOut("发送消息,消息长度=> "+data.Length);
+ }
+
+ private void SendHeartbeat()
+ {
+ try
+ {
+ SendWithIndex(HeartbeatData);
+ }
+ catch (Exception ex)
+ {
+ //连接断开
+ OnCloseReady();
+ return;
+ }
+ //LogOut("发送心跳包");
+ }
+
+ ///
+ /// 发送数据并计数
+ ///
+ ///
+ private void SendWithIndex(byte[] data)
+ {
+ //增加发送计数
+ SendIndex = MaxSendIndexNum;
+ //发送数据
+ client.Send(data);
+ }
+
+ ///
+ /// 供外部调用 发送消息
+ ///
+ ///
+ /// 序列化之后的数据
+ public void SendToClient(byte[] data)
+ {
+ //LogOut("准备数据 data=> "+data);
+ SendToSocket(data);
+ }
+
+ #region 事件定义
+ public delegate void OnReceiveDataHandler(byte[] data);
+ public delegate void OnConnectedHandler(bool IsConnected);
+ public delegate void OnCloseHandler();
+ public delegate void OnLogOutHandler(string Msg);
+ #endregion
+
+ public event OnConnectedHandler OnConnected;
+ public event OnReceiveDataHandler OnReceiveData;
+ public event OnCloseHandler OnClose;
+ ///
+ /// 网络库调试日志输出
+ ///
+ public event OnLogOutHandler OnLogOut;
+
+ ///
+ /// 做好处理的连接管理
+ ///
+ private void OnCloseReady()
+ {
+ LogOut("关闭连接");
+ //关闭Socket连接
+ client.Close();
+ OnClose?.Invoke();
+ }
+
+ ///
+ /// 主动关闭连接
+ ///
+ public void CloseConntect()
+ {
+ OnCloseReady();
+ }
+
+ private void DataCallBackReady(byte[] data)
+ {
+ //增加接收计数
+ RevIndex = MaxRevIndexNum;
+ OnReceiveData(data);
+ }
+
+ MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
+ private void Recive(object o)
+ {
+ var client = o as Socket;
+ //MemoryStream memoryStream = new MemoryStream();//开辟一个内存流
+
+ while (true)
+ {
+ byte[] buffer = new byte[1024 * 1024 * 2];
+ int effective = 0;
+ try
+ {
+ effective = client.Receive(buffer);
+ if (effective == 0)
+ {
+ continue;
+ }
+ }
+ catch (Exception ex)
+ {
+ //远程主机强迫关闭了一个现有的连接
+ OnCloseReady();
+ return;
+ //断开连接
+ }
+ memoryStream.Write(buffer, 0, effective);//将接受到的数据写入内存流中
+ while (true)
+ {
+ if (effective > 0)//如果接受到的消息不为0(不为空)
+ {
+ DataCallBackReady(memoryStream.ToArray());
+ //流复用的方式 不用重新new申请
+ memoryStream.Position = 0;
+ memoryStream.SetLength(0);
+ }
+ }
+ }
+ }
+
+ public void LogOut(string Msg)
+ {
+ //Console.WriteLine(Msg);
+ OnLogOut?.Invoke(Msg);
+ }
+
+ public Socket GetClientSocket()
+ {
+ return client;
+ }
+ }
+}
diff --git a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore_SourceMode.cs b/NetLib/HaoYueNet.ClientNetwork/OtherMode/NetworkHelperCore_SourceMode.cs
similarity index 95%
rename from NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore_SourceMode.cs
rename to NetLib/HaoYueNet.ClientNetwork/OtherMode/NetworkHelperCore_SourceMode.cs
index e6a3c4d..b87f96b 100644
--- a/NetLib/HaoYueNet.ClientNetwork/NetworkHelperCore_SourceMode.cs
+++ b/NetLib/HaoYueNet.ClientNetwork/OtherMode/NetworkHelperCore_SourceMode.cs
@@ -2,7 +2,7 @@
using System.Net.Sockets;
using static HaoYueNet.ClientNetwork.BaseData;
-namespace HaoYueNet.ClientNetwork
+namespace HaoYueNet.ClientNetwork.OtherMode
{
public class NetworkHelperCore_SourceMode
{
@@ -21,9 +21,9 @@ namespace HaoYueNet.ClientNetwork
private static int MaxSendIndexNum = 3;
//响应倒计时计数
- private static int RevIndex=0;
+ private static int RevIndex = 0;
//发送倒计时计数
- private static int SendIndex=0;
+ private static int SendIndex = 0;
//计时器间隔
private static int TimerInterval = 3000;
@@ -32,7 +32,7 @@ namespace HaoYueNet.ClientNetwork
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)
{
LogOut("==>初始化网络核心");
@@ -57,7 +57,7 @@ namespace HaoYueNet.ClientNetwork
//带回调的
try
{
- if(bDetailedLog)
+ if (bDetailedLog)
LogOut("连接到远程IP " + IP + ":" + port);
else
LogOut("连接到远程服务");
@@ -184,7 +184,7 @@ namespace HaoYueNet.ClientNetwork
{
OnCloseReady();
}
-
+
private void DataCallBackReady(byte[] data)
{
//增加接收计数
@@ -201,7 +201,7 @@ namespace HaoYueNet.ClientNetwork
while (true)
{
byte[] buffer = new byte[1024 * 1024 * 2];
- int effective=0;
+ int effective = 0;
try
{
effective = client.Receive(buffer);
@@ -210,7 +210,7 @@ namespace HaoYueNet.ClientNetwork
continue;
}
}
- catch(Exception ex)
+ catch (Exception ex)
{
//远程主机强迫关闭了一个现有的连接
OnCloseReady();
diff --git a/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml b/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml
index 34e4f3a..2fe19f4 100644
--- a/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml
+++ b/NetLib/HaoYueNet.ClientNetwork/Properties/PublishProfiles/FolderProfile.pubxml
@@ -6,8 +6,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
Release
Any CPU
- bin\Release\net7.0\publish\
+ bin\Release\net7.0\publish\win-x64\
FileSystem
<_TargetId>Folder
+ net7.0
+ false
+ win-x64
+ false
+ false
\ No newline at end of file
diff --git a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs
index 808e313..95cfff2 100644
--- a/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs
+++ b/NetLib/HaoYueNet.ServerNetwork/NetWork/SourceMode/TcpSaeaServer_SourceMode.cs
@@ -39,7 +39,7 @@ namespace HaoYueNet.ServerNetwork
///
/// 客户端
/// 客户端数据
- public delegate void OnReceiveDataHandler(AsyncUserToken sk, int CMDID, byte[] data);
+ public delegate void OnReceiveDataHandler(AsyncUserToken sk, byte[] data);
///
/// 断开连接
///
@@ -491,18 +491,10 @@ namespace HaoYueNet.ServerNetwork
///
///
/// 序列化之后的数据
- public void SendToSocket(Socket sk, int CMDID, int ERRCODE, byte[] data)
+ public void SendToSocket(Socket sk, byte[] data)
{
AsyncUserToken token = GetAsyncUserTokenForSocket(sk);
- /*HunterNet_S2C _s2cdata = new HunterNet_S2C();
- _s2cdata.HunterNetCoreCmdID = CMDID;
- _s2cdata.HunterNetCoreData = ByteString.CopyFrom(data);
- _s2cdata.HunterNetCoreERRORCode = ERRCODE;
- byte[] _finaldata = Serizlize(_s2cdata);*/
-
- //byte[] _finaldata = HunterNet_S2C.CreatePkgData((ushort)CMDID, (ushort)ERRCODE, data);
-
- SendWithIndex(token, (ushort)CMDID, (ushort)ERRCODE, data);
+ SendWithIndex(token, data);
}
void SendForMsgPool()
{
@@ -588,12 +580,12 @@ namespace HaoYueNet.ServerNetwork
/// 发送数据并计数
///
///
- void SendWithIndex(AsyncUserToken token, UInt16 CmdID, UInt16 ERRCODE, byte[] data)
+ void SendWithIndex(AsyncUserToken token,byte[] data)
{
try
{
//发送数据
- SendMessage(token, CmdID, ERRCODE, data);
+ SendMessage(token, data);
token.SendIndex = MaxSendIndexNum;
}
catch
@@ -643,28 +635,14 @@ namespace HaoYueNet.ServerNetwork
//增加接收计数
sk.RevIndex = MaxRevIndexNum;
- if (data.Length == 1 && data[0] == 0x00)//心跳包
+ try
{
- //OutNetLog("收到心跳包");
- //无处理
+ //将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度.
+ OnReceive?.Invoke(sk, data);
}
- else
+ catch (Exception ex)
{
- try
- {
- //将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度.
- /*
- HunterNet_C2S _s2c = DeSerizlize(data);
- OnReceive?.Invoke(sk, (int)_s2c.HunterNetCoreCmdID, _s2c.HunterNetCoreData.ToArray());
- //DataCallBack(sk, (int)_s2c.HunterNetCoreCmdID, _s2c.HunterNetCoreData.ToArray());
- */
- HunterNet_C2S.AnalysisPkgData(data, out ushort CmdID, out byte[] resultdata);
- OnReceive?.Invoke(sk, CmdID, resultdata);
- }
- catch (Exception ex)
- {
- OutNetLog("数据解析错误");
- }
+ OutNetLog("数据解析错误");
}
}
private void OutNetLog(string msg)
diff --git a/NetLib/HaoYueNet.ServerNetwork/Properties/PublishProfiles/FolderProfile.pubxml b/NetLib/HaoYueNet.ServerNetwork/Properties/PublishProfiles/FolderProfile.pubxml
index 34e4f3a..ad97410 100644
--- a/NetLib/HaoYueNet.ServerNetwork/Properties/PublishProfiles/FolderProfile.pubxml
+++ b/NetLib/HaoYueNet.ServerNetwork/Properties/PublishProfiles/FolderProfile.pubxml
@@ -6,8 +6,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
Release
Any CPU
- bin\Release\net7.0\publish\
+ bin\Release\net7.0\publish\win-x64\
FileSystem
<_TargetId>Folder
+ net7.0
+ win-x64
+ false
+ false
+ false
\ No newline at end of file