ams
cjs
2025-05-28 53f837bef54387950308db48a06de42663f3ae99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using Hanhe.iWCS.Factroy;
using Hanhe.iWCS.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Hanhe.iWCS.JingmenGLMNorthTCP
{
    public class SessionInstance
    {
        private static readonly SessionInstance instance = new SessionInstance();
 
        public List<WCSSession> WCSSessionList = new List<WCSSession>();
 
        /// <summary>
        /// 设备授权数量
        /// </summary>
        private const int DeviceAuthNumber = 100;
 
        /// <summary>
        /// 设备协议接口实例化
        /// </summary>
        public IProtocolAnalysis _IProtocolAnalysis;
 
 
        /// <summary>
        /// 获取单实例
        /// </summary>
        public static SessionInstance Instance {
            get {
                return instance;
            }
        }
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public SessionInstance() {
 
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initialize() {
            _IProtocolAnalysis = DataAccess.CreateProtocolAnalysis();
        }
 
        /// <summary>
        /// 增加会话到内存队列
        /// </summary>
        /// <param name="session"></param>
        public void AddSession(WCSSession session) {
            if (!WCSSessionList.Exists(p => p.SessionID == session.SessionID)) {
                WCSSessionList.RemoveAll(a => a.RemoteEndPoint.Address == session.RemoteEndPoint.Address);
                var _wcsSession = WCSSessionList.Where(p => p.RemoteEndPoint.Address == session.RemoteEndPoint.Address);
                foreach (var sessionIns in _wcsSession) {
 
                    WCSSessionList.Remove(sessionIns);
                }
                if (WCSSessionList.Count <= DeviceAuthNumber) {
                    WCSSessionList.Add(session);
                }
            }
        }
        private void PrintSession() {
            WCSSessionList.ForEach(a => Console.WriteLine($"{a.RemoteEndPoint.Address}:{a.RemoteEndPoint.Port}"));
        }
        /// <summary>
        /// 给指定客户端发送消息(只匹配IP地址)
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool PLCSend(string ip, int port, string msg) {
            bool iRtn = false;
            //PrintSession();
            //对当前已连接的所有会话进行广播
            WCSSession wcsSession = WCSSessionList.Find(p => p.RemoteEndPoint.Address.ToString() == ip && p.RemoteEndPoint.Port == port);
            if (wcsSession != null) {
                var data = Encoding.UTF8.GetBytes(msg);
                wcsSession.Send(data, 0, data.Length);
                //Console.WriteLine($"{ip}:{port} 发送消息{msg}");
                iRtn = true;
            }
            else {
                Console.WriteLine($"{ip}:{port}连接不存在");
            }
            return iRtn;
        }
 
        public bool PLCSend(string ip, int port, byte[] data) {
            bool iRtn = false;
            //PrintSession();
            //对当前已连接的所有会话进行广播
            
            WCSSession wcsSession = WCSSessionList.Find(p => p.RemoteEndPoint.Address.ToString() == ip && p.RemoteEndPoint.Port == port);
            if (wcsSession != null) {
                wcsSession.Send(data, 0, data.Length);
                //Console.WriteLine($"{ip}:{port} 发送消息{msg}");
                iRtn = true;
            }
            else {
                Console.WriteLine($"{ip}:{port}连接不存在");
            }
            return iRtn;
        }
        /// <summary>
        /// 给指定客户端发送消息(匹配IP地址)
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool PLCSend(string ip, string msg) {
            bool iRtn = false;
            //对当前已连接的所有会话进行广播
            WCSSession wcsSession = WCSSessionList.Find(p => p.RemoteEndPoint.Address.ToString() == ip);
            if (wcsSession != null) {
                //var data = Encoding.Default.GetBytes(msg);
                var data = Encoding.UTF8.GetBytes(msg);
                wcsSession.Send(data, 0, data.Length);
                iRtn = true;
            }
            return iRtn;
        }
 
        /// <summary>
        /// 通过会话句柄ID找到客户端对象发送数据
        /// </summary>
        /// <param name="sessionID">会话句柄ID</param>
        /// <param name="msg">发送数据</param>
        /// <returns></returns>
        public bool PLCSendForSessionID(string sessionID, string msg) {
            bool iRtn = false;
            WCSSession wcsSession = WCSSessionList.Find(p => p.SessionID == sessionID);
            if (wcsSession != null) {
                Hanhe.iWCS.Common.CMMLog.Info("PLC IP=" + wcsSession.RemoteEndPoint.Address.ToString());
                var data = Encoding.UTF8.GetBytes(msg);
                wcsSession.Send(data, 0, data.Length);
                iRtn = true;
            }
            return iRtn;
        }
 
 
        /// <summary>
        /// 删除一个会话
        /// </summary>
        /// <param name="sessionID">会话ID</param>
        public void SessionRemove(string sessionID) {
            if (WCSSessionList.Count > 0) {
                WCSSession wcsSession = WCSSessionList.Find(p => p.SessionID == sessionID);
                if (wcsSession != null) WCSSessionList.Remove(wcsSession);
            }
        }
 
        /// <summary>
        /// 移除重复的会话(同一个设备编号在Session重复)
        /// </summary>
        /// <param name="sessionID">会话SessionID</param>
        /// <param name="deviceCode">设备编号</param>
        public void RemoveRepeatDeviceCodeForSession(string sessionID, string deviceCode) {
            IEnumerable<WCSSession> wcsSessionList = WCSSessionList.Where(p => p.SessionID != sessionID && p.deviceCode == deviceCode);
            foreach (var wcsSession in wcsSessionList) {
                WCSSessionList.Remove(wcsSession);
            }
        }
 
        /// <summary>
        /// 移除会话
        /// </summary>
        /// <param name="ip">会话ip</param>
        public void RemoveSessionExcludeFromIP(string ip) {
            IEnumerable<WCSSession> wcsSessionList = WCSSessionList.Where(p => p.RemoteEndPoint.Address.ToString() != ip);
            foreach (var wcsSession in wcsSessionList) {
                WCSSessionList.Remove(wcsSession);
            }
        }
 
        /// <summary>
        /// 根据心跳会话,更新会话与设备编号的对应关系
        /// </summary>
        /// <param name="sessionID">会话ID</param>
        /// <param name="deviceCode">设备编号</param>
        public void UpdateDeviceCodeForSessionID(string sessionID, string deviceCode) {
            WCSSession wcsSession = WCSSessionList.Find(p => p.SessionID == sessionID);
            if (wcsSession != null) {
                wcsSession.deviceCode = deviceCode;
            }
        }
    }
}