Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            List <CmppClient> clientList = new List <CmppClient>();

            for (int i = 0; i < 1; i++)
            {
                CmppClientConfig config = new CmppClientConfig()
                {
                    ConnNum       = 10,
                    ClientId      = "Test" + i,
                    Ip            = "127.0.0.1",
                    Password      = "******",
                    Port          = 7890,
                    SourceAddress = "614450",
                    UserName      = "******"
                };
                CmppClient cmppClient = new CmppClient(config, new SmsHandler());
                clientList.Add(cmppClient);
            }
            Console.ReadLine();

            Sms.Common.Sms sms = new Sms.Common.Sms()
            {
                ExObj        = "",
                ExtendedCode = "0",
                //Id = Guid.NewGuid().ToString(),
                Messgae = "【测试】123456",
                Mobile  = "13800138000"
            };
            clientList[0].SubmitSmsAsync(sms).ContinueWith(t => {
                if (t.Result)
                {
                    Console.WriteLine("===================>发送完成");
                }
                else
                {
                    Console.WriteLine("===================>发送失败");
                }
            });
            Console.ReadLine();

            ClientClose(clientList).ContinueWith(x =>
            {
                logger.LogInformation("=====================main>ClientClose task complete");
            });
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        //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);
        }
Ejemplo n.º 3
0
 public override void ConnectSuccessCallBack()
 {
     Sms.Common.Sms sms = new Sms.Common.Sms()
     {
         ExObj        = "",
         ExtendedCode = "0",
         //Id = Guid.NewGuid().ToString(),
         Messgae = "【test】123456",
         Mobile  = "13800138000"
     };
     client.SubmitSmsAsync(sms).ContinueWith(t =>
     {
         if (t.Result)
         {
             logger.Info("===================>send complete");
         }
         else
         {
             logger.Info("===================>send fail");
         }
     });
 }