新增TCP扩展
This commit is contained in:
1 parent
514d6f5255
commit
f119665d29
26 files changed
+2457
-34
No files matched your search
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using HHWMS.Web.Models;
|
||||
using ProtoBuf;
|
||||
namespace HHWMS.Web
|
||||
{
|
||||
public class DevCommuBySocketClient
|
||||
{
|
||||
public enum CommuStatus
|
||||
{
|
||||
OFFLINE = 0,
|
||||
READY = 1,
|
||||
//SENDING = 2,
|
||||
RECEIVING = 3
|
||||
}
|
||||
public const int SendWordLength = 70;
|
||||
//用于通讯的Socket对象
|
||||
private SocketClient _socketClient;
|
||||
public ushort SendIndex { get; set; }
|
||||
public ushort ReadIndex { get; set; }
|
||||
public void ResetComm()
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
if (DevCommStatus != CommuStatus.OFFLINE)
|
||||
{
|
||||
DevCommStatus = CommuStatus.READY;
|
||||
}
|
||||
_heartTimer.Stop();
|
||||
_heartTimer.Start();
|
||||
}
|
||||
}
|
||||
public byte[] ExternalSendBuf
|
||||
{
|
||||
set
|
||||
{
|
||||
if (value != null)
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
|
||||
externalData = new byte[value.Length];
|
||||
Array.Copy(value, 0, externalData, 0, value.Length);
|
||||
}
|
||||
SendLoop(null, null);
|
||||
_externalSendtimer.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
externalData = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//外部触发的需要发送的数据缓存
|
||||
|
||||
public object CommuLock=new object();//通讯锁
|
||||
private System.Timers.Timer _heartTimer;//心跳定时器
|
||||
private System.Timers.Timer _externalSendtimer;//外部缓存发送定时器
|
||||
private int _sendErrCount;//发送失败的次数
|
||||
public CommuStatus DevCommStatus { get; set; }//当前问答模式下的状态
|
||||
|
||||
public delegate void RecMsgHandler(byte[] msg);
|
||||
public event RecMsgHandler RecMsgEvent;
|
||||
|
||||
public delegate void OrderRespHandler();
|
||||
public event OrderRespHandler OrderRespEvent;
|
||||
private int mtime = 0;
|
||||
public DevCommuBySocketClient(string ip, int port, int mtime1)
|
||||
{
|
||||
DevCommStatus = CommuStatus.OFFLINE;
|
||||
_socketClient = new SocketClient(ip, port);
|
||||
_socketClient.StartListenThread += SocketClient_StartListenThread;
|
||||
_socketClient.OnSended += SocketClient_OnSended;
|
||||
_socketClient.OnMsgReceived += SocketClient_OnMsgReceived;
|
||||
_socketClient.ConnectFail += SocketClient_ConnectFail;
|
||||
_socketClient.Connect();
|
||||
|
||||
_heartTimer = new System.Timers.Timer();
|
||||
_heartTimer.Interval = 5000;
|
||||
mtime = mtime1;
|
||||
_heartTimer.Elapsed += new ElapsedEventHandler(HeartBeat);
|
||||
_heartTimer.Start();
|
||||
_externalSendtimer = new System.Timers.Timer
|
||||
{
|
||||
Interval = mtime1
|
||||
};
|
||||
_externalSendtimer.Elapsed += new ElapsedEventHandler(SendLoop);
|
||||
reConCount = 30;
|
||||
SendIndex = 1;
|
||||
|
||||
/*MemoryStream ms = new MemoryStream();
|
||||
MsgInstance msgInstance = new MsgInstance
|
||||
{
|
||||
requestId = "wms1",
|
||||
srctype = MsgInstance.SourceType.HEART_BEAT,
|
||||
|
||||
timeInfo = DateTime.Now.ToString()
|
||||
|
||||
};
|
||||
//序列化 msg
|
||||
Serializer.Serialize(ms, msgInstance);
|
||||
ms.Position = 0;
|
||||
heartBeatData = ms.ToArray();*/
|
||||
}
|
||||
|
||||
private int connectFailCount=0;
|
||||
private void SocketClient_ConnectFail()
|
||||
{
|
||||
connectFailCount++;
|
||||
}
|
||||
|
||||
private uint reConCount;
|
||||
//接收到数据的事件
|
||||
private void SocketClient_OnMsgReceived(byte[] info)
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
if (info!=null)
|
||||
{
|
||||
DevCommStatus = CommuStatus.READY;
|
||||
}
|
||||
else
|
||||
{
|
||||
DevCommStatus = CommuStatus.OFFLINE;
|
||||
connectFailCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//发送完成的事件
|
||||
private void SocketClient_OnSended(bool successorfalse)
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
if (successorfalse)
|
||||
{
|
||||
externalData = null;
|
||||
DevCommStatus = CommuStatus.READY;
|
||||
}
|
||||
else
|
||||
{
|
||||
DevCommStatus = CommuStatus.OFFLINE;
|
||||
connectFailCount++;
|
||||
_sendErrCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//连接设备成功的事件
|
||||
private void SocketClient_StartListenThread()
|
||||
{
|
||||
DevCommStatus = CommuStatus.READY;
|
||||
}
|
||||
|
||||
public bool SendData(byte[] data)
|
||||
{
|
||||
if (DevCommStatus == CommuStatus.READY)
|
||||
{
|
||||
if (_socketClient.Send(data))
|
||||
{
|
||||
//DevCommStatus = CommuStatus.SENDING;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (DevCommStatus== CommuStatus.OFFLINE)
|
||||
{
|
||||
if (connectFailCount!=0)
|
||||
{
|
||||
connectFailCount = 0;
|
||||
_socketClient.Connect();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool electry = true;
|
||||
private byte[] heartBeatData;
|
||||
private byte[] externalData;
|
||||
private void HeartBeat(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
if (electry)
|
||||
{
|
||||
electry = false;
|
||||
_heartTimer.Interval = mtime;
|
||||
}
|
||||
|
||||
if (!_externalSendtimer.Enabled && heartBeatData != null)//没有外部指令的情况下发心跳,优先发外部指令
|
||||
{
|
||||
if (SendData(heartBeatData))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++_sendErrCount > reConCount)
|
||||
{
|
||||
_socketClient.ReConnect();
|
||||
_sendErrCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SendLoop(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
lock (CommuLock)
|
||||
{
|
||||
if (externalData != null)
|
||||
{
|
||||
if (SendData(externalData))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++_sendErrCount > reConCount)
|
||||
{
|
||||
_socketClient.ReConnect();
|
||||
_sendErrCount = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_externalSendtimer.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user