wzp
2021-07-28 864986e4cad03f6b9bba9a7e65379db496b62a6a
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
namespace AsiaINFO.SMS.APPCMPP2.MyThread.CMPPThread
{
    using AsiaINFO.SMS.APPCMPP2;
    using AsiaINFO.SMS.APPCMPP2.MyThread;
    using AsiaINFO.SMS.CMPP2;
    using AsiaINFO.SMS.Entity;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Threading;
    using System.IO;
 
    
 
    public class ReceiveThread : CMPPThreadBase
    {
        private Queue<byte[]> _cmppReceiveQueue;
        private SyncEvents _moinfoEvents;
        private Queue<MOInfo> _moinfoQueue;
        private SyncEvents _reportEvents;
        private Queue<ReportInfo> _reportQueue;
        private SyncEvents _submitRespEvents;
        private Queue<byte[]> _submitRespQueue;
 
 
        public ReceiveThread(CMPPClient cmppClient, SyncEvents syncEvents, Queue<byte[]> cmppReceiveQueue, Queue<MOInfo> moinfoQueue, SyncEvents moinfoEvents, Queue<ReportInfo> reportQueue, SyncEvents reportEvents, Queue<byte[]> submitRespQueue, SyncEvents submitRespEvents)
            : base(cmppClient, syncEvents)
        {
            if ((((cmppReceiveQueue == null) || (moinfoQueue == null)) || ((moinfoEvents == null) || (reportQueue == null))) || (((reportEvents == null) || (submitRespQueue == null)) || (submitRespEvents == null)))
            {
                throw new Exception("ReceiveThread参数不能为空!");
            }
            this._cmppReceiveQueue = cmppReceiveQueue;
            this._moinfoQueue = moinfoQueue;
            this._moinfoEvents = moinfoEvents;
            this._reportQueue = reportQueue;
            this._reportEvents = reportEvents;
            this._submitRespQueue = submitRespQueue;
            this._submitRespEvents = submitRespEvents;
        }
 
        
 
 
        private void DealActive(uint seqId)
        {
            base._cmppClient.ActiveResp(seqId);
            //base.OnMsg("收到CMPP_ACTIVE_TEST 并返回 CMPP_ACTIVE_TEST_RESP", MsgLevel.Debug);
        }
 
        private void DealActiveResp()
        {
            ShareData.ActiveSeq = 0;
            //base.OnMsg("收到CMPP_ACTIVE_TEST_RESP", MsgLevel.Debug);
        }
 
        private void DealConnResp(byte[] data)
        {
            CMPP_CONNECT_RESP cmpp_connect_resp = new CMPP_CONNECT_RESP(data);
            base._cmppClient.IsConnected = cmpp_connect_resp.Status == 0;
            base.OnMsg(StateDictionary.ConnResDictionary(cmpp_connect_resp.Status), MsgLevel.Msg);
            
 
            //base.OnMsg(string.Format("收到CMPP_CONNECT_RESP 返回值={0}", cmpp_connect_resp.Status), MsgLevel.Debug);
        }
 
        private void DealDeliver(byte[] data)
        {
            CMPP_DELIVER cmpp_deliver = new CMPP_DELIVER(data);
            base.OnMsg("收到状态报告或收到MO,正在处理...   时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), MsgLevel.Debug);
            base._cmppClient.DeliverResp(cmpp_deliver.Msg_Id, 0, cmpp_deliver.Header.Sequence_Id);
            try
            {
                base.OnMsg("RECEIVE:" + cmpp_deliver.Registered_Delivery, MsgLevel.Msg);
                if (cmpp_deliver.Registered_Delivery == 0)
                {
                    MOInfo info = new MOInfo(Guid.NewGuid().ToString(), cmpp_deliver.Msg_Id, DateTime.Now, cmpp_deliver.Dest_Id, cmpp_deliver.Service_Id, cmpp_deliver.Src_terminal_Id, ShareData.CurrentISMG, cmpp_deliver.Msg_Content, "", SysConf.Instance.ProgramID);
                    this._moinfoData = info;
                    this._moinfoEvents.NewItemEvent.Set();
                    string msg = string.Format("收到MO信息  手机号码=[{0}] 目的号码=[{1}] 内容=[{2}]   时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), info.MOBILE_NO, info.LONG_SERV_NO, info.SMS_CONTENT);
                    base.OnMsg(msg, MsgLevel.Msg);
                }
                else
                {
                    CMPP_Msg_Content content = new CMPP_Msg_Content(cmpp_deliver.Report);
                    bool success = false;
                    if (content.Stat == "DELIVRD")
                    {
                        success = true;
                    }
                    ReportInfo info2 = new ReportInfo(content.Msg_Id, content.Stat, success, content.Done_time, DateTime.Now, content.Dest_terminal_Id);
                    this._reportData = info2;
                    this._reportEvents.NewItemEvent.Set();
                    string str2 = string.Format("收到Report信息 MsgID={0} State={1} mobile={2}    时间:", info2.MSGID, info2.STAT, info2.MOBILE_NO);
                    base.OnMsg(str2, MsgLevel.Debug);
                }
            }
            catch (Exception exception)
            {
                string.Format("收到DELIVER数据错误", new object[0]);
                try
                {
                    string.Format("收到DELIVER数据错误 DELIVER: {0}", cmpp_deliver.ToString());
                }
                catch
                {
                }
                base.OnMsg("[收到DELIVER数据错误]:" + exception.Message, MsgLevel.Err);
            }
        }
 
        private void DealSubmitResp(byte[] data)
        {
            this._submitRespData = data;
            this._submitRespEvents.NewItemEvent.Set();
        }
 
        private void DealTerminate()
        {
            base._cmppClient.IsConnected = false;
            base.OnMsg("服务器主动断开连接", MsgLevel.Msg);
        }
 
        private void DistributeData(byte[] data)
        {
            
 
            try
            {
                base.OnMsg("收到消息了!", MsgLevel.Debug);
                MessageEventArgs args = new MessageEventArgs(data);
                MessageHeader header = args.Header;
                byte[] array = new byte[header.Total_Length];
                header.ToBytes().CopyTo(array, 0);
                args.BodyData.CopyTo(array, 12);
                if (header.Command_Id != CMPP_Command_Id.CMPP_ACTIVE_TEST_RESP || header.Command_Id != CMPP_Command_Id.CMPP_ACTIVE_TEST)
                {
                    base.OnMsg("返回的commandId:" + header.Command_Id + "   时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), MsgLevel.Debug);
                }
                switch (header.Command_Id)
                {
 
                    case CMPP_Command_Id.CMPP_CONNECT_RESP:
                        this.DealConnResp(data);
                        return;
                    //case ((CMPP_Command_Id)(-2147483647)):
                    //    this.DealConnResp(data);
                    //    return;
 
                    //case ((CMPP_Command_Id) (-2147483646)):
                    //case ((CMPP_Command_Id) (-2147483645)):
                    //case ((CMPP_Command_Id) (-2147483643)):
                    //case ((CMPP_Command_Id) (-2147483642)):
                    //case ((CMPP_Command_Id) (-2147483641)):
                    //    return;
                    case CMPP_Command_Id.CMPP_SUBMIT_RESP:
                        this.DealSubmitResp(data);
                        return;
                    //case ((CMPP_Command_Id) (-2147483644)):
                    //    this.DealSubmitResp(data);
                    //    return;
                    case CMPP_Command_Id.CMPP_ACTIVE_TEST_RESP:
                        this.DealActiveResp();
                        return;
                    //case ((CMPP_Command_Id) (-2147483640)):
                    //    this.DealActiveResp();
                    //    return;
 
                    case CMPP_Command_Id.CMPP_ACTIVE_TEST:
                        this.DealActive(header.Sequence_Id);
                        return;
 
                    case CMPP_Command_Id.CMPP_TERMINATE:
                        break;
 
                    case CMPP_Command_Id.CMPP_DELIVER:
                        this.DealDeliver(data);
                        return;
 
                    default:
                        this.DealConnResp(data);
                        return;
                }
                this.DealTerminate();
            }
            catch (Exception exception)
            {
                base.OnMsg(exception.Message, MsgLevel.Err);
            }
        }
 
        protected override void Run()
        {
            byte[] data = null;
            while (WaitHandle.WaitAny(base._syncEvents.EventArray) != 1)
            {
            Label_0004:
                data = this._cmppReceiveData;
                if (data != null)
                {
                    this.DistributeData(data);
                    goto Label_0004;
                }
            }
            base.Stop();
        }
 
        public override void Stop()
        {
            if ((base._thread != null) && (base._thread.ThreadState == ThreadState.Suspended))
            {
                base.StartRun();
            }
            base._syncEvents.ExitThreadEvent.Set();
        }
 
        private byte[] _cmppReceiveData
        {
            get
            {
                lock (((ICollection)this._cmppReceiveQueue).SyncRoot)
                {
                    if (this._cmppReceiveQueue.Count > 0)
                    {
                        return this._cmppReceiveQueue.Dequeue();
                    }
                }
                return null;
            }
        }
 
        private MOInfo _moinfoData
        {
            get
            {
                lock (((ICollection)this._moinfoQueue).SyncRoot)
                {
                    if (this._moinfoQueue.Count > 0)
                    {
                        return this._moinfoQueue.Dequeue();
                    }
                }
                return null;
            }
            set
            {
                lock (((ICollection)this._moinfoQueue).SyncRoot)
                {
                    this._moinfoQueue.Enqueue(value);
                }
            }
        }
 
        private ReportInfo _reportData
        {
            get
            {
                lock (((ICollection)this._reportQueue).SyncRoot)
                {
                    if (this._reportQueue.Count > 0)
                    {
                        return this._reportQueue.Dequeue();
                    }
                }
                return null;
            }
            set
            {
                lock (((ICollection)this._reportQueue).SyncRoot)
                {
                    this._reportQueue.Enqueue(value);
                }
            }
        }
 
        private byte[] _submitRespData
        {
            get
            {
                lock (((ICollection)this._submitRespQueue).SyncRoot)
                {
                    if (this._submitRespQueue.Count > 0)
                    {
                        return this._submitRespQueue.Dequeue();
                    }
                }
                return null;
            }
            set
            {
                lock (((ICollection)this._submitRespQueue).SyncRoot)
                {
                    this._submitRespQueue.Enqueue(value);
                }
            }
        }
    }
}