追加.Net4传统类型类库的支持
This commit is contained in:
parent
d4f811780b
commit
155df3f1e9
Binary file not shown.
@ -30,7 +30,27 @@
|
|||||||
"ProjectGuid": "696f6aae-2d41-48f7-8fcd-e95cfe2a6519",
|
"ProjectGuid": "696f6aae-2d41-48f7-8fcd-e95cfe2a6519",
|
||||||
"DisplayName": "HaoYueNet.ClientNetwork",
|
"DisplayName": "HaoYueNet.ClientNetwork",
|
||||||
"ColorIndex": 5
|
"ColorIndex": 5
|
||||||
|
},
|
||||||
|
"33d77124-2d10-4799-b432-dcc8f0f3d1ca": {
|
||||||
|
"ProjectGuid": "33d77124-2d10-4799-b432-dcc8f0f3d1ca",
|
||||||
|
"DisplayName": "SimpleServer",
|
||||||
|
"ColorIndex": 6
|
||||||
|
},
|
||||||
|
"c558aca4-e59e-4f4d-94b7-2b78da272cf3": {
|
||||||
|
"ProjectGuid": "c558aca4-e59e-4f4d-94b7-2b78da272cf3",
|
||||||
|
"DisplayName": "SimpleClient",
|
||||||
|
"ColorIndex": 7
|
||||||
|
},
|
||||||
|
"a2cad164-0816-4d1d-9793-1b1f398c9d29": {
|
||||||
|
"ProjectGuid": "a2cad164-0816-4d1d-9793-1b1f398c9d29",
|
||||||
|
"DisplayName": "Simple",
|
||||||
|
"ColorIndex": 8
|
||||||
|
},
|
||||||
|
"5559b94a-da9e-4cf7-b799-2bcaa1f11f49": {
|
||||||
|
"ProjectGuid": "5559b94a-da9e-4cf7-b799-2bcaa1f11f49",
|
||||||
|
"DisplayName": "SimpleServer",
|
||||||
|
"ColorIndex": 9
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"NextColorIndex": 6
|
"NextColorIndex": 10
|
||||||
}
|
}
|
Binary file not shown.
@ -11,9 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ServerNetwork", "
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ClientNetwork", "NetLib\HaoYueNet.ClientNetwork\HaoYueNet.ClientNetwork.csproj", "{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HaoYueNet.ClientNetwork", "NetLib\HaoYueNet.ClientNetwork\HaoYueNet.ClientNetwork.csproj", "{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleClient", "Simple\SimpleClient\SimpleClient.csproj", "{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleClient", "Simple\SimpleClient\SimpleClient.csproj", "{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleServer", "Simple\SimpleServer\SimpleServer.csproj", "{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleServer", "Simple\SimpleServer\SimpleServer.csproj", "{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HaoYueNet.ClientNetworkNet4x", "NetLib\HaoYueNet.ClientNetworkNet4x\HaoYueNet.ClientNetworkNet4x.csproj", "{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@ -37,6 +39,10 @@ Global
|
|||||||
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Release|Any CPU.Build.0 = Release|Any CPU
|
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
@ -46,6 +52,7 @@ Global
|
|||||||
{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519} = {D0066C06-A89A-4E05-80E0-D8232FB0FF3C}
|
{696F6AAE-2D41-48F7-8FCD-E95CFE2A6519} = {D0066C06-A89A-4E05-80E0-D8232FB0FF3C}
|
||||||
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
|
{C558ACA4-E59E-4F4D-94B7-2B78DA272CF3} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
|
||||||
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
|
{5559B94A-DA9E-4CF7-B799-2BCAA1F11F49} = {A2CAD164-0816-4D1D-9793-1B1F398C9D29}
|
||||||
|
{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC} = {D0066C06-A89A-4E05-80E0-D8232FB0FF3C}
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
SolutionGuid = {22107F03-013F-4871-AC8E-F082694E2679}
|
SolutionGuid = {22107F03-013F-4871-AC8E-F082694E2679}
|
||||||
|
@ -0,0 +1,74 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||||
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||||
|
<ProjectGuid>{27E139E2-F5F5-47DF-99D8-04DC7EBEBEEC}</ProjectGuid>
|
||||||
|
<OutputType>Library</OutputType>
|
||||||
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
|
<RootNamespace>HaoYueNet.ClientNetworkNet4x</RootNamespace>
|
||||||
|
<AssemblyName>HaoYueNet.ClientNetworkNet4x</AssemblyName>
|
||||||
|
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||||
|
<FileAlignment>512</FileAlignment>
|
||||||
|
<Deterministic>true</Deterministic>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||||
|
<DebugSymbols>true</DebugSymbols>
|
||||||
|
<DebugType>full</DebugType>
|
||||||
|
<Optimize>false</Optimize>
|
||||||
|
<OutputPath>bin\Debug\</OutputPath>
|
||||||
|
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||||
|
<DebugType>pdbonly</DebugType>
|
||||||
|
<Optimize>true</Optimize>
|
||||||
|
<OutputPath>bin\Release\</OutputPath>
|
||||||
|
<DefineConstants>TRACE</DefineConstants>
|
||||||
|
<ErrorReport>prompt</ErrorReport>
|
||||||
|
<WarningLevel>4</WarningLevel>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Reference Include="protobuf-net, Version=3.0.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\protobuf-net.3.0.101\lib\net461\protobuf-net.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="protobuf-net.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=257b51d87d2e4d67, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\protobuf-net.Core.3.0.101\lib\net461\protobuf-net.Core.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System" />
|
||||||
|
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Collections.Immutable, Version=1.2.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\System.Collections.Immutable.1.7.1\lib\net461\System.Collections.Immutable.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Core" />
|
||||||
|
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\System.Memory.4.5.4\lib\net461\System.Memory.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Numerics" />
|
||||||
|
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\System.Runtime.CompilerServices.Unsafe.4.5.3\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
|
<Reference Include="System.Xml.Linq" />
|
||||||
|
<Reference Include="System.Data.DataSetExtensions" />
|
||||||
|
<Reference Include="Microsoft.CSharp" />
|
||||||
|
<Reference Include="System.Data" />
|
||||||
|
<Reference Include="System.Net.Http" />
|
||||||
|
<Reference Include="System.Xml" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="NetworkHelperCore.cs" />
|
||||||
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
<Compile Include="protobuf_HunterNetCore.cs" />
|
||||||
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="packages.config" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
</Project>
|
360
NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
Normal file
360
NetLib/HaoYueNet.ClientNetworkNet4x/NetworkHelperCore.cs
Normal file
@ -0,0 +1,360 @@
|
|||||||
|
using HunterProtobufCore;
|
||||||
|
using ProtoBuf;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace HaoYueNet.ClientNetworkNet4x
|
||||||
|
{
|
||||||
|
public class NetworkHelperCore
|
||||||
|
{
|
||||||
|
private Socket client;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 心跳包数据
|
||||||
|
/// </summary>
|
||||||
|
private byte[] HeartbeatData = new byte[5] { 0x05, 0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
////响应倒计时计数最大值
|
||||||
|
//private static int MaxRevIndexNum = 6;
|
||||||
|
|
||||||
|
////发送倒计时计数最大值
|
||||||
|
//private static int MaxSendIndexNum = 3;
|
||||||
|
|
||||||
|
//响应倒计时计数最大值
|
||||||
|
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;
|
||||||
|
|
||||||
|
private System.Timers.Timer _heartTimer;
|
||||||
|
|
||||||
|
public void Init(string IP, int port)
|
||||||
|
{
|
||||||
|
|
||||||
|
LogOut("==>初始化网络核心");
|
||||||
|
|
||||||
|
RevIndex = MaxRevIndexNum;
|
||||||
|
SendIndex = MaxSendIndexNum;
|
||||||
|
|
||||||
|
client = new Socket(SocketType.Stream, ProtocolType.Tcp);
|
||||||
|
//IPAddress ip = IPAddress.Parse(IP);
|
||||||
|
//IPEndPoint point = new IPEndPoint(ip, port);
|
||||||
|
|
||||||
|
LogOut("连接到IP "+ IP + ":"+ port);
|
||||||
|
|
||||||
|
//带回调的
|
||||||
|
//client.BeginConnect(ip, port, new AsyncCallback(CallBackMethod) , client);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Connect(IP, port);
|
||||||
|
Thread thread = new Thread(Recive);
|
||||||
|
thread.IsBackground = true;
|
||||||
|
thread.Start(client);
|
||||||
|
LogOut("连接成功!");
|
||||||
|
|
||||||
|
_heartTimer = new System.Timers.Timer();
|
||||||
|
_heartTimer.Interval = TimerInterval;
|
||||||
|
_heartTimer.Elapsed += CheckUpdatetimer_Elapsed;
|
||||||
|
_heartTimer.AutoReset = true;
|
||||||
|
_heartTimer.Enabled = true;
|
||||||
|
LogOut("开启心跳包检测");
|
||||||
|
|
||||||
|
OnConnected(true);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
OnConnected(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~NetworkHelperCore()
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//拼接头长度
|
||||||
|
private byte[] SendDataWithHead(byte[] message)
|
||||||
|
{
|
||||||
|
|
||||||
|
MemoryStream memoryStream = new MemoryStream();//创建一个内存流
|
||||||
|
|
||||||
|
byte[] BagHead = BitConverter.GetBytes(message.Length + 4);//往字节数组中写入包头(包头自身的长度和消息体的长度)的长度
|
||||||
|
|
||||||
|
memoryStream.Write(BagHead, 0, BagHead.Length);//将包头写入内存流
|
||||||
|
|
||||||
|
memoryStream.Write(message, 0, message.Length);//将消息体写入内存流
|
||||||
|
|
||||||
|
byte[] HeadAndBody = memoryStream.ToArray();//将内存流中的数据写入字节数组
|
||||||
|
|
||||||
|
memoryStream.Close();//关闭内存
|
||||||
|
memoryStream.Dispose();//释放资源
|
||||||
|
|
||||||
|
return HeadAndBody;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 供外部调用 发送消息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="CMDID"></param>
|
||||||
|
/// <param name="data">序列化之后的数据</param>
|
||||||
|
public void SendToServer(int CMDID,byte[] data)
|
||||||
|
{
|
||||||
|
LogOut("准备数据 CMDID=> "+CMDID);
|
||||||
|
HunterNet_C2S _c2sdata = new HunterNet_C2S();
|
||||||
|
_c2sdata.HunterNetCore_CmdID = CMDID;
|
||||||
|
_c2sdata.HunterNetCore_Data = data;
|
||||||
|
byte[] _finaldata = Serizlize<HunterNet_C2S>(_c2sdata);
|
||||||
|
SendToSocket(_finaldata);
|
||||||
|
}
|
||||||
|
|
||||||
|
public delegate void OnDataCallBack_Data(int CMDID, int ERRCODE, byte[] data);
|
||||||
|
|
||||||
|
public event OnDataCallBack_Data OnDataCallBack;
|
||||||
|
|
||||||
|
public delegate void delegate_NoData();
|
||||||
|
|
||||||
|
public delegate void delegate_Bool(bool IsConnected);
|
||||||
|
|
||||||
|
public event delegate_NoData OnClose;
|
||||||
|
|
||||||
|
public event delegate_Bool OnConnected;
|
||||||
|
|
||||||
|
public delegate void delegate_str(string Msg);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 网络库调试日志输出
|
||||||
|
/// </summary>
|
||||||
|
public event delegate_str OnLogOut;
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
///// 用于调用者回调的虚函数
|
||||||
|
///// </summary>
|
||||||
|
///// <param name="data"></param>
|
||||||
|
//public virtual void DataCallBack(int CMDID,int ERRCODE,byte[] data)
|
||||||
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
///// <summary>
|
||||||
|
///// 断开连接
|
||||||
|
///// </summary>
|
||||||
|
///// <param name="sk"></param>
|
||||||
|
//public virtual void OnClose()
|
||||||
|
//{
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 做好处理的连接管理
|
||||||
|
/// </summary>
|
||||||
|
private void OnCloseReady()
|
||||||
|
{
|
||||||
|
LogOut("关闭心跳包计数");
|
||||||
|
_heartTimer.Enabled = false;
|
||||||
|
LogOut("关闭连接");
|
||||||
|
OnClose();
|
||||||
|
//关闭Socket连接
|
||||||
|
client.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 主动关闭连接
|
||||||
|
/// </summary>
|
||||||
|
public void CloseConntect()
|
||||||
|
{
|
||||||
|
OnCloseReady();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DataCallBackReady(byte[] data)
|
||||||
|
{
|
||||||
|
|
||||||
|
//增加接收计数
|
||||||
|
RevIndex = MaxRevIndexNum;
|
||||||
|
|
||||||
|
//不处理心跳包
|
||||||
|
if (data.Length == 1 && data[0] == 0x00)
|
||||||
|
{
|
||||||
|
LogOut("收到心跳包");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
HunterNet_S2C _c2s = DeSerizlize<HunterNet_S2C>(data);
|
||||||
|
|
||||||
|
OnDataCallBack((int)_c2s.HunterNetCore_CmdID, (int)_c2s.HunterNetCore_ERRORCode, _c2s.HunterNetCore_Data);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);//将接受到的数据写入内存流中
|
||||||
|
byte[] getData = memoryStream.ToArray();//将内存流中的消息体写入字节数组
|
||||||
|
int StartIndex = 0;//设置一个读取数据的起始下标
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
if (effective > 0)//如果接受到的消息不为0(不为空)
|
||||||
|
{
|
||||||
|
int HeadLength = 0;//包头长度(包头+包体)
|
||||||
|
if (getData.Length - StartIndex < 4)//包头接受不完整
|
||||||
|
{
|
||||||
|
HeadLength = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//如果包头接受完整 转换成int类型的数值
|
||||||
|
HeadLength = BitConverter.ToInt32(getData, StartIndex);
|
||||||
|
}
|
||||||
|
//包头接受完整但是消息体不完整 //包头接受不完整
|
||||||
|
//↓↓↓↓↓↓↓↓ ↓↓↓
|
||||||
|
if (getData.Length - StartIndex < HeadLength || HeadLength == -1)
|
||||||
|
{
|
||||||
|
memoryStream.Close();//关闭内存流
|
||||||
|
memoryStream.Dispose();//释放内存资源
|
||||||
|
memoryStream = new MemoryStream();//创建新的内存流
|
||||||
|
memoryStream.Write(getData, StartIndex, getData.Length - StartIndex);//从新将接受的消息写入内存流
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//把头去掉,就可以吃了,蛋白质是牛肉的六倍
|
||||||
|
DataCallBackReady(getData.Skip(StartIndex+4).Take(HeadLength-4).ToArray());
|
||||||
|
StartIndex += HeadLength;//当读取一条完整的数据后,读取数据的起始下标应为当前接受到的消息体的长度(当前数据的尾部或下一条消息的首部)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CheckUpdatetimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
|
||||||
|
{
|
||||||
|
//接收服务器数据计数
|
||||||
|
RevIndex--;
|
||||||
|
if (RevIndex <= 0)
|
||||||
|
{
|
||||||
|
//判定掉线
|
||||||
|
OnCloseReady();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//发送计数
|
||||||
|
SendIndex--;
|
||||||
|
if (SendIndex <= 0)//需要发送心跳包了
|
||||||
|
{
|
||||||
|
//重置倒计时计数
|
||||||
|
SendIndex = MaxSendIndexNum;
|
||||||
|
|
||||||
|
SendHeartbeat();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] Serizlize<T>(T MsgObj)
|
||||||
|
{
|
||||||
|
using (MemoryStream ms = new MemoryStream())
|
||||||
|
{
|
||||||
|
Serializer.Serialize<T>(ms, MsgObj);
|
||||||
|
byte[] data1 = ms.ToArray();
|
||||||
|
return data1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T DeSerizlize<T>(byte[] MsgObj)
|
||||||
|
{
|
||||||
|
using (MemoryStream ms = new MemoryStream(MsgObj))
|
||||||
|
{
|
||||||
|
var ds_obj = Serializer.Deserialize<T>(ms);
|
||||||
|
return ds_obj;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LogOut(string Msg)
|
||||||
|
{
|
||||||
|
//Console.WriteLine(Msg);
|
||||||
|
OnLogOut(Msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
// 有关程序集的一般信息由以下
|
||||||
|
// 控制。更改这些特性值可修改
|
||||||
|
// 与程序集关联的信息。
|
||||||
|
[assembly: AssemblyTitle("HaoYueNet.ClientNetworkNet4x")]
|
||||||
|
[assembly: AssemblyDescription("")]
|
||||||
|
[assembly: AssemblyConfiguration("")]
|
||||||
|
[assembly: AssemblyCompany("")]
|
||||||
|
[assembly: AssemblyProduct("HaoYueNet.ClientNetworkNet4x")]
|
||||||
|
[assembly: AssemblyCopyright("Copyright © 2021")]
|
||||||
|
[assembly: AssemblyTrademark("")]
|
||||||
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
// 将 ComVisible 设置为 false 会使此程序集中的类型
|
||||||
|
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
|
||||||
|
//请将此类型的 ComVisible 特性设置为 true。
|
||||||
|
[assembly: ComVisible(false)]
|
||||||
|
|
||||||
|
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||||
|
[assembly: Guid("27e139e2-f5f5-47df-99d8-04dc7ebebeec")]
|
||||||
|
|
||||||
|
// 程序集的版本信息由下列四个值组成:
|
||||||
|
//
|
||||||
|
// 主版本
|
||||||
|
// 次版本
|
||||||
|
// 生成号
|
||||||
|
// 修订号
|
||||||
|
//
|
||||||
|
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
|
||||||
|
//通过使用 "*",如下所示:
|
||||||
|
// [assembly: AssemblyVersion("1.0.*")]
|
||||||
|
[assembly: AssemblyVersion("1.0.0.0")]
|
||||||
|
[assembly: AssemblyFileVersion("1.0.0.0")]
|
10
NetLib/HaoYueNet.ClientNetworkNet4x/packages.config
Normal file
10
NetLib/HaoYueNet.ClientNetworkNet4x/packages.config
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="protobuf-net" version="3.0.101" targetFramework="net48" />
|
||||||
|
<package id="protobuf-net.Core" version="3.0.101" targetFramework="net48" />
|
||||||
|
<package id="System.Buffers" version="4.5.1" targetFramework="net48" />
|
||||||
|
<package id="System.Collections.Immutable" version="1.7.1" targetFramework="net48" />
|
||||||
|
<package id="System.Memory" version="4.5.4" targetFramework="net48" />
|
||||||
|
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
|
||||||
|
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
|
||||||
|
</packages>
|
120
NetLib/HaoYueNet.ClientNetworkNet4x/protobuf_HunterNetCore.cs
Normal file
120
NetLib/HaoYueNet.ClientNetworkNet4x/protobuf_HunterNetCore.cs
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
//------------------------------------------------------------------------------
|
||||||
|
// <auto-generated>
|
||||||
|
// This code was generated by a tool.
|
||||||
|
//
|
||||||
|
// Changes to this file may cause incorrect behavior and will be lost if
|
||||||
|
// the code is regenerated.
|
||||||
|
// </auto-generated>
|
||||||
|
//------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Option: missing-value detection (*Specified/ShouldSerialize*/Reset*) enabled
|
||||||
|
|
||||||
|
// Generated from: proto/protobuf_HunterNetCore.proto
|
||||||
|
namespace HunterProtobufCore
|
||||||
|
{
|
||||||
|
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"HunterNet_C2S")]
|
||||||
|
public partial class HunterNet_C2S : global::ProtoBuf.IExtensible
|
||||||
|
{
|
||||||
|
public HunterNet_C2S() {}
|
||||||
|
|
||||||
|
private int? _HunterNetCore_CmdID;
|
||||||
|
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"HunterNetCore_CmdID", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
|
||||||
|
public int? HunterNetCore_CmdID
|
||||||
|
{
|
||||||
|
get { return _HunterNetCore_CmdID; }
|
||||||
|
set { _HunterNetCore_CmdID = value; }
|
||||||
|
}
|
||||||
|
[global::System.Xml.Serialization.XmlIgnore]
|
||||||
|
[global::System.ComponentModel.Browsable(false)]
|
||||||
|
public bool HunterNetCore_CmdIDSpecified
|
||||||
|
{
|
||||||
|
get { return this._HunterNetCore_CmdID != null; }
|
||||||
|
set { if (value == (this._HunterNetCore_CmdID== null)) this._HunterNetCore_CmdID = value ? this.HunterNetCore_CmdID : (int?)null; }
|
||||||
|
}
|
||||||
|
private bool ShouldSerializeHunterNetCore_CmdID() { return HunterNetCore_CmdIDSpecified; }
|
||||||
|
private void ResetHunterNetCore_CmdID() { HunterNetCore_CmdIDSpecified = false; }
|
||||||
|
|
||||||
|
private byte[] _HunterNetCore_Data;
|
||||||
|
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"HunterNetCore_Data", DataFormat = global::ProtoBuf.DataFormat.Default)]
|
||||||
|
public byte[] HunterNetCore_Data
|
||||||
|
{
|
||||||
|
get { return _HunterNetCore_Data; }
|
||||||
|
set { _HunterNetCore_Data = value; }
|
||||||
|
}
|
||||||
|
[global::System.Xml.Serialization.XmlIgnore]
|
||||||
|
[global::System.ComponentModel.Browsable(false)]
|
||||||
|
public bool HunterNetCore_DataSpecified
|
||||||
|
{
|
||||||
|
get { return this._HunterNetCore_Data != null; }
|
||||||
|
set { if (value == (this._HunterNetCore_Data== null)) this._HunterNetCore_Data = value ? this.HunterNetCore_Data : (byte[])null; }
|
||||||
|
}
|
||||||
|
private bool ShouldSerializeHunterNetCore_Data() { return HunterNetCore_DataSpecified; }
|
||||||
|
private void ResetHunterNetCore_Data() { HunterNetCore_DataSpecified = false; }
|
||||||
|
|
||||||
|
private global::ProtoBuf.IExtension extensionObject;
|
||||||
|
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
|
||||||
|
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
|
||||||
|
}
|
||||||
|
|
||||||
|
[global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"HunterNet_S2C")]
|
||||||
|
public partial class HunterNet_S2C : global::ProtoBuf.IExtensible
|
||||||
|
{
|
||||||
|
public HunterNet_S2C() {}
|
||||||
|
|
||||||
|
private int? _HunterNetCore_CmdID;
|
||||||
|
[global::ProtoBuf.ProtoMember(1, IsRequired = false, Name=@"HunterNetCore_CmdID", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
|
||||||
|
public int? HunterNetCore_CmdID
|
||||||
|
{
|
||||||
|
get { return _HunterNetCore_CmdID; }
|
||||||
|
set { _HunterNetCore_CmdID = value; }
|
||||||
|
}
|
||||||
|
[global::System.Xml.Serialization.XmlIgnore]
|
||||||
|
[global::System.ComponentModel.Browsable(false)]
|
||||||
|
public bool HunterNetCore_CmdIDSpecified
|
||||||
|
{
|
||||||
|
get { return this._HunterNetCore_CmdID != null; }
|
||||||
|
set { if (value == (this._HunterNetCore_CmdID== null)) this._HunterNetCore_CmdID = value ? this.HunterNetCore_CmdID : (int?)null; }
|
||||||
|
}
|
||||||
|
private bool ShouldSerializeHunterNetCore_CmdID() { return HunterNetCore_CmdIDSpecified; }
|
||||||
|
private void ResetHunterNetCore_CmdID() { HunterNetCore_CmdIDSpecified = false; }
|
||||||
|
|
||||||
|
private int? _HunterNetCore_ERRORCode;
|
||||||
|
[global::ProtoBuf.ProtoMember(2, IsRequired = false, Name=@"HunterNetCore_ERRORCode", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
|
||||||
|
public int? HunterNetCore_ERRORCode
|
||||||
|
{
|
||||||
|
get { return _HunterNetCore_ERRORCode; }
|
||||||
|
set { _HunterNetCore_ERRORCode = value; }
|
||||||
|
}
|
||||||
|
[global::System.Xml.Serialization.XmlIgnore]
|
||||||
|
[global::System.ComponentModel.Browsable(false)]
|
||||||
|
public bool HunterNetCore_ERRORCodeSpecified
|
||||||
|
{
|
||||||
|
get { return this._HunterNetCore_ERRORCode != null; }
|
||||||
|
set { if (value == (this._HunterNetCore_ERRORCode== null)) this._HunterNetCore_ERRORCode = value ? this.HunterNetCore_ERRORCode : (int?)null; }
|
||||||
|
}
|
||||||
|
private bool ShouldSerializeHunterNetCore_ERRORCode() { return HunterNetCore_ERRORCodeSpecified; }
|
||||||
|
private void ResetHunterNetCore_ERRORCode() { HunterNetCore_ERRORCodeSpecified = false; }
|
||||||
|
|
||||||
|
private byte[] _HunterNetCore_Data;
|
||||||
|
[global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"HunterNetCore_Data", DataFormat = global::ProtoBuf.DataFormat.Default)]
|
||||||
|
public byte[] HunterNetCore_Data
|
||||||
|
{
|
||||||
|
get { return _HunterNetCore_Data; }
|
||||||
|
set { _HunterNetCore_Data = value; }
|
||||||
|
}
|
||||||
|
[global::System.Xml.Serialization.XmlIgnore]
|
||||||
|
[global::System.ComponentModel.Browsable(false)]
|
||||||
|
public bool HunterNetCore_DataSpecified
|
||||||
|
{
|
||||||
|
get { return this._HunterNetCore_Data != null; }
|
||||||
|
set { if (value == (this._HunterNetCore_Data== null)) this._HunterNetCore_Data = value ? this.HunterNetCore_Data : (byte[])null; }
|
||||||
|
}
|
||||||
|
private bool ShouldSerializeHunterNetCore_Data() { return HunterNetCore_DataSpecified; }
|
||||||
|
private void ResetHunterNetCore_Data() { HunterNetCore_DataSpecified = false; }
|
||||||
|
|
||||||
|
private global::ProtoBuf.IExtension extensionObject;
|
||||||
|
global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
|
||||||
|
{ return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user