schangxiang@126.com
2025-09-17 c9c91ad3e3ac218d81c86809c93a35622b51a686
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
using HPSocketCS;
using iWareCc.Conveyor.Datagram;
using iWareCc.Conveyor.Entity;
using iWareCc.Conveyor.Service;
using iWareCc.Properties;
using iWareCommon.Utils;
using System;
using System.Text;
using iWareCc.Util;
using System.Runtime.InteropServices;
 
namespace iWareCc.Conveyor.Socket
{
   public class GateSocketClient:SocketClient
    {
        /// <summary>
        /// 端口对象
        /// </summary>
        private GateEntity GateEntity;
 
        public GateSocketClient(GateEntity gateEntity, TcpClient client, String ip, String port)
            : base(client, ip, port)
        {
            this.GateEntity = gateEntity;
        }
 
        protected override HandleResult OnClose(TcpClient sender, SocketOperation enOperation, int errorCode)
        {
 
            lock (GateEntity)
            {
                //清空发送和接收的报文缓存
                GateEntity.DatagramReceivePool = "";
                GateEntity.DatagramSendPool = "";
                GateEntity.SendFlag = false;
                this.IsClientOn = false;
 
            }
            
            return HandleResult.Ok;
        }
 
        protected override HandleResult OnReceive(TcpClient sender, byte[] bytes)
        {          
            string msg = UTF8Encoding.UTF8.GetString(bytes);
 
            //过滤#之后的乱码 
            string endFlag = "#";
            int index = msg.IndexOf(endFlag);
            msg = msg.Substring(0,index+endFlag.Length);
 
 
            GateEntity.SendFlag = true;
 
            new GateService(GateEntity).AddToDatagramReceivePool(msg);
 
            LogTextHelper.WriteLine(Resources.LogDir + @"/输送机端口接收报文/" + GateEntity.Place.PlaceTypeName, msg);
 
            return HandleResult.Ok;
        }
 
        protected override HandleResult OnSend(TcpClient sender, byte[] bytes)
        {
            GateEntity.SendFlag = false;
 
            LogTextHelper.WriteLine(Resources.LogDir + @"/输送机端口发送报文/" + GateEntity.Place.PlaceTypeName, UTF8Encoding.UTF8.GetString(bytes));
 
            return HPSocketCS.HandleResult.Ok;
        }
 
        protected override HandleResult OnConnect(TcpClient sender)
        {
            LogTextHelper.WriteLine(Resources.LogDir, GateEntity.Place.PlaceTypeName + "socket连上啦");
 
            lock (GateEntity)
            {
                //清空发送和接收的报文缓存
                GateEntity.DatagramReceivePool = "";
                GateEntity.DatagramSendPool = "";
                this.IsClientOn = true;
                GateEntity.SendFlag = true;
 
            }
 
            return HandleResult.Ok;
        }
 
 
        public override bool Send(byte[] bytes)
        {
           
            return base.Send(bytes);
        }
    }
}