void SWSet(SlidingWindow send) { lock (_sendWindows) { for (int i = 0; i < _sendWindows.Length; i++) { if (_sendWindows[i].Status == WindowStatus.Idle) { _sendWindows[i] = send; _sendWindows[i].Status = WindowStatus.Fill; _sendsCount++; return; } } } }
private List <SlidingWindow> GetSend(SMS sms) { List <SlidingWindow> submits = new List <SlidingWindow>(); //拆分长消息 int pkSize; int pkNum = SMSSplit.GetSplitNumber(sms.Content, sms.Signature, out pkSize); if (pkNum > 1) { for (int j = 0; j < sms.Number.Count; j++) { string content = sms.Content; //发送号码 for (int i = 0; i < pkNum; i++) { CMPP_SUBMIT submit = new CMPP_SUBMIT(); submit.Head = new CMPP_HEAD(); submit.Head.CommandID = (uint)CMPP_COMMAND.CMD_SUBMIT; submit.Head.SequenceID = Sequence.Instance.CreateID(); submit.MsgID = 0; submit.SrcID = _setting.SrcID + sms.SPNumber; submit.PkTotal = (byte)pkNum; submit.PkNumber = (byte)(i + 1); submit.ServiceID = _setting.ServiceID; submit.FeeUserType = _setting.FeeUserType; submit.FeeTerminalID = _setting.FeeTerminalID; submit.FeeTerminalType = _setting.FeeTerminalType; submit.TPPID = 0; submit.MsgFmt = 8; submit.FeeType = _setting.FeeType; submit.FeeCode = _setting.FeeCode; submit.DestTerminalType = _setting.DestTerminalType; submit.MsgLevel = 1; submit.MsgSrc = _setting.SPID; submit.ValidTime = _setting.ValidTime; submit.AtTime = ""; submit.RegisteredDelivery = sms.StatusReport == StatusReportType.Disable ? (byte)0 : (byte)1; submit.LinkID = ""; //信息内容 submit.MsgContent = SMSSplit.GetSubString(content, pkSize); content = content.Substring(submit.MsgContent.Length); if (submit.PkNumber == pkNum) { submit.MsgContent = submit.MsgContent + sms.Signature; } submit.SetHeader(); //信息内容长度 submit.MsgLength = Convert.Length(submit.MsgContent, submit.MsgFmt); submit.DestTerminalID = new string[] { sms.Number[j] }; submit.DestUsrTl = (byte)1; SMS tsms = new SMS(); tsms.Account = sms.Account; tsms.Audit = sms.Audit; tsms.Channel = sms.Channel; tsms.Content = submit.MsgContent; tsms.Filter = sms.Filter; tsms.Level = sms.Level; tsms.Number = sms.Number; tsms.SendTime = sms.SendTime; tsms.SerialNumber = sms.SerialNumber; tsms.StatusReport = sms.StatusReport; tsms.Signature = sms.Signature; tsms.SPNumber = sms.SPNumber; tsms.WapURL = sms.WapURL; SlidingWindow _send = new SlidingWindow(); _send.Report = new SendEventArgs(tsms, "", false, ((ushort)PlatformCode.CMPP + (ushort)SystemCode.SendReady), "", (ushort)submit.PkTotal, (ushort)submit.PkNumber); _send.MSG = submit; _send.SendCount = 0; _send.SendTime = DateTime.Now; _send.Status = WindowStatus.Fill; submits.Add(_send); } } } else { for (int j = 0; j < sms.Number.Count; j++) { CMPP_SUBMIT submit = new CMPP_SUBMIT(); submit.Head = new CMPP_HEAD(); submit.Head.CommandID = (uint)CMPP_COMMAND.CMD_SUBMIT; submit.Head.SequenceID = Sequence.Instance.CreateID(); submit.MsgID = 0; submit.SrcID = _setting.SrcID + sms.SPNumber; submit.PkTotal = 1; submit.PkNumber = 1; submit.ServiceID = _setting.ServiceID; submit.FeeUserType = _setting.FeeUserType; submit.FeeTerminalID = _setting.FeeTerminalID; submit.FeeTerminalType = _setting.FeeTerminalType; submit.TPPID = 0; submit.TPUdhi = 0; submit.MsgFmt = 15; submit.FeeType = _setting.FeeType; submit.FeeCode = _setting.FeeCode; submit.DestTerminalType = _setting.DestTerminalType; submit.MsgLevel = 1; submit.MsgSrc = _setting.SPID; submit.ValidTime = _setting.ValidTime; submit.AtTime = ""; submit.RegisteredDelivery = sms.StatusReport == StatusReportType.Disable ? (byte)0 : (byte)1; submit.LinkID = ""; //信息内容 submit.MsgContent = sms.Content + sms.Signature; //信息内容长度 submit.MsgLength = Convert.Length(submit.MsgContent, submit.MsgFmt); submit.DestTerminalID = new string[] { sms.Number[j] }; submit.DestUsrTl = (byte)1; SMS tsms = new SMS(); tsms.Account = sms.Account; tsms.Audit = sms.Audit; tsms.Channel = sms.Channel; tsms.Content = submit.MsgContent; tsms.Filter = sms.Filter; tsms.Level = sms.Level; tsms.Number = sms.Number; tsms.SendTime = sms.SendTime; tsms.SerialNumber = sms.SerialNumber; tsms.StatusReport = sms.StatusReport; tsms.Signature = sms.Signature; tsms.SPNumber = sms.SPNumber; tsms.WapURL = sms.WapURL; SlidingWindow _send = new SlidingWindow(); _send.Report = new SendEventArgs(sms, "", false, ((ushort)PlatformCode.CMPP + (ushort)SystemCode.SendReady), "", submit.PkTotal, submit.PkNumber); _send.MSG = submit; _send.SendCount = 0; _send.SendTime = DateTime.Now; _send.Status = WindowStatus.Fill; submits.Add(_send); } } return(submits); }
void _sc_DataReceived(byte[] data) { int headSize = 0; Byte[] buffer = null; CMPP_HEAD head; try { if (data.Length > 0) { buffer = new byte[12]; Buffer.BlockCopy(data, 0, buffer, 0, buffer.Length); head.TotalLength = Convert.ToUInt32(buffer, 0); head.CommandID = Convert.ToUInt32(buffer, 4); head.SequenceID = Convert.ToUInt32(buffer, 8); } else { OnNetworkError(new Exception("Head data error.")); return; } } catch (Exception ex) { OnNetworkError(ex); return; } switch ((CMPP_COMMAND)head.CommandID) { case CMPP_COMMAND.CMD_SUBMIT_RESP: CMPP_SUBMIT_RESP submit_resp = new CMPP_SUBMIT_RESP(); submit_resp.Head = head; try { headSize = Marshal.SizeOf(head); buffer = new byte[head.TotalLength - headSize]; Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length); submit_resp.MsgID = Convert.ToUInt64(buffer, 0); submit_resp.Result = Convert.ToUInt32(buffer, 8); } catch (Exception ex) { OnNetworkError(ex); return; } // DebugLog.Instance.Write<CMPP_SUBMIT_RESP>("Client Received -> CMPP_SUBMIT_RESP", submit_resp); MessageTools.MessageHelper.Instance.WirteInfo("Client Received -> CMPP_SUBMIT_RESP" + submit_resp.ToString()); SlidingWindow send = SWGet(head.SequenceID); if (send.Status == WindowStatus.Idle) { return; } //流控 if (submit_resp.Result == 8) { SWClr(head.SequenceID); send.SendTime = DateTime.Now; send.MSG.SequenceID = Sequence.Instance.CreateID(); Send(send.MSG); return; } if (SendEvent != null) { send.Report.Succeed = false; switch (submit_resp.Result) { case 0: send.Report.Succeed = true; send.Report.Describe = "等待报告"; break; case 1: send.Report.Describe = "消息结构错"; break; case 2: send.Report.Describe = "命令字错"; break; case 3: send.Report.Describe = "消息序号重复"; break; case 4: send.Report.Describe = "消息长度错"; break; case 5: send.Report.Describe = "资费代码错"; break; case 6: send.Report.Describe = "超过最大信息长"; break; case 7: send.Report.Describe = "业务代码错"; break; case 8: send.Report.Describe = "流量控制错"; break; case 9: send.Report.Describe = "本网关不负责服务此计费号码"; break; case 10: send.Report.Describe = "源号码错误"; break; case 11: send.Report.Describe = "短信内容错误"; break; case 12: send.Report.Describe = "被计费号码错误"; break; case 13: send.Report.Describe = "终端号码错误"; break; } send.Report.Serial = submit_resp.MsgID.ToString(); SendEvent(this, send.Report); } //清除窗口 SWClr(head.SequenceID); break; case CMPP_COMMAND.CMD_CANCEL_RESP: CMPP_CANCEL_RESP cancel_resp = new CMPP_CANCEL_RESP(); cancel_resp.Head = head; try { headSize = Marshal.SizeOf(head); buffer = new byte[head.TotalLength - headSize]; Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length); cancel_resp.SuccessID = Convert.ToUInt32(buffer, 0); } catch (Exception ex) { OnNetworkError(ex); return; } // DebugLog.Instance.Write<CMPP_CANCEL_RESP>("Client Received -> CMPP_CANCEL_RESP", cancel_resp); break; case CMPP_COMMAND.CMD_CONNECT_RESP: CMPP_CONNECT_RESP connect_resp = new CMPP_CONNECT_RESP(); connect_resp.Head = head; try { headSize = Marshal.SizeOf(head); buffer = new byte[head.TotalLength - headSize]; Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length); connect_resp.Status = Convert.ToUInt32(buffer, 0); connect_resp.AuthenticatorISMG = new Byte[16]; Array.Copy(buffer, 4, connect_resp.AuthenticatorISMG, 0, 16); connect_resp.Version = buffer[buffer.Length - 1]; } catch (Exception ex) { OnNetworkError(ex); return; } // DebugLog.Instance.Write<CMPP_CONNECT_RESP>("Client Received -> CMPP_CONNECT_RESP", connect_resp); switch (connect_resp.Status) { case 0: _connect = true; autoConnectEvent.Set(); OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT, "登录成功")); break; default: switch (connect_resp.Status) { case 1: OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "消息结构错")); break; case 2: OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "非法源地址")); break; case 3: OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "认证错")); break; case 4: OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "版本太高")); break; default: OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, string.Format("其他错误(错误码:{0})", connect_resp.Status))); break; } OnSMSEvent(new SMSEventArgs(SMS_Event.SP_CONNECT_ERROR, "错误代码 " + connect_resp.Status)); break; } break; case CMPP_COMMAND.CMD_ACTIVE_TEST_RESP: CMPP_ACTIVE_TEST_RESP resp = new CMPP_ACTIVE_TEST_RESP(); resp.Head = head; try { headSize = Marshal.SizeOf(head); buffer = new byte[head.TotalLength - headSize]; Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length); resp.Reserved = buffer[0]; } catch (Exception ex) { OnNetworkError(ex); return; } // DebugLog.Instance.Write<CMPP_ACTIVE_TEST_RESP>("Client Received -> CMPP_ACTIVE_TEST_RESP", resp); //SWClr(head.SequenceID); break; case CMPP_COMMAND.CMD_ACTIVE_TEST: // DebugLog.Instance.Write("Client Received -> CMD_ACTIVE_TEST"); MessageTools.MessageHelper.Instance.WirteTest("Client Received -> CMD_ACTIVE_TEST"); CMPP_ACTIVE_TEST_RESP active_test_resp = new CMPP_ACTIVE_TEST_RESP(); active_test_resp.Head = new CMPP_HEAD(); active_test_resp.Head.CommandID = (uint)CMPP_COMMAND.CMD_ACTIVE_TEST_RESP; active_test_resp.Head.SequenceID = head.SequenceID; active_test_resp.Reserved = 0; Send(active_test_resp); MessageTools.MessageHelper.Instance.WirteTest("Client Send -> CMPP_ACTIVE_TEST_RESP"); // DebugLog.Instance.Write<CMPP_ACTIVE_TEST_RESP>("Client Send -> CMPP_ACTIVE_TEST_RESP", active_test_resp); break; case CMPP_COMMAND.CMD_DELIVER: CMPP_DELIVER deliver = new CMPP_DELIVER(); deliver.Head = head; try { headSize = Marshal.SizeOf(head); buffer = new byte[head.TotalLength - headSize]; Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length); deliver.Init(buffer); } catch (Exception ex) { OnNetworkError(ex); return; } // DebugLog.Instance.Write<CMPP_DELIVER>("Client Received -> CMPP_DELIVER", deliver); CMPP_DELIVER_RESP deliver_resp = new CMPP_DELIVER_RESP(); deliver_resp.Head = new CMPP_HEAD(); deliver_resp.Head.CommandID = (uint)CMPP_COMMAND.CMD_DELIVER_RESP; deliver_resp.Head.SequenceID = deliver.Head.SequenceID; deliver_resp.MsgID = deliver.MsgID; deliver_resp.Result = 0; Send(deliver_resp); // DebugLog.Instance.Write<CMPP_DELIVER_RESP>("Client Send -> CMPP_DELIVER_RESP", deliver_resp); if (deliver.RegisteredDelivery == 1) { ////状态报告 if (ReportEvent != null) { CMPP_REPORT report = deliver.GetReport(); bool result = false; string resultMessage = ""; ushort resultCode = ((ushort)PlatformCode.CMPP + (ushort)SystemCode.ReportBack); DateTime time; try { time = new DateTime(DateTime.Now.Year, System.Convert.ToInt32(report.DoneTime.Substring(2, 2)), System.Convert.ToInt32(report.DoneTime.Substring(4, 2)), System.Convert.ToInt32(report.DoneTime.Substring(6, 2)), System.Convert.ToInt32(report.DoneTime.Substring(8, 2)), 0); } catch { time = DateTime.Now; } switch (report.Stat.ToUpper()) { case "DELIVRD": case "RETRIEV": result = true; break; case "EXPIRED": case "DELETED": case "UNDELIV": case "ACCEPTD": case "UNKNOWN": case "REJECTD": default: resultMessage = report.Stat; break; } ReportEventArgs args = new ReportEventArgs(report.MsgID.ToString(), result, resultCode, resultMessage, time); ReportEvent(this, args); } } else { if (DeliverEvent != null) { DeliverEvent(this, new DeliverEventArgs(deliver.MsgID.ToString(), DateTime.Now, deliver.MsgContent, deliver.SrcTerminalID, deliver.DestID, deliver.ServiceID.ToString())); } } break; case CMPP_COMMAND.CMPP_QUERY_RESP: CMPP_QUERY_RESP query_resp = new CMPP_QUERY_RESP(); query_resp.Head = head; try { headSize = Marshal.SizeOf(head); buffer = new byte[head.TotalLength - headSize]; Buffer.BlockCopy(data, headSize, buffer, 0, buffer.Length); query_resp.Init(buffer); } catch (Exception ex) { OnNetworkError(ex); return; } // DebugLog.Instance.Write<CMPP_QUERY_RESP>("Client Received -> CMPP_QUERY_RESP", query_resp); break; case CMPP_COMMAND.CMD_TERMINATE: // DebugLog.Instance.Write("Client Received -> CMD_TERMINATE"); _connect = false; Thread.Sleep(100); break; case CMPP_COMMAND.CMD_ERROR: // DebugLog.Instance.Write("Client Received -> CMD_ERROR"); _connect = false; Thread.Sleep(100); break; } }