using AsiaINFO.SMS.CMPP2;
using AsiaINFO.SMS.DBFactory;
using AsiaINFO.SMS.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
namespace CMPPHOST
{
class Program
{
static byte[] ReadFromStream(int Length, NetworkStream Stream)
{
byte[] buffer = null;
if (Stream.CanRead && Stream.DataAvailable)
{
buffer = new byte[Length];
int num = 0;
for (int i = 0; i < Length; i += num)
{
num = Stream.Read(buffer, i, Length - i);
}
}
return buffer;
}
static byte[] ReadFromStreamWithLock(int Length, NetworkStream Stream)
{
try
{
return ReadFromStream(Length, Stream);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
return null;
}
static void WriteToStream(byte[] bytes, NetworkStream Stream)
{
if (Stream.CanWrite)
{
Stream.Write(bytes, 0, bytes.Length);
}
else
{
throw new Exception("Cann't Write Stream");
}
}
static void WriteToStreamWithLock(byte[] data, NetworkStream Stream)
{
try
{
if (Stream == null)
{
throw new Exception("Stream is null.");
}
if (!Stream.CanWrite) throw new Exception("Cann't Write Stream!");
WriteToStream(data, Stream);
}
catch (Exception exception)
{
IsConnected = false;
Console.Write(exception);
}
}
static void DealConnResp(byte[] data)
{
CMPP_CONNECT_RESP cmpp_connect_resp = new CMPP_CONNECT_RESP(data);
IsConnected = cmpp_connect_resp.Status == 0;
}
static bool IsConnected = false;
static void DistributeData(byte[] data)
{
try
{
Console.WriteLine("收到消息了!");
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)
{
Console.WriteLine("返回的commandId:" + header.Command_Id + " 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
}
Console.Write(header.Command_Id);
switch (header.Command_Id)
{
case CMPP_Command_Id.CMPP_CONNECT_RESP:
DealConnResp(data);
return;
default:
DealConnResp(data);
return;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
static NetworkStream _NetworkStream = null;
private static string _Source_Addr="";
static void Main(string[] args)
{
string userId= SysConf.Instance.SPID;
string passwd = SysConf.Instance.PASSWD;
string host = SysConf.Instance.ServerIP;
int port = SysConf.Instance.ServerPort;
_Source_Addr = userId;
uint seqId = 1;
CMPP_CONNECT cmpp_connect = new CMPP_CONNECT(userId, passwd, DateTime.Now, 0x20, seqId);
var tc = new TcpClient();
tc.Connect(host, port);
if (_NetworkStream != null)
{
try
{
_NetworkStream.Close();
_NetworkStream = null;
}
catch (Exception)
{
}
}
Console.WriteLine("连接服务器成功");
_NetworkStream = tc.GetStream();
WriteToStreamWithLock(cmpp_connect.ToBytes(), _NetworkStream);
Console.WriteLine("开始登录服务器");
while (true) {
if (_NetworkStream.CanRead && _NetworkStream.DataAvailable)
{
Console.WriteLine("接收到数据流...");
byte[] bytes = new byte[12];
bytes = Program.ReadFromStreamWithLock(12, _NetworkStream);
MessageHeader header = new MessageHeader(bytes);
byte[] dst = new byte[header.Total_Length];
Buffer.BlockCopy(bytes, 0, dst, 0, bytes.Length);
int length = ((int)header.Total_Length) - 12;
if (length > 0)
{
bytes = ReadFromStreamWithLock(length, _NetworkStream);
Buffer.BlockCopy(bytes, 0, dst, 12, bytes.Length);
}
DistributeData(dst);
Console.WriteLine("连接成功");
IsConnected = true;
break;
}
}
int maxSmsCount = 1;
DateTime startdate = DateTime.Now;
System.Threading.Thread.Sleep(500);
string batchId = DateTime.Now.ToString("yyyyMMddHHmmss");
Console.WriteLine("开始发送:" + startdate.ToString("yyyy-MM-dd HH:mm:ss:ms"));
for (var i = 0; i < maxSmsCount; i++) {
string mobile = "13611452175"; // "1350201" + string.Format("{0:D4}", i);
Console.WriteLine(mobile);
DateTime oneStartTime = DateTime.Now;
Console.WriteLine("发送一条开始时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms"));
MTWaitInfo mtInfo = new MTWaitInfo(Guid.NewGuid().ToString(), "10690183", "183", mobile, 1, "01","0","【掌骏】验证码测试短信,请忽略,批次:"+ batchId + "序号:"+i,"","",1,1);
sendSubmit(new SubmitInfo(mtInfo, (uint)i, 0));
DateTime oneEndTime = DateTime.Now;
Console.WriteLine("发送一条结束时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms") + "一条时长:" + (oneEndTime - oneStartTime).TotalMilliseconds);
}
DateTime endDate = DateTime.Now;
Console.WriteLine("结束发送:" + endDate.ToString("yyyy-MM-dd HH:mm:ss:ms"));
Console.WriteLine("发送总量:" + maxSmsCount);
double diff = (endDate - startdate).TotalSeconds;
double agv = maxSmsCount / diff;
Console.WriteLine(string.Format("总共消耗时间:{0} 平均每秒发送量:{1:N4}", diff, agv));
Console.Read();
//连接服务器
}
static void Submit(ulong MsgId, string mobile, string MsgContent, uint SequenceId, string Service_Id, string FeeType, string FeeCode, string Src_Id, string WapURL, uint MsgSum, uint MsgSub)
{
if ((mobile != null) && (mobile.Length == 11))
{
mobile = "86" + mobile;
}
Submit(MsgId, mobile, new string[] { mobile }, MsgContent, SequenceId, Service_Id, FeeType, FeeCode, Src_Id, WapURL, MsgSum, MsgSub);
}
///
///
///
///
/// 收费
///
///
///
/// 企业帐户如 hs0055
/// 默认都为01
/// 0
/// 接入号:如 1069022600055
///
///
///
static void Submit(ulong MsgId, string FeeTerminalId, string[] DestTerminalId, string MsgContent, uint SequenceId, string Service_Id, string FeeType, string FeeCode, string Src_Id, string WapURL, uint MsgSum, uint MsgSub)
{
Submit(MsgId, 1, 0, FeeTerminalId, DestTerminalId, MsgContent, SequenceId, Service_Id, FeeType, FeeCode, Src_Id, WapURL, MsgSum, MsgSub);
}
///
///
///
///
/// 1表示需要上报状态报告
///
///
///
///
///
///
///
///
///
///
///
///
static void Submit(ulong MsgId, uint RegisteredDelivery, uint Fee_UserType, string FeeTerminalId, string[] DestTerminalId, string MsgContent, uint SequenceId, string Service_Id, string FeeType, string FeeCode, string Src_Id, string WapURL, uint MsgSum, uint MsgSub)
{
CMPP_SUBMIT submit = new CMPP_SUBMIT(SequenceId)
{
WapURL = WapURL,
Msg_Id = MsgId,
Pk_total = MsgSum,
Pk_number = MsgSub,
Registered_Delivery = RegisteredDelivery,
Msg_level = 1,
Service_Id = Service_Id,
Fee_UserType = Fee_UserType,
Fee_terminal_Id = "",
TP_pId = 0,
TP_udhi = 0,
Msg_Fmt = 8,
Msg_src = _Source_Addr,
FeeType = FeeType,
FeeCode = FeeCode,
ValId_Time = "",
At_Time = "",
Src_Id = Src_Id,
Dest_terminal_Id = DestTerminalId,
DestUsr_tl = (uint)DestTerminalId.Length,
Msg_Content = MsgContent,
Reserve = ""
};
WriteToStreamWithLock(submit.ToBytes(), _NetworkStream);
}
private static void sendSubmit(SubmitInfo submit)
{
//长短信处理
string smscontent = submit.MTWait.SMS_CONTENT;
if (!MTFactory.checkSplitSMSContent(smscontent))
{
Submit(0L, submit.MTWait.MOBILE_NO, submit.MTWait.SMS_CONTENT, submit.SeqID, submit.MTWait.SERV_CODE, submit.MTWait.FEE_TYPE, submit.MTWait.FEE, submit.MTWait.LONG_SERV_NO, submit.MTWait.WapURL, submit.MTWait.MSG_SUM, submit.MTWait.MSG_SUB);
}
else
{
Dictionary dict = MTFactory.SplitContent(smscontent);
foreach (KeyValuePair pair in dict)
{
Submit(0L, submit.MTWait.MOBILE_NO, pair.Value, submit.SeqID, submit.MTWait.SERV_CODE, submit.MTWait.FEE_TYPE, submit.MTWait.FEE, submit.MTWait.LONG_SERV_NO, submit.MTWait.WapURL, (uint)dict.Count, (uint)pair.Key);
}
}
}
}
}