📄 cngpprotocol.cs
字号:
//
this.cngpSendMessage.InsertErrorMessage(sendinfo.Mobileid.Trim()+":"+sendinfo.ServiceId.Trim()+":"+
sendinfo.Content.Trim());
}
}
}
}
Thread.Sleep(2000);
}
catch(Exception ex)
{
Logs.writeLog(string.Concat(
ex.Message.ToString(),
"\r\n",
ex.StackTrace,
"\r\n",
ex.TargetSite.ToString(),
"\r\n",
ex.Source),false);
}
}
}
/// <summary>
/// 重新发送信息使用,只在CngpProtocol类中使用
/// </summary>
/// <param name="Sendinfo">发送信息Struct</param>
[LastModified("2006-01-06","重新发送信息,只在CngpProtocol类中使用")]
[LastModified("2007-02-01","状态报告根据配置文件设置是否需要")]
private void SendStruct(SendInfo Sendinfo)
{
//将发送信息内容转换为字节数组
byte[] byteContent=Encoding.GetEncoding("GB2312").GetBytes(Sendinfo.Content.Replace("\0",""));
//获取发送信息字节数组的长度,以字节表示
byte Length=(byte)byteContent.Length;
//提交
Cngp_Submit(
ConfigInformation.CorpId, //Corp Id
(byte)Sendinfo.SubType, //子类型
(byte)ConfigInformation.Needreport, //是否需要状态报告
(byte)Sendinfo.Priority, //优先级
Sendinfo.ServiceId, //栏目代码
Sendinfo.FeeType, //计费类型
(byte)(CngpProtocol.FeeUserType.Dest), //计费用户类型
Sendinfo.FeeCode, //计费金额
((byte)CngpMessage.Character.GB), //信息格式
"0", //validtime
"0", //attime
Sendinfo.SourceId.Trim(), //下行代码
Sendinfo.Mobileid.Trim(), //计费用户号码
1, //短消息接收号码总数
Sendinfo.Mobileid.Trim(), //短消息接收号码
Length, //短消息长度
Sendinfo.Content.Trim(), //短消息内容
Sendinfo.Sequence //序列值
);
}
/// <summary>
/// 提交数据,更新最后发送时间,加入到SubmitArray中
/// </summary>
/// <param name="Sendinfo">带发送信息</param>
/// <param name="outSequence" >返回序列制</param>
[LastModified("2007-02-01","根据配置文件设置是否需要状态报告")]
public void SendStruct(SendInfo Sendinfo,ref uint outSequence)
{
//将发送信息内容转换为字节数组
byte[] byteContent=Encoding.GetEncoding("GB2312").GetBytes(Sendinfo.Content.Replace("\0",""));
//获取发送信息字节数组的长度,以字节表示
byte Length=(byte)byteContent.Length;
//提交
Cngp_Submit(
ConfigInformation.CorpId, //Corp Id
(byte)Sendinfo.SubType, //子类型
(byte)ConfigInformation.Needreport, //是否需要状态报告
(byte)Sendinfo.Priority, //优先级
Sendinfo.ServiceId, //栏目代码
Sendinfo.FeeType, //计费类型
(byte)(CngpProtocol.FeeUserType.Dest), //计费用户类型
Sendinfo.FeeCode, //计费金额
((byte)CngpMessage.Character.GB), //信息格式
"0", //validtime
"0", //attime
Sendinfo.SourceId.Trim(), //下行代码
Sendinfo.Mobileid.Trim(), //计费用户号码
1, //短消息接收号码总数
Sendinfo.Mobileid.Trim(), //短消息接收号码
Length, //短消息长度
Sendinfo.Content.Trim(), //短消息内容
ref outSequence
);
}
/// <summary>
/// 提供Sequence
/// </summary>
/// <returns>返回uint的序号</returns>
[LastModified("2006-03-13","增加check检查")]
public uint sequence()
{
try
{
checked
{
Sequence++;
}
}
catch
{
Sequence=0;
}
return Sequence;
}
/// <summary>
/// sp方发起连接终止,发送请求
/// </summary>
public void Cngp_Terminate()
{
if(IsConnectedWithIsmg==true)
{
byte[] ByteHeader=new byte[16];//包头
byte[] MessageBody=new byte[0];//0大小包体
uint CommandId=(uint)CngpMessage.Command_Id.exit;//commandid
uint Command_Status=0;
//
//封包
//
cngpMessage.Pack(CommandId,Command_Status,sequence(),MessageBody,ref ByteHeader);
//
//发送
//
int SendBytes=cngpSocket.SendToIsmg(ByteHeader);
}
}
/// <summary>
/// Ismg发起停止连接
/// </summary>
/// <param name="Sequence">ismg发过来的sequence</param>
public void Cngp_Terminate_Resp(uint Sequence)
{
uint CommandId=(uint)CngpMessage.Command_Id.exit_resp;
byte[] messageBody=new byte[0];//包体
byte[] Message=null;//包头
uint Command_Status=0;//status,0:成功标记
cngpMessage.Pack(CommandId,Command_Status,Sequence,messageBody,ref Message);
int i=cngpSocket.SendToIsmg(Message);
//
//断开连接
//
EndConnect();
}
/// <summary>
/// 断开连接
/// 如果同Ismg还在连接状态,那么断开Socket,关闭进程队列中的所有进程,更新连接标志
/// </summary>
public void EndConnect()
{
IsConnectedWithIsmg=false;
cngpSocket.Exit();
foreach(Thread thread in ThreadArray)
{
thread.Abort();
thread.Join(1000);
}
Logs.writeLog("成功同服务器断开连接");
}
/// <summary>
/// 监听线程,接收socket,根据CommandId,选择处理过程
/// </summary>
[LastModified("2006-03-13","修改循环,停止使用do while循环")]
[LastModified("2007-04-03","修改DealSubResp")]
public void ReceiveData()
{
try
{
byte[] PartMessage=null;
uint Length=0;
//循环
while(true)
{
//
//检测到退出标志
//
if(quitFlag==true)
{
break;
}
/*-----2006-03-13 停止使用
do
{
if(IsConnectedWithIsmg==false)
{
Thread.CurrentThread.Abort();
}
}
while(cngpSocket.ReceiveFromIsmg(ref PartMessage,ref Length)<0);
-----------------*/
uint count=cngpSocket.ReceiveFromIsmg(ref PartMessage,ref Length);
//---unpack------------------------------------------------------------
uint reCommandId=0;
uint reCommandStatus=0;
uint returnSequence=0;
byte[] MessageBody=null;
cngpMessage.UnPack(Length,ref reCommandId,ref reCommandStatus,ref returnSequence,ref MessageBody,PartMessage);
//标记接收时间
this.RenewTime();
//
//检查发送信息时钟
if(timerSendData.Enabled==false )
{
TimeSpan span=DateTime.Now-SleepTime;
if(span.TotalSeconds>5)
{
timerSendData.Enabled=true;
}
}
//----------------------根据CommandId作处理------------------------------
switch(reCommandId)
{
case((uint)CngpMessage.Command_Id.exit)://ismg发起停止
Cngp_Terminate_Resp(returnSequence);
break;
//2007-4-3
case((uint)CngpMessage.Command_Id.submit_resp)://submit回复
DealSubResp(reCommandStatus,returnSequence,MessageBody);
// MtCount++;
break;
case((uint)CngpMessage.Command_Id.exit_resp)://sp发起的断开回复
EndConnect();
break;
case ((uint)CngpMessage.Command_Id.active_test_resp)://active 回复
break;
case((uint)CngpMessage.Command_Id.active_test)://ismg发起
Cngp_Active_Test_Resp(returnSequence);
break;
case((uint)CngpMessage.Command_Id.deliver):
//log
Cngp_Deliver_Resp(returnSequence,MessageBody);
break;
}
}
}
catch(Exception ex)
{
Logs.writeLog(string.Concat(
ex.Message,
"\r\n",
ex.StackTrace,
"\r\n",
ex.TargetSite.ToString(),
"\r\n",
ex.Source),false);
}
}
/// <summary>
/// 更新操作时间
/// </summary>
[LastModified("2006-03-13","更新操作时间")]
private void RenewTime()
{
//标记接收时间
LastTalk=DateTime.Now;
}
/// <summary>
/// 处理Submit的回包,只有当网关正常处理提交后,才会给Mt计数器累加
/// </summary>
/// <param name="CommandStatus">接收到的消息的CommandStatus</param>
[LastModified("2006-01-13","更改发送停止条件,如果没有回复的信息超过4条,那么停止发送,"+
"收到正确回复后,继续发送;并且,停止发送时间在10秒后,重新发送,由接收控制")]
[LastModified("2006-03-14","增加事件,当MT累加时触发,更新界面显示")]
[LastModified("2007-04-03","获取MsgId,并写入数据库")]
private void DealSubResp(uint CommandStatus,uint returnSequence,byte[] MessageBody)
{
//
//2007-04-02
//获取MessageId
byte[] MsgID=new byte[10];
Array.Copy(MessageBody,0,MsgID,0,10);
string messageid=Encoding.ASCII.GetString(MsgID).Replace("\0","");
//
//检查Sleeptime
TimeSpan sleeptime=DateTime.Now-this.SleepTime;
if(sleeptime.Seconds>10)
{
this.timerSendData.Enabled=true;
}
//-----------------处理发送的信息--------------------------------------------
lock(submitArray)
{
try
{
for(int n=submitArray.Count-1;n>=0;n--)
{
SendInfo Sendinfo=(SendInfo)submitArray[n];
if(Sendinfo.Sequence==returnSequence) //找到对应的发送信息
{
if(CommandStatus==0) //如果成功
{
submitArray.Remove(Sendinfo); //从集合中删除
//
//MT计数器增加
//
MtCount++;
//
//2006-03-14
//事件发生
//
MtchangeEventArgs mtArgs=new MtchangeEventArgs(MtCount);
if(MtOnChange!=null)
{
MtOnChange(this,mtArgs);
}
//
//
//
Logs.writeLog(Sendinfo.Mobileid.Trim()+":"+Sendinfo.ServiceId.Trim()+":"+Sendinfo.Content.Trim().Replace("\0","")+":Ok");
//
//2007-04-03
//写入Msgid到数据库
this.cngpSendMessage.InsertSubmitResp(Sendinfo.Mobileid,Sendinfo.ServiceId,Sendinfo.Content,messageid);
//
if(timerSendData.Enabled==false)
{
timerSendData.Enabled=true;
}
//
FeebackOk=true;
break;
}
else //没有成功
{
Logs.writeLog("CommandStatus:"+CommandStatus.ToString(),false);
SendStruct(Sendinfo); //重新发送
Sendinfo.Times++; //更新发送次数
Sendinfo.DealTime=DateTime.Now; //更新发送时间
//
//如果没有回复的信息数超过4条,那么停止发送
if(submitArray.Count>4 && timerSendData.Enabled==true)
{
timerSendData.Enabled=false;
SleepTime=DateTime.Now; //标记开始休眠的时间
}
//
FeebackOk=false;
break;
}
}
}
}
catch(Exception ex)
{
Logs.writeLog(string.Concat(
ex.Source,
"\r\n",
ex.Message,
"\r\n",
ex.StackTrace,
"\r\n",
ex.TargetSite.ToString()),false);
}
}
}
/// <summary>
/// submit 信息
/// </summary>
/// <param name="spId">SP的企业代码</param>
/// <param name="subType">短消息子类型</param>
/// <param name="needReport">是否要求返回状态报告</param>
/// <param name="priority">发送优先级</param>
/// <param name="serviceId">业务类型</param>
/// <param name="feeType">资费类型</param>
/// <param name="feeUserType">计费用户类型</param>
/// <param name="feeCode">每条短消息的信息费</param>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -