..
This commit is contained in:
parent
d9534a1a24
commit
4211f40b6c
@ -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("发送心跳包");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送数据并计数
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="data"></param>
|
||||||
|
private void SendWithIndex(byte[] data)
|
||||||
|
{
|
||||||
|
//增加发送计数
|
||||||
|
SendIndex = MaxSendIndexNum;
|
||||||
|
//发送数据
|
||||||
|
client.Send(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 供外部调用 发送消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="CMDID"></param>
|
||||||
|
/// <param name="data">序列化之后的数据</param>
|
||||||
|
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;
|
||||||
|
/// <summary>
|
||||||
|
/// 网络库调试日志输出
|
||||||
|
/// </summary>
|
||||||
|
public event OnLogOutHandler OnLogOut;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 做好处理的连接管理
|
||||||
|
/// </summary>
|
||||||
|
private void OnCloseReady()
|
||||||
|
{
|
||||||
|
LogOut("关闭连接");
|
||||||
|
//关闭Socket连接
|
||||||
|
client.Close();
|
||||||
|
OnClose?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主动关闭连接
|
||||||
|
/// </summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using static HaoYueNet.ClientNetwork.BaseData;
|
using static HaoYueNet.ClientNetwork.BaseData;
|
||||||
|
|
||||||
namespace HaoYueNet.ClientNetwork
|
namespace HaoYueNet.ClientNetwork.OtherMode
|
||||||
{
|
{
|
||||||
public class NetworkHelperCore_SourceMode
|
public class NetworkHelperCore_SourceMode
|
||||||
{
|
{
|
||||||
@ -21,9 +21,9 @@ namespace HaoYueNet.ClientNetwork
|
|||||||
private static int MaxSendIndexNum = 3;
|
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;
|
private static int TimerInterval = 3000;
|
||||||
@ -32,7 +32,7 @@ namespace HaoYueNet.ClientNetwork
|
|||||||
public static int LastConnectPort;
|
public static int LastConnectPort;
|
||||||
public bool bDetailedLog = false;
|
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("==>初始化网络核心");
|
LogOut("==>初始化网络核心");
|
||||||
|
|
||||||
@ -57,7 +57,7 @@ namespace HaoYueNet.ClientNetwork
|
|||||||
//带回调的
|
//带回调的
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if(bDetailedLog)
|
if (bDetailedLog)
|
||||||
LogOut("连接到远程IP " + IP + ":" + port);
|
LogOut("连接到远程IP " + IP + ":" + port);
|
||||||
else
|
else
|
||||||
LogOut("连接到远程服务");
|
LogOut("连接到远程服务");
|
||||||
@ -184,7 +184,7 @@ namespace HaoYueNet.ClientNetwork
|
|||||||
{
|
{
|
||||||
OnCloseReady();
|
OnCloseReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void DataCallBackReady(byte[] data)
|
private void DataCallBackReady(byte[] data)
|
||||||
{
|
{
|
||||||
//增加接收计数
|
//增加接收计数
|
||||||
@ -201,7 +201,7 @@ namespace HaoYueNet.ClientNetwork
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
byte[] buffer = new byte[1024 * 1024 * 2];
|
byte[] buffer = new byte[1024 * 1024 * 2];
|
||||||
int effective=0;
|
int effective = 0;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
effective = client.Receive(buffer);
|
effective = client.Receive(buffer);
|
||||||
@ -210,7 +210,7 @@ namespace HaoYueNet.ClientNetwork
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch(Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
//远程主机强迫关闭了一个现有的连接
|
//远程主机强迫关闭了一个现有的连接
|
||||||
OnCloseReady();
|
OnCloseReady();
|
@ -6,8 +6,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration>Release</Configuration>
|
<Configuration>Release</Configuration>
|
||||||
<Platform>Any CPU</Platform>
|
<Platform>Any CPU</Platform>
|
||||||
<PublishDir>bin\Release\net7.0\publish\</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>
|
||||||
|
<SelfContained>false</SelfContained>
|
||||||
|
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||||
|
<PublishSingleFile>false</PublishSingleFile>
|
||||||
|
<PublishReadyToRun>false</PublishReadyToRun>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
@ -39,7 +39,7 @@ namespace HaoYueNet.ServerNetwork
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="token">客户端</param>
|
/// <param name="token">客户端</param>
|
||||||
/// <param name="buff">客户端数据</param>
|
/// <param name="buff">客户端数据</param>
|
||||||
public delegate void OnReceiveDataHandler(AsyncUserToken sk, int CMDID, byte[] data);
|
public delegate void OnReceiveDataHandler(AsyncUserToken sk, byte[] data);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 断开连接
|
/// 断开连接
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -491,18 +491,10 @@ namespace HaoYueNet.ServerNetwork
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="CMDID"></param>
|
/// <param name="CMDID"></param>
|
||||||
/// <param name="data">序列化之后的数据</param>
|
/// <param name="data">序列化之后的数据</param>
|
||||||
public void SendToSocket(Socket sk, int CMDID, int ERRCODE, byte[] data)
|
public void SendToSocket(Socket sk, byte[] data)
|
||||||
{
|
{
|
||||||
AsyncUserToken token = GetAsyncUserTokenForSocket(sk);
|
AsyncUserToken token = GetAsyncUserTokenForSocket(sk);
|
||||||
/*HunterNet_S2C _s2cdata = new HunterNet_S2C();
|
SendWithIndex(token, data);
|
||||||
_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);
|
|
||||||
}
|
}
|
||||||
void SendForMsgPool()
|
void SendForMsgPool()
|
||||||
{
|
{
|
||||||
@ -588,12 +580,12 @@ namespace HaoYueNet.ServerNetwork
|
|||||||
/// 发送数据并计数
|
/// 发送数据并计数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="data"></param>
|
/// <param name="data"></param>
|
||||||
void SendWithIndex(AsyncUserToken token, UInt16 CmdID, UInt16 ERRCODE, byte[] data)
|
void SendWithIndex(AsyncUserToken token,byte[] data)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//发送数据
|
//发送数据
|
||||||
SendMessage(token, CmdID, ERRCODE, data);
|
SendMessage(token, data);
|
||||||
token.SendIndex = MaxSendIndexNum;
|
token.SendIndex = MaxSendIndexNum;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@ -643,28 +635,14 @@ namespace HaoYueNet.ServerNetwork
|
|||||||
//增加接收计数
|
//增加接收计数
|
||||||
sk.RevIndex = MaxRevIndexNum;
|
sk.RevIndex = MaxRevIndexNum;
|
||||||
|
|
||||||
if (data.Length == 1 && data[0] == 0x00)//心跳包
|
try
|
||||||
{
|
{
|
||||||
//OutNetLog("收到心跳包");
|
//将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度.
|
||||||
//无处理
|
OnReceive?.Invoke(sk, data);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
try
|
OutNetLog("数据解析错误");
|
||||||
{
|
|
||||||
//将数据包交给后台处理,这里你也可以新开个线程来处理.加快速度.
|
|
||||||
/*
|
|
||||||
HunterNet_C2S _s2c = DeSerizlize<HunterNet_C2S>(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("数据解析错误");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void OutNetLog(string msg)
|
private void OutNetLog(string msg)
|
||||||
|
@ -6,8 +6,13 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
|
|||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Configuration>Release</Configuration>
|
<Configuration>Release</Configuration>
|
||||||
<Platform>Any CPU</Platform>
|
<Platform>Any CPU</Platform>
|
||||||
<PublishDir>bin\Release\net7.0\publish\</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>
|
||||||
|
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||||
|
<SelfContained>false</SelfContained>
|
||||||
|
<PublishSingleFile>false</PublishSingleFile>
|
||||||
|
<PublishReadyToRun>false</PublishReadyToRun>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
</Project>
|
</Project>
|
Loading…
Reference in New Issue
Block a user