修改一些可能在某些条件 下可能会触发的BUG

This commit is contained in:
2026-03-26 14:18:52 +08:00
parent d0c626c189
commit 696144b2ff
59 changed files with 109 additions and 82682 deletions

3
.gitignore vendored
View File

@@ -39,3 +39,6 @@
/WebSite/Logs
WebSite/welcomebgm
MvcApplication1
ConsoleApplication4
RCUHost内存泄漏分析报告.md
CRICS_V3_1124.suo

Binary file not shown.

View File

@@ -1,108 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{85BC55B1-083D-4AE9-8DE8-3DE59B654990}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleApplication4</RootNamespace>
<AssemblyName>ConsoleApplication4</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<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|x86' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CommonEntity\CommonEntity.csproj">
<Project>{1D7073B2-4CC3-49F5-9F37-50A21D74A39D}</Project>
<Name>CommonEntity</Name>
</ProjectReference>
<ProjectReference Include="..\Common\Common.csproj">
<Project>{B3F29715-E925-4E56-9248-580F06C3BC11}</Project>
<Name>Common</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 和 x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishUrlHistory />
<InstallUrlHistory />
<SupportUrlHistory />
<UpdateUrlHistory />
<BootstrapperUrlHistory />
<ErrorReportUrlHistory />
<FallbackCulture>zh-CN</FallbackCulture>
<VerifyUploadedFiles>false</VerifyUploadedFiles>
</PropertyGroup>
</Project>

View File

@@ -1,179 +0,0 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Common;
using System.Timers;
using CommonEntity;
namespace ConsoleApplication4
{
internal class UdpState
{
public UdpClient UdpClient { get; set;}
public IPEndPoint RemoteEndPoint { get; set; }
public UdpState(UdpClient client)
{
this.UdpClient = client;
}
}
class Program
{
private static bool _isRunning = true;
public static System.Timers.Timer CPUTimer = null;
public static void StartCPUTongJiTask()
{
try
{
CPUTimer = new System.Timers.Timer();
CPUTimer.Elapsed += new ElapsedEventHandler(CPUtimer_Elapsed);
//10分钟统计一次
//现在修改成20秒
//CPUTimer.Interval = 1 * 60 * 1000;
CPUTimer.Interval = 1000;
CPUTimer.Start();
}
catch (Exception ex)
{
}
}
static private void CPUtimer_Elapsed(object sender, ElapsedEventArgs e)
{
var T = sender as System.Timers.Timer;
try
{
// 防止重入
T.Stop();
double d = CPUData.GetCPU();
DataTongJi.CPU_Data.Add(d);
Console.WriteLine("aaaaaaaaaa");
}
catch (Exception ex)
{
Console.WriteLine("出错了:"+ex.Message);
}
finally
{
// 恢复计时器
T.Start();
}
}
static void Main(string[] args)
{
StartCPUTongJiTask();
var a= CPUData.GetCPU();
Console.WriteLine(a);
var b = CPUData.GetNowPrecise();
Console.WriteLine(b.ToString());
Console.ReadLine();
//Console.CancelKeyPress += (sender, e) =>
//{
// _isRunning = false;
// e.Cancel = true;
//};
//try
//{
// var udpClient = new UdpClient(3340);
// udpClient.Client.ReceiveBufferSize = 3 * 1024 * 1024;
// // 开始接收
// udpClient.BeginReceive(ReceiveCallback, new UdpState(udpClient));
// Console.WriteLine("UDP服务器已启动按Ctrl+C停止...");
// // 保持程序运行
// while (_isRunning)
// {
// Thread.Sleep(100);
// }
// udpClient.Close();
// Console.WriteLine("服务器已停止");
//}
//catch (Exception ex)
//{
// Console.WriteLine($"启动失败: {ex.Message}");
//}
}
public static void ReceiveCallback(IAsyncResult ar)
{
UdpState state = ar.AsyncState as UdpState;
try
{
// 1. 先获取接收到的数据
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
byte[] receivedData = state.UdpClient.EndReceive(ar, ref remoteEndPoint);
// 2. 立即开始下一次接收(不等待数据处理完成)
state.UdpClient.BeginReceive(ReceiveCallback, state);
// 3. 异步处理数据,避免阻塞接收
ThreadPool.QueueUserWorkItem(_ =>
{
ProcessData(receivedData, remoteEndPoint, state.UdpClient);
});
}
catch (ObjectDisposedException)
{
// 正常关闭,忽略
}
catch (SocketException ex)
{
//Console.WriteLine($"网络错误: {ex.SocketErrorCode} - {ex.Message}");
//// 尝试重新开始接收
//if (_isRunning && state?.UdpClient?.Client != null)
//{
// try
// {
// state.UdpClient.BeginReceive(ReceiveCallback, state);
// }
// catch { }
//}
}
catch (Exception ex)
{
//Console.WriteLine($"接收回调错误: {ex.Message}");
}
}
private static void ProcessData(byte[] data, IPEndPoint remoteEP, UdpClient udpClient)
{
try
{
//// 这里是您的数据处理逻辑
//Console.WriteLine($"收到来自 {remoteEP} 的数据,长度: {data.Length} 字节");
//// 示例:解码为字符串
//if (data.Length > 0)
//{
// string text = Encoding.UTF8.GetString(data);
// Console.WriteLine($"内容: {text}");
//}
//// 这里可以处理复杂的业务逻辑
//// 例如:数据库操作、文件处理、复杂计算等
//// 如果需要回复
//if (data.Length > 0)
//{
// byte[] response = Encoding.UTF8.GetBytes($"已收到: {data.Length} 字节");
// udpClient.Send(response, response.Length, remoteEP);
//}
}
catch (Exception ex)
{
//Console.WriteLine($"数据处理失败: {ex.Message}");
}
}
}
}

View File

@@ -1,36 +0,0 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的常规信息通过以下
// 特性集控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("ConsoleApplication4")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ConsoleApplication4")]
[assembly: AssemblyCopyright("Copyright © 2026")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 使此程序集中的类型
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
// 则将该类型上的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("3ac386f6-d433-4860-9b22-ee7e0dc298d4")]
// 程序集的版本信息由下面四个值组成:
//
// 主版本
// 次版本
// 内部版本号
// 修订号
//
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
// 方法是按如下所示使用“*”:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -1,3 +0,0 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +0,0 @@
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

View File

@@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,4 +0,0 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.0,Profile=Client", FrameworkDisplayName = ".NET Framework 4 Client Profile")]

View File

@@ -1,4 +0,0 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework 4")]

View File

@@ -1,43 +0,0 @@
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\ConsoleApplication4.exe
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\ConsoleApplication4.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\obj\x86\Debug\ConsoleApplication4.exe
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\obj\x86\Debug\ConsoleApplication4.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\obj\x86\Debug\ConsoleApplication4.csprojResolveAssemblyReference.cache
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\ConsoleApplication4.exe.config
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Common.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\log4net.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NHibernate.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\M2Mqtt.Net.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CacheManager.Core.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CSRedisCore.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Newtonsoft.Json.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\aliyun-net-sdk-core.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\aliyun-net-sdk-dysmsapi.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.OOXML.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\System.ValueTuple.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Iesi.Collections.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Antlr3.Runtime.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Remotion.Data.Linq.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\ICSharpCode.SharpZipLib.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.OpenXmlFormats.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.OpenXml4Net.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Common.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\M2Mqtt.Net.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CacheManager.Core.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CSRedisCore.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CSRedisCore.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Newtonsoft.Json.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.OOXML.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\System.ValueTuple.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\NPOI.OpenXml4Net.xml
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CommonEntity.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Domain.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\jose-jwt.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\RestSharp.dll
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\CommonEntity.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\Domain.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\jose-jwt.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\RestSharp.pdb
E:\tian\chongxin\NewGit\CRICS\CRICS_Web_Server_VS2010_Prod\ConsoleApplication4\bin\Debug\RestSharp.xml

View File

@@ -123,6 +123,18 @@ namespace RCUHost.Implement
try
{
var redis = CSRedisCacheHelper.redis1;
// 检查并创建消费者组
try
{
// 尝试创建消费者组,如果已存在则忽略错误
redis.XGroupCreate(UDPAllDataKey, group, "0", true);
}
catch (Exception)
{
// 消费者组可能已存在,忽略错误
}
var data = redis.XReadGroup(group, consumer, 500, 10, new ValueTuple<string, string>(UDPAllDataKey, ">"));
if (data != null)
@@ -150,22 +162,31 @@ namespace RCUHost.Implement
logger.Error("消息队列失败:" + ex.Message);
}
}
// 静态CancellationTokenSource和CancellationToken
public static CancellationTokenSource source = new CancellationTokenSource();
public static CancellationToken token = source.Token;
// 存储长时间运行的Task
private static List<Task> _consumerTasks = new List<Task>();
/// <summary>
/// 启动 HostServer
/// </summary>
public void Start()
{
// 停止之前的Task
StopConsumerTasks();
// 创建新的取消令牌源
source = new CancellationTokenSource();
token = source.Token;
CSRedisCacheHelper.redis1.Del(UDPAllDataKey);
CSRedisCacheHelper.redis1.XGroupCreate(UDPAllDataKey, "UDPData", "0", true);
var DDD = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
CSRedisCacheHelper.Forever<string>(CacheKey.ServerStartTime, DDD);
//FaultReport.Interval = 10000;
//FaultReport.Elapsed += new System.Timers.ElapsedEventHandler(FaultReport_Elapsed);
//FaultReport.Start();
Close();
//CSRedisCacheHelper.StreamAdd(1,UDPAllDataKey, data);
//CSRedisCacheHelper.StreamConsume(1, UDPAllDataKey);
try
{
udpClient = new UdpClient(3339);
@@ -177,29 +198,71 @@ namespace RCUHost.Implement
udpClient.BeginReceive(ReceiveCallback, new UdpState(udpClient));
logger.Error("Host Server启动成功端口3339");
CSRedisCacheHelper.redis1.Del(UDPAllDataKey);
CSRedisCacheHelper.redis1.XGroupCreate(UDPAllDataKey, "UDPData", "0", true);
// 清空之前的Task列表
_consumerTasks.Clear();
// 创建15个消费者Task
for (int i = 1; i <= 15; i++)
{
Task.Factory.StartNew((iii) =>
int index = i;
var task = Task.Factory.StartNew(() =>
{
string osos = iii.ToString();
while (true)
string osos = index.ToString();
while (!token.IsCancellationRequested)
{
StreamConsume("UDPData", "Crics" + osos, "task" + osos);
try
{
StreamConsume("UDPData", "Crics" + osos, "task" + osos);
}
catch (OperationCanceledException)
{
// 任务被取消,正常退出
break;
}
catch (Exception ex)
{
logger.Error("消费者Task错误:" + ex.Message);
}
}
}, i, TaskCreationOptions.LongRunning);
}, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
_consumerTasks.Add(task);
}
}
catch (Exception ex)
{
logger.Error("Host Server启动失败端口3339");
Close();
StopConsumerTasks();
throw ex;//不能去掉否则重启iis服务通讯服务不会再次启动
}
}
/// <summary>
/// 停止消费者Task
/// </summary>
private static void StopConsumerTasks()
{
if (source != null)
{
try
{
// 取消所有Task
source.Cancel();
// 等待Task完成
Task.WaitAll(_consumerTasks.ToArray(), TimeSpan.FromSeconds(5));
}
catch (Exception ex)
{
logger.Error("停止消费者Task错误:" + ex.Message);
}
finally
{
source.Dispose();
_consumerTasks.Clear();
}
}
}
//public Random rs = new Random();
@@ -455,11 +518,15 @@ namespace RCUHost.Implement
private void ReceiveCallback(IAsyncResult ar)
{
UdpState state = ar.AsyncState as UdpState;
if (state == null || state.UdpClient == null)
{
return;
}
try
{
IPEndPoint remoteEP111 = new IPEndPoint(IPAddress.Any, 0);
byte[] receiveBuffer111 = state.UdpClient.EndReceive(ar, ref remoteEP111);
state.UdpClient.BeginReceive(ReceiveCallback, state);
// 2. 数据处理逻辑 - 异步处理,不阻塞接收线程
if (receiveBuffer111 != null && receiveBuffer111.Length > 0)
@@ -472,23 +539,31 @@ namespace RCUHost.Implement
}, Tuple.Create(receiveBuffer111, remoteEP111));
}
}
catch (ObjectDisposedException)
{
// UdpClient已被释放正常退出
}
catch (Exception ex)
{
logger.Error(string.Format("接收数据失败:{0}", ex.ToString()));
}
finally
{
//try
//{
// if (this.udpClient != null)
// {
// state.UdpClient.BeginReceive(ReceiveCallback, new UdpState(this.udpClient));
// }
//}
//catch
//{
//}
try
{
if (this.udpClient != null && state != null && state.UdpClient != null)
{
state.UdpClient.BeginReceive(ReceiveCallback, new UdpState(this.udpClient));
}
}
catch (ObjectDisposedException)
{
// UdpClient已被释放正常退出
}
catch
{
// 忽略其他错误
}
}
}

View File

@@ -5329,10 +5329,10 @@ namespace WebSite.Controllers
long jishu_error = Interlocked.Read(ref MvcApplication.UDPServerErrorCount);
if (jishu_error >= 3)
{
logger.Error("重启了UDP服务器");
Interlocked.Exchange(ref MvcApplication.UDPServerErrorCount, 0);
var hostServer = (IHostServer)MvcApplication.cxt.GetObject("RCUHost.HostServer");
hostServer.Start();
//logger.Error("重启了UDP服务器");
//Interlocked.Exchange(ref MvcApplication.UDPServerErrorCount, 0);
//var hostServer = (IHostServer)MvcApplication.cxt.GetObject("RCUHost.HostServer");
//hostServer.Start();
}
var TotalKey = "UDPPackage_TotalRecvPackage";
UDPPackageCount LLL_T = null;