Exemple #1
0
        public void TestProtocol()
        {
            using (var client = new TcpClient(_config.GatewayIp, _config.GatewayPort))
            {
                var dt = DateTime.Now;
                SendMessage(client.Client, new CmppConnect
                {
                    TimeStamp = uint.Parse(string.Format("{0:MMddhhmmss}", dt)),
                    AuthenticatorSource = CreateAuthenticatorSource(dt),
                    Version = CmppConstants.Version,
                    SourceAddress = _config.GatewayUsername,
                });
                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                var buffer = new byte[12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                var head = new CmppHead();
                head.FromBytes(buffer);
                Assert.AreEqual(CmppConstants.CommandCode.ConnectResp, head.CommandId);

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[head.TotalLength - 12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                var response = new CmppConnectResp();
                response.FromBytes(buffer);
                Assert.AreEqual(0, response.Status);
                // connected
                SendMessage(client.Client, new CmppActiveTest());

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                head = new CmppHead();
                head.FromBytes(buffer);
                Assert.AreEqual(CmppConstants.CommandCode.ActiveTestResp, head.CommandId);

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[head.TotalLength - 12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                var active = new CmppActiveTestResp();
                active.FromBytes(buffer);

                Console.WriteLine("Sending message.");
                var submit = new CmppSubmit
                {
                    // 信息内容。
                    MsgContent = "Test",
                    // 信息编码。
                    MsgFmt = (byte)CmppEncoding.UCS2,
                    // SP的服务代码,将显示在最终用户手机上的短信主叫号码。
                    SrcId = _config.SpCode + "1111",
                    // 接收短信的电话号码列表。
                    DestTerminalId = new[] { "13800138000" },
                    // 业务标识(如:woodpack)。
                    ServiceId = _config.ServiceId,
                    // 是否要求返回状态报告。
                    RegisteredDelivery = 1,
                    // 资费类别。
                    FeeType = string.Format("{0:D2}", (int)FeeType.Free),
                    // 计费用户。
                    FeeUserType = (byte)FeeUserType.SP,
                    // 被计费的号码(feeUserType 值为 FeeUser 时有效)。
                    FeeTerminalId = _config.SpCode,
                    // 被计费号码的真实身份(“真实号码”或“伪码”)。
                    FeeTerminalType = 0,
                    // 信息费(以“分”为单位,如:10 分代表 1角)。
                    FeeCode = "10",
                    // 点播业务的 linkId。
                    LinkId = "",
                    PkTotal = (byte)(_config.AttemptRemoveSignature ? 3 : 1),
                    PkNumber = (byte)(_config.AttemptRemoveSignature ? 2 : 1),
                    MsgLevel = 0,
                    TPPId = 0,
                    TPUdhi = 0,
                    MsgSrc = _config.GatewayUsername,
                    ValidTime = "",
                    AtTime = ""
                };
                SendMessage(client.Client, submit);

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                head = new CmppHead();
                head.FromBytes(buffer);
                Assert.AreEqual(CmppConstants.CommandCode.SubmitResp, head.CommandId);

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[head.TotalLength - 12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                var submitResponse = new CmppSubmitResp();
                submitResponse.FromBytes(buffer);
                Assert.AreEqual(0, submitResponse.Result);

                Console.WriteLine("Waiting for message report.");

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                head = new CmppHead();
                head.FromBytes(buffer);
                Assert.AreEqual(CmppConstants.CommandCode.Deliver, head.CommandId);

                Assert.True(client.Client.Poll(-1, SelectMode.SelectRead));
                buffer = new byte[head.TotalLength - 12];
                Assert.AreEqual(buffer.Length, client.Client.Receive(buffer));
                var deliver = new CmppDeliver();
                deliver.FromBytes(buffer);
                Assert.AreEqual(1, deliver.RegisteredDelivery);
                var report = deliver.GetReport();
                Assert.AreEqual(submitResponse.MsgId, report.MsgId);

                Console.WriteLine("Message report: [{0}] {1}", report.MsgId, report.Stat);
            }
        }
Exemple #2
0
 private CmppWindow _SendAsync(
     string extendedCode,
     string[] destinations,
     string text,
     bool isLongMessage, bool needStatusReport)
 {
     var submit = new CmppSubmit
     {
         // 信息内容。
         MsgContent = text,
         // 信息编码。
         MsgFmt = (byte)(_config.AttemptRemoveSignature ? CmppEncoding.Special : CmppEncoding.UCS2),
         // SP的服务代码,将显示在最终用户手机上的短信主叫号码。
         SrcId = _config.SpCode + extendedCode,
         // 接收短信的电话号码列表。
         DestTerminalId = destinations,
         // 业务标识(如:woodpack)。
         ServiceId = _config.ServiceId,
         // 是否要求返回状态报告。
         RegisteredDelivery = (byte)(needStatusReport ? 1 : 0),
         // 资费类别。
         FeeType = string.Format("{0:D2}", (int)FeeType.Free),
         // 计费用户。
         FeeUserType = (byte)FeeUserType.SP,
         // 被计费的号码(feeUserType 值为 FeeUser 时有效)。
         FeeTerminalId = _config.SpCode,
         // 被计费号码的真实身份(“真实号码”或“伪码”)。
         FeeTerminalType = 0,
         // 信息费(以“分”为单位,如:10 分代表 1角)。
         FeeCode = "05",
         // 点播业务的 linkId。
         LinkId = "",
         MsgLevel = 0,
         TPPId = 0,
         TPUdhi = (byte)(isLongMessage ? 1 : 0),
         MsgSrc = _config.GatewayUsername,
         ValidTime = "",
         AtTime = ""
     };
     var window = new CmppWindow
     {
         Message = submit,
         WaitHandle = new ManualResetEvent(false)
     };
     lock (_pendingMessages)
         _pendingMessages.Enqueue(window);
     return window;
 }