public static List <string> SplitLongMessage(string content) { var result = new List <string>(); var messageUCS2 = Encoding.BigEndianUnicode.GetBytes(content); var messageUCS2Len = messageUCS2.Length; if (messageUCS2Len > MaxMessageByteLength) { // long message amount var messageUCS2Count = (messageUCS2Len - 1) / (MaxMessageByteLength - LongMessageHeadLength) + 1; var tpUdhiHead = new byte[LongMessageHeadLength]; tpUdhiHead[0] = 0x05; tpUdhiHead[1] = 0x00; tpUdhiHead[2] = 0x03; tpUdhiHead[3] = CmppTools.GetLongSmsSequenceId(); //(byte)(_sequenceId % 256); tpUdhiHead[4] = (byte)(messageUCS2Count); for (var i = 0; i < messageUCS2Count; i++) { // message sequence tpUdhiHead[5] = (byte)(i + 1); byte[] msgContent; if (i != messageUCS2Count - 1) { // not the last message msgContent = new byte[MaxMessageByteLength]; Array.Copy(tpUdhiHead, msgContent, LongMessageHeadLength); Array.Copy(messageUCS2, i * (MaxMessageByteLength - LongMessageHeadLength), msgContent, LongMessageHeadLength, MaxMessageByteLength - LongMessageHeadLength); } else { // the last message msgContent = new byte[tpUdhiHead.Length + messageUCS2Len - i * (MaxMessageByteLength - LongMessageHeadLength)]; Array.Copy(tpUdhiHead, msgContent, LongMessageHeadLength); Array.Copy(messageUCS2, i * (MaxMessageByteLength - LongMessageHeadLength), msgContent, LongMessageHeadLength, messageUCS2Len - i * (MaxMessageByteLength - LongMessageHeadLength)); } result.Add(Encoding.BigEndianUnicode.GetString(msgContent)); } } else { result.Add(content); } return(result); }
//public async Task<bool> ConnectAsync() //{ // int connNum = Config.ConnNum - sessionMap.Count; // for (int i = 0; i < connNum; i++) // { // try // { // await bootstrap.ConnectAsync(new IPEndPoint(IPAddress.Parse(Config.Ip), Config.Port)); // } // catch (Exception e) // { // ClientStatus = ClientStatus.CONNECT_FAIL; // logger.Error($"connect {Config.Ip}:{Config.Port} exception", e); // return false; // } // } // return true; //} //void StartSubmit() //{ // Stopwatch stopwatch = new Stopwatch(); // stopwatch.Start(); // Task.Factory.StartNew(() => // { // logger.Info($"客户端{Config.ClientId}发送任务开始"); // //重试设置 提交返回失败时重试两次 // var policy = Policy // .HandleResult<bool>((r) => !r) // .RetryAsync(2, async (dr, retryCount) => // { // logger.Info($"客户端{Config.ClientId}发送失败尝试重试, 第{retryCount.ToString()}次重试"); // await Task.CompletedTask; // }); // Sms.Common.Sms sms = null; // while (!tokenSource.IsCancellationRequested) // { // try // { // if (ClientStatus == ClientStatus.CONNECT_SUCCESS && !smsQueue.IsEmpty && smsQueue.TryDequeue(out sms)) // { // policy.ExecuteAsync(async () => await SubmitSmsAsync(sms)) // .ContinueWith(t => // { // if (t.IsFaulted || !t.Result) // { // logger.Error($"客户端{Config.ClientId}发送短信失败, sms:{sms?.ToString()}, exception:{t.Exception}"); // } // }); // } // else // { // Thread.Sleep(100); // } // } // catch (Exception e) // { // logger.Error(e); // } // } // }, tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).ContinueWith(t => // { // logger.Info($"客户端{Config.ClientId}发送任务完成, 运行时间:{stopwatch.ElapsedMilliseconds / 1000}s"); // }); //} public async Task <bool> SubmitSmsAsync(Sms.Common.Sms sms) { var session = sessionMap.GetLoginSuccSessionRandom(); if (session == null && !session.Send) { return(false); } List <string> list = CmppTools.SplitLongMessage(sms.Messgae); var isLongMessage = list.Count > 1; string[] destinations = new string[] { sms.Mobile }; string longid = Config.ClientId.ToString() + CmppTools.GetHeadSequenceId().ToString(); for (int i = 0; i < list.Count; i++) { var content = list[i]; var serial = i + 1; var count = list.Count; var submit = Cmpp2CmppSubmitPack(content, sms.ExtendedCode, destinations, isLongMessage, count, serial); MsgEx msgEx = new MsgEx() { Count = list.Count, ExObj = sms.ExObj, Id = longid, IsLong = isLongMessage, Serial = i }; var sequenceId = CmppTools.GetHeadSequenceId(); var smsPacket = CmppTools.GroupPacket(submit, sequenceId); while (!matchQueue.Add(session.Channel, smsPacket, msgEx, sequenceId.ToString())) { logger.Debug($"wait sms add match cache"); Thread.Sleep(1); } logger.Info($"sms {longid} add match cache"); await CmppTools.SendAsync(session.Channel, smsPacket); } return(true); }
internal static SmsPacket GroupPacket(IMessage message) { var sequenceId = CmppTools.GetHeadSequenceId(); return(GroupPacket(message, sequenceId)); }