采用异步来实现重新连接服务器或者重新启动服务
开启异步监听,不会导致主线程的堵塞,在服务异常断开后一直检测重新连接服务,成功连接服务后通知各个注册的客户端!
#region 检测断线并重连opc服务 可以强制启动opc服务程序
/// <summary>
/// 提供外部使用
/// 重新连接事件
/// </summary>
public event linkstatechangedevent onlinkstatechanged;
/// <summary>
/// 当前连接状态
/// </summary>
volatile elinkstate curlinkstate = elinkstate.unkown;
/// <summary>
/// 获取或设置当前连接状态
/// </summary>
elinkstate curlinkstate
{
get
{
lock (this)
{
return curlinkstate;
}
}
set
{
lock (this)
{
curlinkstate = value;
}
}
}
/// <summary>
/// 检测间隔毫秒数
/// </summary>
const int _checkinterval = 3000;
/// <summary>
/// 空闲心跳时间
/// </summary>
autoresetevent _waitcheckandtryrelinkevent = new autoresetevent(false);
/// <summary>
/// 是否正在检测并尝试重连
/// </summary>
volatile bool _ischeckingandtryrelink = false;
/// <summary>
/// 获取或设置是否正在检测并尝试重连
/// </summary>
bool ischeckingandtryrelink
{
get
{
lock (this)
{
return _ischeckingandtryrelink;
}
}
set
{
lock (this)
{
_ischeckingandtryrelink = value;
}
}
}
/// <summary>
/// 启动检测并尝试重连
/// </summary>
void startcheckandtryrelink()
{
ischeckingandtryrelink = true;
beginwaitcheckandtryrelink();
}
/// <summary>
/// 开始等待检测并尝试重连
/// </summary>
void beginwaitcheckandtryrelink()
{
//开始异步等待
threadpool.registerwaitforsingleobject(_waitcheckandtryrelinkevent, waitcheckandtryrelinkcallback, null, _checkinterval, true);
}
/// <summary>
/// 等待检测并尝试重连回调函数
/// </summary>
/// <param name="state"></param>
/// <param name="timedout"></param>
void waitcheckandtryrelinkcallback(object state, boolean timedout)
{
//检测断线并重连
try
{
opcautomation.opcserver service = getopcserver();
if (service != null)
{//连接正常
if (curlinkstate != elinkstate.connected)
{
elinkstate oldstate = curlinkstate;
curlinkstate = elinkstate.connected;
outlinkstatechanged(oldstate, curlinkstate);
}
}
else
{//连接已断开
if (curlinkstate != elinkstate.disconnected)
{
elinkstate oldstate = curlinkstate;
curlinkstate = elinkstate.disconnected;
outlinkstatechanged(oldstate, curlinkstate);
}
#region 尝试重连
//先注销
//try
//{
// service.disconnect();
// // remotingclient.unregisterchannel();
//}
//catch(exception ex)
//{
//}
//等待一下
system.threading.thread.sleep(3000);
//再重新注册
try
{
// remotingclient.registerchannel();
server = new opcautomation.opcserver();//重新初始化服务
server.connect(servername, ip);
//server = service;
opcisconnected = true;
task.factory.startnew(creategroupsinfos);
}
catch
{
opcisconnected = false;
}
#endregion
}
}
catch
{
}
//进入下一轮
if (ischeckingandtryrelink)
{
beginwaitcheckandtryrelink();
}
}
/// <summary>
/// 停止检测并尝试重连
/// </summary>
void stopcheckandtryrelink()
{
ischeckingandtryrelink = false;
_waitcheckandtryrelinkevent.set();
}
/// <summary>
/// 委托:连接状态发生变化
/// </summary>
/// <param name="oldstate">旧状态</param>
/// <param name="newstate">新状态</param>
void outlinkstatechanged(elinkstate oldstate, elinkstate newstate)
{
if (onlinkstatechanged != null)
{
//广播
linkstatechangedevent tempevent = null;
foreach (delegate del in onlinkstatechanged.getinvocationlist())
{
try
{
tempevent = (linkstatechangedevent)del;
tempevent.begininvoke(oldstate, newstate, null, null);
}
catch
{
}
}
}
}
#endregion
public enum elinkstate
{
/// <summary>
/// 未知状态
/// </summary>
[description("未知状态")]
unkown = -1,
/// <summary>
/// 断开
/// </summary>
[description("已断开")]
disconnected = 0,
/// <summary>
/// 已连接
/// </summary>
[description("已连接")]
connected = 1
}
上一篇: python 实现倒排索引的方法
下一篇: Python类装饰器实现方法详解