📄 smgpfrm.cpp
字号:
mmlog->SelAttributes->Size=10;
mmlog->SelAttributes->Color=clLime;
break;
}
case 4: //其它消息提示
{
mmlog->SelStart=mmlog->Text.Length()-LogContent.Length()-2;
mmlog->SelLength=LogContent.Length()+2;
mmlog->SelAttributes->Size=10;
mmlog->SelAttributes->Color=clPurple;
}
default:
{
mmlog->SelStart=mmlog->Text.Length()-LogContent.Length()-2;
mmlog->SelLength=LogContent.Length()+2;
mmlog->SelAttributes->Size=10;
mmlog->SelAttributes->Color=clWhite;
break;
}
}
}
//-----------------------------------------------------------------------------
void __fastcall TMainFrm::SaveLog()
{
mmlog->Modified=true;
mmlog->ReadOnly=false;
AnsiString LogDir,LogFile;
LogDir=ExtractFilePath(Application->ExeName)+"\\log\\";
LogFile=LogDir+FormatDateTime("yyyy-mm-dd",Now())+".txt";
if (!DirectoryExists(LogDir))
CreateDir(LogDir);
if (FileExists(LogFile))
{ // 日志文件存在时
TStringList *sl;
sl=new TStringList;
try
{
sl->LoadFromFile(LogFile);
sl->AddStrings(mmlog->Lines);
sl->SaveToFile(LogFile);
mmlog->Clear();
delete sl;
}
catch(...)
{
delete sl;
}
}
else
{
mmlog->Lines->SaveToFile(LogFile);
mmlog->Clear();
}
}
//---------------------------------------------------------------------------
AnsiString __fastcall TMainFrm::Convert2AsII(const char *buffer, int len)
{
AnsiString sRet="", sTmp;
if(len<1)
return "";
for(int i=1; i<=len; i++)
{
sTmp.sprintf("%02x ", (BYTE)buffer[i-1]);
sRet += sTmp;
if(!(i%16))
sRet += "\r\n";
}
sRet += "\r\n";
return sRet;
}
//-----------------------------------------------------------------------------
void __fastcall TMainFrm::UnicodeToGB2312(char *in, int length)
{
int i;
char out[161];
for(i=0;i<length;i=i+2)
{
out[i] = in[i+1];
out[i+1] = in[i];
}
memcpy(in, out, length);
}
//-----------------------------------------------------------------------------
void __fastcall TMainFrm::SmgpClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
int UDLen,BODY_Len,HEAD_Len;
SMGP_HEADER_BUF* SMGP_HEAD;
UDLen=Socket->ReceiveLength();
Log(2,"SmgpClient收到数据包长度:"+IntToStr(UDLen));
while(UDLen>0)
{
SMGP_HEAD=new SMGP_HEADER_BUF;
memset(SMGP_HEAD,0,sizeof(SMGP_HEADER_BUF));
HEAD_Len=Socket->ReceiveBuf(SMGP_HEAD,sizeof(SMGP_HEADER_BUF));
switch(htonl(SMGP_HEAD->CommandID))
{
case SMGP_LOGIN_RESP:
{
SMGP_LOGIN_RESP_BUF* Connect_Resp;
Connect_Resp=new SMGP_LOGIN_RESP_BUF;
memset(Connect_Resp,0,sizeof(SMGP_LOGIN_RESP_BUF));
BODY_Len=Socket->ReceiveBuf(Connect_Resp,sizeof(SMGP_LOGIN_RESP_BUF));
Log(2,"||************************************");
Log(2,"|| 收到网关返回的Login_Resp包");
Log(2,"|| 状态值:"+IntToStr(Connect_Resp->Status));
Log(2,"||************************************");
if(Connect_Resp->Status==0)
{
Smgp_Reci_Times=0;
Smgp_Send_Times=0;
Smgp_Dis_Times=0;
SmgpNetAct->Enabled=true;
OnLine=true;
//
}
delete Connect_Resp;
break;
}
case SMGP_SUBMIT_RESP:
{
Smgp_Reci_Times++;
SMGP_SUBMIT_RESP_BUF* SubmitResp;
SubmitResp=new SMGP_SUBMIT_RESP_BUF;
memset(SubmitResp,0,sizeof(SMGP_SUBMIT_RESP_BUF));
BODY_Len=Socket->ReceiveBuf(SubmitResp,sizeof(SMGP_SUBMIT_RESP_BUF));
Log(2,"||************************************");
Log(2,"|| 收到网关返回的Submit_Resp数据包");
Log(2,"|| MsgID="+String(SubmitResp->MagID));
Log(2,"|| Status="+IntToStr(SubmitResp->Status));
Log(2,"||***********************************");
delete SubmitResp;
break;
}
case SMGP_DELIVER:
{
Smgp_Reci_Times++;
SMGP_DELIVER_BUF* Deliver;
Deliver=new SMGP_DELIVER_BUF;
memset(Deliver,0,sizeof(SMGP_DELIVER_BUF));
BODY_Len=Socket->ReceiveBuf(Deliver,sizeof(SMGP_DELIVER_BUF));
Log(2,"||*************************************");
Log(2,"|| 收到网关发来Deliver数据包 ");
Log(2,"||*************************************");
char Deliver_Message[252];
memset(Deliver_Message,0,252);
memcpy(Deliver_Message,Deliver->MsgContent,Deliver->MsgLength);
if(Deliver->MsgFormat==8)
{
UnicodeToGB2312(Deliver_Message,Deliver->MsgLength);
GetDeliverMessage(Deliver->DestTermID,Deliver->SrcTermID,Deliver->IsReport,Deliver->MsgLength,Deliver->MsgContent,Now());
}
else if(Deliver->MsgFormat==0||Deliver->MsgFormat==15)
{
GetDeliverMessage(Deliver->DestTermID,Deliver->SrcTermID,Deliver->IsReport,Deliver->MsgLength,Deliver->MsgContent,Now());
}
else
{
Log(2,"收到用户上行二进制信息:"+Convert2AsII(Deliver->MsgContent,Deliver->MsgLength));
}
delete Deliver;
break;
}
case SMGP_QUERY_RESP:
{
Smgp_Reci_Times++;
SMGP_QUERY_RESP_BUF* QueryResp;
QueryResp=new SMGP_QUERY_RESP_BUF;
memset(QueryResp,0,sizeof(SMGP_QUERY_RESP_BUF));
BODY_Len=Socket->ReceiveBuf(QueryResp,sizeof(SMGP_QUERY_RESP_BUF));
Log(2,"||**********************************************");
Log(2,"|| 收到网关发的QueryResp数据包 ");
Log(2,"|| Query_Time="+String(QueryResp->Query_Time));
Log(2,"|| Query_Type="+IntToStr(QueryResp->Query_Type));
Log(2,"|| Query_Code="+String(QueryResp->Query_Code));
Log(2,"|| MT_TLMSG ="+IntToStr(QueryResp->MT_TLMsg));
Log(2,"|| MT_TLUser ="+IntToStr(QueryResp->MT_TLusr));
Log(2,"|| MT_Scs ="+IntToStr(QueryResp->MT_Scs));
Log(2,"|| MT_WT ="+IntToStr(QueryResp->MT_WT));
Log(2,"|| MT_FL ="+IntToStr(QueryResp->MT_FL));
Log(2,"|| MO_Scs ="+IntToStr(QueryResp->MO_Scs));
Log(2,"|| MO_WT ="+IntToStr(QueryResp->MO_WT));
Log(2,"|| MO_FL ="+IntToStr(QueryResp->MO_FL));
Log(2,"||**********************************************");
delete QueryResp;
break;
}
case SMGP_ACTIVE_TEST:
{
Smgp_Reci_Times++;
Log(2,"||**************************************");
Log(2,"|| 收到网关发来的NetActive数据包 ");
Log(2,"||**************************************");
SMGP_NetActive(SMGP_HEAD->SequenceID);
BODY_Len=0;
break;
}
case SMGP_ACTIVE_TEST_RESP:
{
Smgp_Reci_Times++;
Log(2,"||**************************************");
Log(2,"|| 收到网关发来的NetActiveResp数据包 ");
Log(2,"||**************************************");
BODY_Len=0;
break;
}
case SMGP_EXIT_RESP:
{
Smgp_Reci_Times++;
Log(2,"||**************************************");
Log(2,"|| 收到网关发来的ExitResp数据包 ");
Log(2,"||**************************************");
BODY_Len=0;
SmgpNetAct->Enabled=false;
OnLine=false;
break;
}
default:
{
Smgp_Reci_Times++;
Log(2,"||*****************************************");
Log(2,"|| 收到错误信息包,正在处理中........");
Log(2,"|| COMMAND_ID:"+IntToStr(ntohl(SMGP_HEAD->CommandID)));
Log(2,"|| 数据包长度:"+IntToStr(ntohl(SMGP_HEAD->Total_Len)) );
Log(2,"||*****************************************");
char ErrorBuf[1024];
memset(ErrorBuf,0,1024);
BODY_Len=Socket->ReceiveBuf(ErrorBuf,1024);
Log(2,Convert2AsII(ErrorBuf, BODY_Len));
break;
}
}
delete SMGP_HEAD;
Log(2,"SmgpClient接收到数据包数据头的长度:"+IntToStr(HEAD_Len));
Log(2,"SmgpClient接收到数据包数据体的长度:"+IntToStr(BODY_Len));
if(HEAD_Len==-1)
{
HEAD_Len=0;
}
if(BODY_Len==-1)
{
BODY_Len=0;
}
UDLen-=(HEAD_Len+BODY_Len);
}
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::GetDeliverMessage(AnsiString DestAddr,AnsiString OrgAddr,
unsigned int IsReport,unsigned int Msg_Length,AnsiString UserData,AnsiString MO_DateTime)
{
switch(IsReport)
{
case 0:
{
Log(3,"||************************************");
Log(3,"|| 收到用户上行信息");
Log(3,"|| 上行端口:"+DestAddr);
Log(3,"|| 上行电话:"+OrgAddr);
Log(3,"|| 上行信息:"+UserData);
Log(3,"|| 上行时间:"+MO_DateTime);
Log(3,"||************************************");
break;
}
case 1:
{
break;
}
}
}
//--------------------------------------------------------------------------
void __fastcall TMainFrm::SmgpNetActTimer(TObject *Sender)
{
if(SmgpClient->Socket->SocketHandle==INVALID_SOCKET)
{
SmgpClient->Close();
OnLine=false;
}
Log(2,"SmgpClient发网络检测的次数:"+IntToStr(Smgp_Send_Times));
Log(2,"SmgpClient收到网关返回包的次数:"+IntToStr(Smgp_Reci_Times));
if(SMGP_ActiveTest()!=0)
{
Log(2,"注意:与网关通讯中断......");
OnLine=false;
}
if(Smgp_Reci_Times==Smgp_Send_Times)
{
Smgp_Dis_Times++;
Log(2,"SmgpClient已有"+IntToStr(Smgp_Dis_Times)+"次没收网关的返回包!");
}
else
{
Smgp_Dis_Times=0;
Smgp_Send_Times=Smgp_Reci_Times;
}
if(Smgp_Dis_Times>=10)
{
Log(2,"SmgpClient已达到10次没收网关的返回包!");
Smgp_Dis_Times=0;
btn_InitInterface->Click();
OnLine=false;
}
if(!OnLine)
{
Sleep(300);
SmgpClient->Open();
btn_Connect->Click();
}
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::btn_InitInterfaceClick(TObject *Sender)
{
SMGP_InitInterface(Smgp_Address,Smgp_Port);
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::btn_ConnectClick(TObject *Sender)
{
SMGP_CONNECT(Smgp_UserName,Smgp_PassWord,Smgp_LoginMode);
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::btn_ExitClick(TObject *Sender)
{
SMGP_Exit();
}
//---------------------------------------------------------------------------
bool __fastcall TMainFrm::SubmitA(AnsiString Spno,AnsiString OrgAddr,AnsiString DestAddr,
AnsiString Order,unsigned char SubType,AnsiString FeeType,AnsiString FeeCode,unsigned int DCS,
unsigned int UDLen,LPCTSTR UserData)
{
AnsiString iRt;
iRt=SMGP_Submit(Spno.c_str(),OrgAddr.c_str(),DestAddr.c_str(),Order.c_str(),
FeeType.c_str(),FeeCode.c_str(),SubType,0,DCS,UDLen,UserData);
if(iRt==0)
{
return true;
}
else
{
return false;
}
}
//-----------------------------------------------------------------------------
void __fastcall TMainFrm::FormCreate(TObject *Sender)
{
TIniFile *ini;
ini = new TIniFile(ExtractFilePath(Application->ExeName)+"\\"+"main.ini");
Smgp_Address=ini->ReadString("ISMG","IP","");
// Smgp_Address="61.144.38.118";
Smgp_UserName=ini->ReadString("ISMG","Account","");
//Smgp_UserName="yitong";
Smgp_PassWord=ini->ReadString("ISMG","Password","");
//Smgp_PassWord="123456";
Smgp_Port=ini->ReadInteger("ISMG","Port",0);
//Smgp_Port=8890;
Smgp_LoginMode=ini->ReadInteger("ISMG","LoginMode",0);
//Smgp_LoginMode=2;
SMGP_InitInterface(Smgp_Address,Smgp_Port);
SmgpClient->Active=true;
SMGP_CONNECT (Smgp_UserName,Smgp_PassWord,Smgp_LoginMode);
SmgpNetAct->Interval=ini->ReadInteger("Timer","SmgpNetTest",15000);
delete ini;
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::FormClose(TObject *Sender, TCloseAction &Action)
{
SaveLog();
}
//---------------------------------------------------------------------------
void __fastcall TMainFrm::ApplicationEvents1Exception(TObject *Sender,
Exception *E)
{
Log(1,E->Message);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -