📄 mainunit.pas
字号:
PK_Head.Total_Length := WinSock.htonl(SizeOf(PK_Head) + SizeOf(ACMPP_SUBMIT));
PK_Head.Command_ID := WinSock.htonl(CMPP_Protocol2.CMPP_SUBMIT);
TMPSequence_ID := self.CMPP2_GetLSH();
PK_Head.Sequence_ID := WinSock.htonl(TMPSequence_ID);
try
MemStream := TMemoryStream.Create;
MemStream.WriteBuffer(PK_Head,SizeOf(PK_Head));
MemStream.WriteBuffer(ACMPP_SUBMIT,SizeOf(ACMPP_SUBMIT));
MemStream.Seek( 0, soFromBeginning );
//My_GetBuf(MemStream,'D:\Log\' + IntToStr(PK_Head.Sequence_ID) + '.txt');
if not self.CMPP2Socket.Active then
begin
Result := false;
Exit;
end;
try
if self.CMPP2Socket.Socket.SendBuf(MemStream.Memory^,MemStream.Size) = MemStream.Size then
begin
Result := true;
end else
begin
Result := false;
end;
try
Query6.Close ;
Query6.SQL.Text:= 'update sms_submit set SENDSTATE=1 , msgid=' +''''+'A'+IntToStr(TMPSequence_ID)+''''+ ' where submitid=' +''''+ SubmitID+'''';
Query6.ExecSQL ;
// self.My_WriteInfo('update OK');
except
// self.My_WriteInfo('update 失败');
end;
except
Result := false;
end;
finally
FreeAndNil(MemStream);
end;
end;
function TMainForm.CMPP2_DELIVER():boolean;
begin
if not self.CMPP2Socket.Active then
begin
Result := false;
Exit;
end;
Result := true;
end;
function TMainForm.CMPP2_ACTIVE_TEST():boolean;
type
TPK_ACTIVE_TEST = packed record
Head: TCMPP_HEAD;
Body: TCMPP_ACTIVE_TEST;
end;
var
PK_ACTIVE_TEST : TPK_ACTIVE_TEST;
begin
FillChar(PK_ACTIVE_TEST,SizeOf(PK_ACTIVE_TEST),0);
PK_ACTIVE_TEST.Head.Total_Length := WinSock.htonl(SizeOf(PK_ACTIVE_TEST));
PK_ACTIVE_TEST.Head.Command_ID := WinSock.htonl(CMPP_Protocol2.CMPP_ACTIVE_TEST);
PK_ACTIVE_TEST.Head.Sequence_ID := WinSock.htonl(self.CMPP2_GetLSH());
if not self.CMPP2Socket.Active then
begin
if not self.My_OpenSocket() then
begin
Result := false;
Exit;
end;
end;
try
if self.CMPP2Socket.Socket.SendBuf(PK_ACTIVE_TEST,SizeOf(PK_ACTIVE_TEST)) = SizeOf(PK_ACTIVE_TEST) then
begin
self.My_WriteInfo('进行链路测试.........');
Result := true;
end else
begin
self.My_WriteInfo('链路测试失败.');
Result := false;
end;
except
Result := false;
end;
end;
function TMainForm.CMPP2_GetLSH(): LongWord;
begin
if ( My_Sequence_Number < 1 ) or ( My_Sequence_Number > 4294967295 ) then
begin
My_Sequence_Number := 2;
end;
Result := My_Sequence_Number;
inc( My_Sequence_Number );
end;
function TMainForm.My_OpenSocket():boolean;
var
IniFile : TIniFile;
TmpTime : TDateTime;
begin
try
try
self.CMPP2Socket.Close;
IniFile := TIniFile.Create(self.My_GetIniConfigFile);
self.CMPP2Socket.Address := IniFile.ReadString('Option','IP','127.0.0.1');
self.CMPP2Socket.Port := IniFile.ReadInteger('Option','Port',7890);
self.CMPP2Socket.Open;
TmpTime := Now( );
while true do
begin
Application.ProcessMessages;
if ( Now - TmpTime ) > 5 / 86400 then break;
if CMPP2Socket.Active then break;
end;
self.My_WriteInfo('SOCKET环境初使化成功...');
if not self.CMPP2_CONNECT() then Abort;//////
Result := true;
except
self.My_WriteInfo('SOCKET连接失败,请检查网络及配置文件!');
Result := false;
end;
finally
FreeAndNil(IniFile);
end;
end;
procedure TMainForm.My_CloseSocket();
begin
try
self.CMPP2Socket.Close;
self.My_WriteInfo('服务器断开连接成功.');
except
self.My_WriteInfo('服务器断开连接失败.');
end;
end;
procedure TMainForm.My_LoadParameters();
var
IniFile : TIniFile;
begin
try
IniFile := TIniFile.Create(self.My_GetIniConfigFile);
My_DelayCount := iniFile.ReadInteger('Option','DelayTime',20);
My_SPCode := trim(iniFile.ReadString('Option','SPCode',''));
My_SPCorpID := trim(iniFile.ReadString('Option','SPCorpID',''));
My_Passwd := trim(iniFile.ReadString('Option','Passwd',''));
My_MaxRecCount := iniFile.ReadInteger('Option','MaxRecCount',500);
My_ClearInfoLineCount := iniFile.ReadInteger('Option','ClearInfoLineCount',1000);
My_CanWriteLog := iniFile.ReadString('Option','CanWriteLog','0');
My_CanPackBadWord := iniFile.ReadString('Option','CanPackBadWord','0');
self.LoopTimer.Interval := iniFile.ReadInteger('Option','LoopTime',1000);
self.CheckTimer.Interval := iniFile.ReadInteger('Option','CheckTime',60000);
finally
FreeAndNil(IniFile);
end;
end;
procedure TMainForm.My_DelayTime();
var
IStart:LongInt;
begin
IStart := GetTickCount();
while ((LongInt(GetTickCount()) - IStart) <= My_DelayCount) do
begin
Application.ProcessMessages; //100为毫秒
end;
end;
function TMainForm.My_PackBadWord(Msg:String):String;
var
i :integer;
begin
for i:=0 to BadWordList.Count - 1 do
begin
Msg := StringReplace(Msg,BadWordList[i],'#',[rfReplaceAll]);
end;
Result := Msg;
end;
procedure TMainForm.My_StartServer();
begin
self.LoopTimer.Enabled := false;
self.CheckTimer.Enabled := false;
if self.CMPP2Socket.Active then
begin
self.CMPP2_TERMINATE;
self.My_CloseSocket;
end;
if self.ADOConn.Connected then self.ADOConn.Close;
My_LoginFlag := false;
self.InfoMemo.Clear;
if not self.My_ConnectToDB() then Abort;
if not self.My_OpenSocket() then Abort;
if not self.CMPP2_CONNECT() then Abort;
self.CheckTimer.Enabled := true;
self.LoopTimer.Enabled := true;
My_ReceiveCount := 0 ;
My_SendCount := 0 ;
end;
function TMainForm.My_ConnectToDB():boolean;
var
ini :TIniFile;
begin
try
ini := TIniFile.Create(self.My_GetIniConfigFile);
try
self.ADOConn.Close;
self.ADOConn.ConnectionString := ini.ReadString('Option','AdoConn','');
self.ADOConn.Open;
self.MainStatusBar.Panels.Items[0].Text := '数据库:已连接';
self.My_WriteInfo('数据库连接成功.');
//init DB StoredProc
Result := true;
except
self.MainStatusBar.Panels.Items[0].Text := '数据库:连接失败';
self.My_WriteInfo('数据库连接失败.');
Result := false;
end;
finally
FreeAndNil(Ini);
end;
end;
function TMainForm.My_GetIniConfigFile():String;
begin
Result := ExtractFilePath(Application.ExeName) + 'CMPP2.ini';
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Application.Title := 'A:中国移动SP短信收发程序(CMPP2.0) V2.1 0119';
self.Caption := Application.Title;
self.My_LoadParameters;
My_Sequence_Number := 1;
BadWordList := TStringList.Create;
end;
procedure TMainForm.Button_StartClick(Sender: TObject);
begin
self.My_StartServer;
end;
procedure TMainForm.Button_ShutClick(Sender: TObject);
begin
self.My_LoginFlag := false;
self.LoopTimer.Enabled := false;
self.CheckTimer.Enabled := false;
self.CMPP2_TERMINATE;
self.My_CloseSocket;
self.ADOConn.Close;
end;
procedure TMainForm.CMPP2SocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
self.MainStatusBar.Panels.Items[1].Text := '网络:已连接';
self.MainStatusBar.Update;
end;
procedure TMainForm.CMPP2SocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
self.MainStatusBar.Panels.Items[1].Text := '网络:正在连接...';
self.MainStatusBar.Update;
end;
procedure TMainForm.CMPP2SocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
self.MainStatusBar.Panels.Items[1].Text := '网络:已断开';
self.MainStatusBar.Update;
end;
procedure TMainForm.CMPP2SocketError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
begin
ErrorCode := -1;
end;
function TMainForm.ConvertMsgID(Msg_ID: PChar):string;
var
High4, Low4: Cardinal;
Month, Day, Hour, Minute, Second, GateWayID, SID: Word;
begin
CopyMemory(@High4, @Msg_ID[0], 4);
CopyMemory(@Low4, @Msg_ID[4], 4);
High4 := ntohl(High4);
Low4 := ntohl(Low4);
Month := (High4 shr 28) and $0F;
Day := (High4 shr 23) and $1F;
Hour := (High4 shr 18) and $1F;
Minute := (High4 shr 12) and $3F;
Second := (High4 shr 6) and $3F;
SID := Low4 and $FF;
GateWayID := (Low4 shr 16) and $FF + (((High4) and $3F) * 65536);
Result := Format('%.2d.%.2d.%.2d.%.2d.%.2d.%d.%d', [Month, Day, Hour, Minute,
Second, GateWayID, SID]);
end;
procedure TMainForm.CMPP2SocketRead(Sender: TObject;Socket: TCustomWinSocket);
type
TPK_TCMPP_DELIVER_RESP = packed record
Head: TCMPP_HEAD;
Body : TCMPP_DELIVER_RESP;
end;
TPK_TCMPP_ACTIVE_TEST_RESP = packed record
Head: TCMPP_HEAD;
Body : TCMPP_ACTIVE_TEST_RESP;
end;
const
LinkID_LENGTH = 20;
var
PK_Head : TCMPP_HEAD;
PK_CMPP_CONNECT_RESP : TCMPP_CONNECT_RESP;
PK_CMPP_SUBMIT_RESP : TCMPP_SUBMIT_RESP;
PK_TCMPP_ACTIVE_TEST_RESP : TPK_TCMPP_ACTIVE_TEST_RESP;
PK_CMPP_DELIVER_HEAD : TCMPP_DELIVER_HEAD;
PK_CMPP_DELIVER_BODY : TCMPP_DELIVER;
PK_CMPP_REPORT_BODY : TCMPP_REPORT;
PK_TCMPP_DELIVER_RESP : TPK_TCMPP_DELIVER_RESP;
MsgLen : integer;
ShortMessage :String;
WideShortMessage :WideString;
ReceiveShortMessage : TReceiveShortMessage;
ReceiveReport : TReceiveReport;
ssql:string;
ssdk:string;
begin
FillChar(PK_Head,SizeOf(PK_Head),0);
if Socket.ReceiveBuf(PK_Head,SizeOf(PK_Head)) <> SizeOf(PK_Head) then
begin
//The Package is bad by Receive
Exit;
end;
PK_Head.Total_Length := WinSock.ntohl(PK_Head.Total_Length);
PK_Head.Command_ID := WinSock.ntohl(PK_Head.Command_ID);
PK_Head.Sequence_ID := WinSock.ntohl(PK_Head.Sequence_ID);
//----------------------------------------------------------------------------
if PK_Head.Command_ID = CMPP_Protocol2.CMPP_CONNECT_RESP then
begin
FillChar(PK_CMPP_CONNECT_RESP,SizeOf(PK_CMPP_CONNECT_RESP),0);
if Socket.ReceiveBuf(PK_CMPP_CONNECT_RESP,SizeOf(PK_CMPP_CONNECT_RESP)) = SizeOf(PK_CMPP_CONNECT_RESP) then
begin
PK_CMPP_CONNECT_RESP.Status := WinSock.ntohl(PK_CMPP_CONNECT_RESP.Status);
if (PK_CMPP_CONNECT_RESP.Status = 0) then
begin
self.My_WriteInfo('网关回应:登陆成功.');
BadWordList.Clear;
BadWordList.LoadFromFile(ExtractFilePath(Application.ExeName)+'BadWord.DB');
self.My_WriteInfo('非法字过滤引荐载入成功...');
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -