📄 main.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
#include "About.h"
#include "f_cinfo.h"
#include "f_ceshi.h"
#include "DataDef.h"
#include "ConstDef.h"
#include "f_shezhi.h"
#include "f_moniceshi.h"
//---------------------------------------------------------------------------
#pragma link "Trayicon"
#pragma link "PERFGRAP"
#pragma resource "*.dfm"
TMainForm *MainForm;
int iUserCountToday;
int iMoCountToday;
//---------------------------------------------------------------------------
//替换字符
AnsiString ChangeChar(AnsiString sDate,AnsiString cS,AnsiString cD)
{
for(int i=0;i<sDate.Length();i++)
{
if(sDate.SubString(i+1,1)==cS)
{
sDate=sDate.SubString(1,i)+cD+sDate.SubString(i+2,sDate.Length()-(i+1));
}
}
return sDate;
}
//-----------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent *Owner)
: TForm(Owner)
{
ReCon=false;
UserInfo=new TUserInfo();
YClientSocket=new TYClientSocket();
dtpProtocol=new TdtpProtocol();
YClientSocket->b_Connected=false;
iMoCountToday=0;
iUserCountToday=0;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FileExit1Execute(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
delete UserInfo;
delete YClientSocket;
delete dtpProtocol;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Error(TObject *Sender,
TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
ErrorCode=0;
Socket->Disconnect(Socket->SocketHandle);
Socket->Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormShow(TObject *Sender)
{
YClientSocket->GetClientSocket(ClientSocket1);
InitSystem();
ReadFromIni();
if(n_shezhi->CheckBox2->Checked)
{
Run(true);
}
//Panel6->Align=alClient;
}
//系统启动
void TMainForm::Run(bool bReComSrv)
{
ReadIni(bReComSrv);
if(!UserInfo->OpenDatabase())
{
MessageBox(Handle,"连接数据库失败!\n请仔细检查后重新连接数据库。","错误",MB_OK|MB_ICONINFORMATION);
Label_db->Caption="未连接";
Label_db->Font->Color=clRed;
}
else
{
Label_db->Caption="已连接";
Label_db->Font->Color=clBlue;
}
Timer4->Enabled=true;
ToolButton5->Enabled=false;
N8->Enabled=false;
}
/*///////////////////////////////////////////////////////////////////
函数名: PubWriteToLog
输入参数:日志文件名除日期以外的后半部分 AnsiString
要存入日志文件的一条记录 AnsiString
返回: void
功 能: 将一条数据写入日志文件
*/
bool TMainForm::PubWriteToLog(AnsiString _filename,AnsiString data)
{
AnsiString filename,msg_str;
filename=Date();
filename+=_filename;
if((logfp=fopen(filename.c_str(),"a+"))==NULL)
{return false;}
//data=DateToStr(Date())+" "+TimeToStr(Time())+" "+data;
fprintf(logfp,"%s\n",data.c_str());
fclose(logfp);
return true;
}
/*///////////////////////////////////////////////////////////////////
函数名: InitSystem
返回: void
功 能: 初始化系统
*/
void TMainForm::InitSystem()
{
ToolButton1->Enabled=false;
N1->Enabled=false;
Timer6->Enabled=false;
Timer3->Enabled=false;
}
/*///////////////////////////////////////////////////////////////////
函数名: RecallSystem
返回: void
功 能: 正常化系统
*/
void TMainForm::RecallSystem()
{
ToolButton1->Enabled=true;
N1->Enabled=true;
Timer6->Enabled=true;
Timer3->Enabled=true;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Connect(TObject *Sender,
TCustomWinSocket *Socket)
{
if(!YClientSocket->ConnectToSrv())
{
MessageBox(Handle,"向服务器发送连接数据包时发生错误!","错误",MB_OK|MB_ICONINFORMATION);
return;
}
ShowOneData(9,"STRUCT_CENTER_LOGIN:向服务器发送连接数据包");
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
if(ToolButton5->Enabled==false)
if(YClientSocket->b_Connected)
{
StatusBar1->Panels->Items[2]->Text="与服务器连接正常";
}
else
{
StatusBar1->Panels->Items[2]->Text="登录服务器。。。。。。";
}
StatusBar1->Panels->Items[3]->Text="记录条数:"+IntToStr(ListView2->Items->Count);
Label9->Caption=IntToStr(iUserCountToday);
Label11->Caption=IntToStr(iMoCountToday);
if(TimeToStr(Time())=="0:00:00")
{
iMoCountToday=0;
iUserCountToday=0;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::N2Click(TObject *Sender)
{
if(MessageBox(Handle,"你真的要退出吗?","提问",MB_YESNO|MB_ICONQUESTION)==IDYES)
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::MenuItem3Click(TObject *Sender)
{
AboutBox=new TAboutBox(Application);
AboutBox->ShowModal();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ClientSocket1Read(TObject *Sender,
TCustomWinSocket *Socket)
{
char szReceiveBuf[3000];
char szOneBuf[300];
long lCommandWord;
memset(szReceiveBuf,0,sizeof(szReceiveBuf));
int Len=Socket->ReceiveBuf(szReceiveBuf,3000);
int lp=0;
int ReceMsgLen=0;
while(lp<Len)
{
memset(szOneBuf,0,sizeof(szOneBuf));
//Total_Length
memcpy(&ReceMsgLen,&szReceiveBuf[lp],4);
ReceMsgLen=ntohl(ReceMsgLen);
//Command_ID
memcpy(&lCommandWord,&szReceiveBuf[lp+4],4);
switch (ntohl(lCommandWord))
{
case CTKP_CONNECT_RSP:
LoginRep(&szReceiveBuf[lp]);
break;
case CTKP_LINKTEST_RSP:
YClientSocket->RcvLinkTest();
if(n_shezhi->CheckBox1->Checked)
ShowOneData(8,"CTKP_LINKTEST_RSP");
break;
case CTKP_DELIVERY:
YClientSocket->DeliverRsp(&szReceiveBuf[lp],2);
Deliver(&szReceiveBuf[lp]);
break;
default: break;
}//switch
//移动指针
lp+=ReceMsgLen;
}//while
}
//---------------------------------------------------------------------------
//处理连接服务器应答
void TMainForm::LoginRep(char *buff)
{
STRUCT_CONNECT_RSP s;
memset(&s,0,sizeof(s));
memcpy(&s,buff,sizeof(s));
switch(ntohl(s.sHeader.dwStatus))
{
case E_SUCCESS:
YClientSocket->ConnectToSrvRep();
ShowOneData(8,"CTKP_CONNECT_RSP:成功登录到服务器!");
ReCon=false;
MainForm->RecallSystem();
MainForm->StatusBar1->Panels->Items[1]->Text=("启动时间 "+DateToStr(Date())+" "+TimeToStr(Time()));
Label_srv->Caption="已连接";
Label_srv->Font->Color=clBlue;
break;
case 1:
ShowOneData(8,"CTKP_CONNECT_RSP:登录到服务器失败!返回值:"+StrToInt(ntohl(s.sHeader.dwStatus)));
break;
}
}
//---------------------------------------------------------------------------
//接收服务器下发数据
void TMainForm::Deliver(char *buff)
{
STRUCT_DELIVERY s;
memset(&s,0,sizeof(STRUCT_DELIVERY));
memcpy(&s,buff,sizeof(s));
ShowOneSimDate(7,"STRUCT_DELIVERY:"+AnsiString(s.sCMPP12Delivery.szSrc_terminalID)+" "+AnsiString(s.sCMPP12Delivery.Msg_Content),AnsiString(s.sCMPP12Delivery.szSrc_terminalID));
if(AnsiString(s.sCMPP12Delivery.szSrc_terminalID).Length()<11||AnsiString(s.sCMPP12Delivery.Msg_Content)=="") return;
STRUCT_INBUFF sInBuff;
sInBuff.sim=s.sCMPP12Delivery.szSrc_terminalID;
sInBuff.data=s.sCMPP12Delivery.Msg_Content;
sInBuff.data=sInBuff.data.SubString(3,sInBuff.data.Length()-2);
dtpProtocol->InputBuffer.Length++;
dtpProtocol->InputBuffer[dtpProtocol->InputBuffer.Length-1]=sInBuff;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer3Timer(TObject *Sender)
{
//向服务器发送信息
if(dtpProtocol->OutputBuffer.Length>0)
{
if(YClientSocket->b_Connected)
{
STRUCT_OUTBUFF sOutBuff;
sOutBuff=dtpProtocol->OutputBuffer[dtpProtocol->OutputBuffer.Length-1];
if(sOutBuff.bRealSend)
YClientSocket->SendDataToSrv(sOutBuff.data,sOutBuff.sim,sOutBuff.sourcesim);
ShowOneSimDate(9,"发送:"+sOutBuff.sim+" "+sOutBuff.data+" "+sOutBuff.sourcesim,sOutBuff.sim);
if(sOutBuff.sim==Edit1->Text)
{
int iTmp=StrToInt(Label14->Caption);
iTmp++;
Label14->Caption=IntToStr(iTmp);
}
dtpProtocol->OutputBuffer.Length--;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer6Timer(TObject *Sender)
{
//取得输入缓冲区所有数据,调用相应处理函数分类处理
MainForm->dtpProtocol->GetInputBuffer();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer4Timer(TObject *Sender)
{
//发送测试信息
switch(YClientSocket->SendLinkTest(true))
{
case 0:
if(n_shezhi->CheckBox1->Checked)
ShowOneData(9,"STRUCT_CENTER_LINKTEST:向服务器发送测试消息");
Timer4->Interval=40000;
break;
case 1:
Timer4->Interval=2000;
break;
case 2:
ShowOneData(9,"连接服务器...");
Timer4->Interval=2000;
InitSystem();
Label_srv->Caption="未连接";
Label_srv->Font->Color=clRed;
break;
}
}
/*///////////////////////////////////////////////////////////////////
函数名: ShowOneData
输入参数:要显示的图标编号 int
要显示的内容 AnsiString
返回: void
功 能: 在列表框中显示一条系统信息
*/
void TMainForm::ShowOneData(int bNum,AnsiString data)
{
TListItem *ListItem;
ListItem=ListView2->Items->Add();
ListItem->Caption =DateToStr(Date())+" "+TimeToStr(Time());
ListItem->ImageIndex=bNum;
switch(bNum)
{
case 7:
ListItem->SubItems->Add("移动终端");
break;
case 8:
ListItem->SubItems->Add("服务器");
break;
case 9:
ListItem->SubItems->Add("本地");
break;
}
ListItem->SubItems->Add(data);
if(CheckBox1->Checked)
{
ListItem->MakeVisible(false);
ListView2->Perform(LVM_SCROLL,0,10);
}
if(ListView2->Items->Count>200)
{
for(int i=ListView2->Items->Count-1;i>=0;i--)
{
PubWriteToLog(".txt",ListView2->Items->Item[i]->Caption+ListView2->Items->Item[i]->SubItems->Strings[1]);
}
ListView2->Items->Clear();
}
}
void TMainForm::ShowOneData(TListView* ListView,int bNum,AnsiString data)
{
TListItem *ListItem;
ListItem=ListView->Items->Add();
ListItem->Caption =DateToStr(Date())+" "+TimeToStr(Time());
ListItem->ImageIndex=bNum;
switch(bNum)
{
case 7:
ListItem->SubItems->Add("移动终端");
break;
case 8:
ListItem->SubItems->Add("服务器");
break;
case 9:
ListItem->SubItems->Add("本地");
break;
}
ListItem->SubItems->Add(data);
ListItem->MakeVisible(false);
ListView->Perform(LVM_SCROLL,0,10);
if(ListView->Items->Count>200)
{
ListView->Items->Clear();
}
}
//显示一条含手机的系统信息
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -