You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
581 lines
17 KiB
C#
581 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using HuizhongLibrary.Log;
|
|
|
|
namespace HuizhongLibrary.Network
|
|
{
|
|
#region 消息对象
|
|
public class SocketMessage
|
|
{
|
|
private int m_MessageID;
|
|
public int MessageID
|
|
{
|
|
get { return m_MessageID; }
|
|
set { m_MessageID = value; }
|
|
}
|
|
private string m_SequenceID;
|
|
|
|
public string SequenceID
|
|
{
|
|
get { return m_SequenceID; }
|
|
set { m_SequenceID = value; }
|
|
}
|
|
private byte[] m_Bytes;
|
|
public byte[] Bytes
|
|
{
|
|
get { return m_Bytes; }
|
|
set { m_Bytes = value; }
|
|
}
|
|
public string Data { get; set; }
|
|
private int m_SendLength = 0;
|
|
public int SendLength
|
|
{
|
|
get { return m_SendLength; }
|
|
set { m_SendLength = value; }
|
|
}
|
|
private DateTime m_SendTime;
|
|
public DateTime SendTime
|
|
{
|
|
get { return m_SendTime; }
|
|
set { m_SendTime = value; }
|
|
}
|
|
private string m_IpAddress;
|
|
public string IpAddress
|
|
{
|
|
get { return m_IpAddress; }
|
|
set { m_IpAddress = value; }
|
|
}
|
|
private string m_DeviceNo;
|
|
public string DeviceNo
|
|
{
|
|
get { return m_DeviceNo; }
|
|
set { m_DeviceNo = value; }
|
|
}
|
|
private string m_FunNo;
|
|
public string FunNo
|
|
{
|
|
get { return m_FunNo; }
|
|
set { m_FunNo = value; }
|
|
}
|
|
private int m_SendNumber;
|
|
public int SendNumber
|
|
{
|
|
get { return m_SendNumber; }
|
|
set { m_SendNumber = value; }
|
|
}
|
|
private bool m_IsRevert;
|
|
/// <summary>
|
|
/// 是否要求回复
|
|
/// </summary>
|
|
public bool IsRevert
|
|
{
|
|
get { return m_IsRevert; }
|
|
set { m_IsRevert = value; }
|
|
}
|
|
private bool m_Enabled;
|
|
public bool Enabled
|
|
{
|
|
get { return m_Enabled; }
|
|
set { m_Enabled = value; }
|
|
}
|
|
private bool m_IsLock;
|
|
public bool IsLock
|
|
{
|
|
get { return m_IsLock; }
|
|
set { m_IsLock = value; }
|
|
}
|
|
|
|
|
|
public SocketMessage()
|
|
{
|
|
MessageID = 0;
|
|
SendNumber = 0;
|
|
SendTime = DateTime.Now.AddMinutes(-1);
|
|
Enabled = true;
|
|
DeviceNo = "";
|
|
IpAddress = "";
|
|
Data = "";
|
|
IsRevert = true;
|
|
IsLock = false;
|
|
}
|
|
|
|
public SocketMessage Copy()
|
|
{
|
|
SocketMessage Model = new SocketMessage();
|
|
Model.MessageID = MessageID;
|
|
Model.Bytes = Bytes;
|
|
Model.Data = Data;
|
|
Model.SendTime = SendTime;
|
|
Model.FunNo = FunNo;
|
|
Model.SequenceID = SequenceID;
|
|
Model.DeviceNo = DeviceNo;
|
|
Model.IpAddress = IpAddress;
|
|
Model.SendNumber = SendNumber;
|
|
Model.Enabled = Enabled;
|
|
Model.IsRevert = IsRevert;
|
|
Model.IsLock = IsLock;
|
|
Model.SendLength = SendLength;
|
|
return Model;
|
|
}
|
|
}
|
|
#endregion
|
|
#region 消息对象列表
|
|
public class SocketMessages
|
|
{
|
|
public List<SocketMessage> ListModel = new List<SocketMessage>();
|
|
public bool IsEmployMessages = false;
|
|
#region 消息数
|
|
public int Count
|
|
{
|
|
get
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
return ListModel.Count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
#endregion
|
|
#region 新增消息
|
|
public void Add(SocketMessage item)
|
|
{
|
|
if (item == null) return;
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
ListModel.Add(item);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 使消息无效
|
|
public int Employ(string SequenceID)
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
for (int i = 0; i < ListModel.Count; i++)
|
|
{
|
|
var model = ListModel[i];
|
|
if (model == null) continue;
|
|
if (model.SequenceID == SequenceID)
|
|
{
|
|
IsEmployMessages = true;
|
|
model.Enabled = false;
|
|
return model.MessageID;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
#endregion
|
|
#region 移除无效的消息
|
|
public void RemoveEmploy()
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
ListModel.RemoveAll(m => m.Enabled == false);
|
|
IsEmployMessages = false;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 返回要发送的消息
|
|
public SocketMessage GetNextSocketMessage(int RefshSecond)
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
SocketMessage model = null;
|
|
for (int i = 0; i < ListModel.Count; i++)
|
|
{
|
|
model = ListModel[i];
|
|
if (model.Enabled == false) continue;
|
|
if (model.IsLock == true && i > 0)
|
|
{
|
|
return null;
|
|
}
|
|
if (model.SendTime.AddSeconds(RefshSecond) <= DateTime.Now)
|
|
{
|
|
return model;
|
|
}
|
|
else
|
|
{
|
|
if (model.IsLock == true && i == 0) return null;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
#endregion
|
|
#region 根据流水号返回消息
|
|
public SocketMessage GetSocketMessage(string SequenceID)
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
SocketMessage model = null;
|
|
SocketMessage model2 = null;
|
|
for (int i = 0; i < this.ListModel.Count; i++)
|
|
{
|
|
model2 = this.ListModel[i];
|
|
if (model2.SequenceID == SequenceID) { model = model2; break; }
|
|
}
|
|
return model;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
#endregion
|
|
#region 清除全部消息
|
|
public void Clear()
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
ListModel.Clear();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 移除并返回一个对象
|
|
public SocketMessage CheckOut(string SequenceID)
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
SocketMessage model = null;
|
|
for (int i = 0; i < this.ListModel.Count; i++)
|
|
{
|
|
if (this.ListModel[i].SequenceID == SequenceID)
|
|
{
|
|
model = this.ListModel[i];
|
|
this.ListModel.RemoveAt(i);
|
|
break;
|
|
}
|
|
}
|
|
return model;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
#endregion
|
|
#region 返回顶部的消息延迟时间
|
|
public int PeekInterval(int RefshSecond)
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
TimeSpan ts = ListModel[0].SendTime.AddSeconds(RefshSecond) - DateTime.Now;
|
|
return Convert.ToInt32(ts.TotalMilliseconds);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
#endregion
|
|
#region 移除并返回一个对象
|
|
public SocketMessage CheckOut()
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
if (ListModel.Count == 0) return null;
|
|
SocketMessage model = ListModel[0];
|
|
ListModel.RemoveAt(0);
|
|
return model;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite("SocketMessages.CheckOut", ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
#endregion
|
|
#region 移除并返回一个对象
|
|
public void Remove(string FunNo)
|
|
{
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
this.ListModel.RemoveAll(m => m.FunNo == FunNo);
|
|
//for (int i = this.ListModel.Count - 1; i >= 0; i--)
|
|
//{
|
|
// if (this.ListModel[i].FunNo == FunNo)
|
|
// {
|
|
// this.ListModel.RemoveAt(i);
|
|
// }
|
|
//}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite("SocketMessages.Remove", ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 移除设备消息
|
|
public void Clear(string DeviceNo)
|
|
{
|
|
if (string.IsNullOrEmpty(DeviceNo) == true) return;
|
|
lock (ListModel)
|
|
{
|
|
try
|
|
{
|
|
ListModel.RemoveAll(m => m.DeviceNo == DeviceNo);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
#region 消息缓存池
|
|
public class SocketMessagePool
|
|
{
|
|
Queue<SocketMessage> argsPool;
|
|
|
|
public bool IsRead = false;
|
|
|
|
public SocketMessagePool()
|
|
{
|
|
argsPool = new Queue<SocketMessage>();
|
|
IsRead = true;
|
|
}
|
|
|
|
|
|
#region 新增对象
|
|
public void CheckIn(SocketMessage item)
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
argsPool.Enqueue(item);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 返回对象
|
|
public SocketMessage CheckOut()
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
if (argsPool.Count == 0) return null;
|
|
return argsPool.Dequeue();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
#endregion
|
|
#region 返回缓存数量
|
|
public int Available
|
|
{
|
|
get
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
return argsPool.Count;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 移除
|
|
public void Clear()
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
argsPool.Clear();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
#region 设备缓存
|
|
public class ServerConnectionPools
|
|
{
|
|
Dictionary<string, ServerConnection> argsPool=new Dictionary<string, ServerConnection>();
|
|
|
|
#region 新增对象
|
|
public void CheckIn(string DeviceNo,ServerConnection item)
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
if (argsPool.ContainsKey(DeviceNo) == true)
|
|
{
|
|
argsPool[DeviceNo] = item;
|
|
return;
|
|
}
|
|
argsPool.Add(DeviceNo,item);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 返回对象
|
|
public ServerConnection CheckOut(string DeviceNo)
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
if (argsPool.Count == 0) return null;
|
|
ServerConnection model=null;
|
|
bool bk= argsPool.TryGetValue(DeviceNo,out model);
|
|
if (bk == true && model != null) return model;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
#endregion
|
|
#region 移除
|
|
public void Remove(string DeviceNo)
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
argsPool.Remove(DeviceNo);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 移除
|
|
public void Clear()
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
argsPool.Clear();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
#region 清除已断开连接
|
|
public void RemoveEnabled()
|
|
{
|
|
lock (argsPool)
|
|
{
|
|
try
|
|
{
|
|
var argsPool2 = new Dictionary<string, ServerConnection>();
|
|
foreach (var item in argsPool.Values)
|
|
{
|
|
if (item.Enabled == false) continue;
|
|
if (argsPool2.ContainsKey(item.DeviceNo + ":" + item.Port) == true) continue;
|
|
argsPool2.Add(item.DeviceNo + ":" + item.Port, item);
|
|
}
|
|
argsPool = argsPool2;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|