11
cjs
2025-06-23 823a7818345dedc7e460b3009c7ee2a89fa03e03
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
using Hanhe.iWCS.Common;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Linq;
using System.Text;
 
namespace Hanhe.iWCS.JingmenGEMTwoTCP
{
    public class WCSSession : AppSession<WCSSession, BinaryRequestInfo>
    {
        /// <summary>
        /// 设备编号
        /// </summary>
        public string deviceCode { get; set; }
        protected override void OnSessionStarted()
        {
            SessionInstance.Instance.AddSession(this.AppServer.GetSessionByID(this.SessionID));
            string msg = DateTime.Now.ToString() + "-a new Session connectioned,new Client Ip=" + this.RemoteEndPoint.Address + " and Port=" + this.RemoteEndPoint.Port + "";
            Console.WriteLine(msg);
        }
 
        /// <summary>
        /// PLC信号指令
        /// </summary>
        /// <param name="requestInfo"></param>
        protected override void HandleUnknownRequest(BinaryRequestInfo requestInfo)
        {
            var hex = BitConverter.ToString(requestInfo.Body, 0).Replace("-", string.Empty).ToLower();
            WCSSession currSession = this.AppServer.GetSessionByID(this.SessionID);
            var ip = currSession.RemoteEndPoint.Address.ToString();
            var port = currSession.RemoteEndPoint.Port;
            //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "--receive:" + hex + "--length:" + hex.Length);
            SessionInstance.Instance._IProtocolAnalysis.StringRequestInfo(hex, ip, port, currSession.SessionID);
            SessionInstance.Instance._IProtocolAnalysis.StringRequestInfo(hexToStr(hex), ip, port);
 
            var res = requestInfo.Body.ToList();res.Add(36);res.Add(36);
            SessionInstance.Instance.PLCSend(ip, port, res.ToArray());
 
        }
        private static string hexToStr(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
 
            return Encoding.ASCII.GetString(returnBytes);
        }
        protected override void HandleException(Exception e)
        {
            this.Send("Application error: {0}", e.Message);
        }
 
        protected override void OnSessionClosed(CloseReason reason)
        {
            //add you logics which will be executed after the session is closed
            SessionInstance.Instance.SessionRemove(this.SessionID);
            base.OnSessionClosed(reason);
            string msg = DateTime.Now.ToString() + "-a connection Client Ip=" + this.RemoteEndPoint.Address + " and Port=" + this.RemoteEndPoint.Port + " is SocketClosed";
            Console.WriteLine(msg);
            CMMLog.Debug(msg);
            //ProtocolAnalysis.Instance._Logger.DeleteOnline(this.RemoteEndPoint.Address.ToString(), this.RemoteEndPoint.Port);
 
        }
    }
}