📄 submitunit.pas
字号:
upQueryData: TADOQuery;
StrInfo: string;
// SequenceNumber1, SequenceNumber2, SequenceNumber3: integer;
msgid2: string;
begin
Result := false;
if not self.SGIPSocket.Active then
begin
Exit;
end;
DateTimeToString(StrTimeStamp, 'mmddhhnnss', Now());
FillChar(SGIPSubmit, SizeOf(SGIPSubmit), 0);
FillChar(SGIPSubmit_Resp, SizeOf(SGIPSubmit_Resp), 0);
with SGIPSubmit.Head do
begin
MessageLength := WinSock.htonl(SizeOf(SGIPSubmit));
CommandId := WinSock.htonl(SGIP12_Protocol.SGIP_SUBMIT);
SNumber1 := WinSock.htonl(StrToInt64(self.F_NodId)); //
SNumber2 := WinSock.htonl(StrToInt64(StrTimeStamp)); //
SNumber3 := WinSock.htonl(self.SGIP12_GetLSH);
end;
SequenceNumber1 := SGIPSubmit.Head.SNumber1;
SequenceNumber2 := SGIPSubmit.Head.SNumber2;
SequenceNumber3 := SGIPSubmit.Head.SNumber3;
if self.F_CanPackBadWord = 1 then
begin
SubmitRecord.F_MessageContent := self.PackBadWord(SubmitRecord.F_MessageContent);
end;
StrPCopy(SGIPSubmit.Submit.SPNumber, SubmitRecord.F_SPNumber);
StrPCopy(SGIPSubmit.Submit.ChargeNumber, SubmitRecord.F_ChargeNumber);
SGIPSubmit.Submit.UserCount := 1;
StrPCopy(SGIPSubmit.Submit.UserNumber, SubmitRecord.F_UserNumber);
StrPCopy(SGIPSubmit.Submit.CorpID, self.F_SPCorpID);
StrPCopy(SGIPSubmit.Submit.ServiceType, SubmitRecord.F_ServiceType);
SGIPSubmit.Submit.FeeType := SubmitRecord.F_FeeType;
StrPCopy(SGIPSubmit.Submit.FeeValue, SubmitRecord.F_FeeValue);
StrPCopy(SGIPSubmit.Submit.GivenValue, SubmitRecord.F_GivenValue);
SGIPSubmit.Submit.AgentFlag := SubmitRecord.F_AgentFlag;
SGIPSubmit.Submit.MOrelatetoMTFlag := SubmitRecord.F_MOrelatetoMTFlag;
SGIPSubmit.Submit.Priority := SubmitRecord.F_Priority;
StrPCopy(SGIPSubmit.Submit.ExpireTime, SubmitRecord.F_ExpireTime);
StrPCopy(SGIPSubmit.Submit.ScheduleTime, SubmitRecord.F_ScheduleTime);
SGIPSubmit.Submit.ReportFlag := SubmitRecord.F_ReportFlag;
SGIPSubmit.Submit.TP_pid := SubmitRecord.F_TP_pid;
SGIPSubmit.Submit.TP_udhi := SubmitRecord.F_TP_udhi;
SGIPSubmit.Submit.MessageCoding := SubmitRecord.F_MessageCoding;
SGIPSubmit.Submit.MessageType := SubmitRecord.F_MessageType;
StrPCopy(SGIPSubmit.Submit.MessageContent, SubmitRecord.F_MessageContent);
StrPCopy(SGIPSubmit.Submit.Reserve, SubmitRecord.F_LinkID);
SGIPSubmit.Submit.MessageLength := WinSock.htonl(Length(SGIPSubmit.Submit.MessageContent)); ;
try
try
ScktStream := TWinSocketStream.Create(self.SGIPSocket.Socket, 3000);
ScktStream.Write(SGIPSubmit, SizeOf(SGIPSubmit));
if ScktStream.WaitForData(5000) then
begin
ScktStream.Read(SGIPSubmit_Resp, sizeOf(SGIPSubmit_Resp));
if SGIPSubmit_Resp.Submit_Resp.Result = 0 then
begin
StrInfo := '' + SubmitRecord.F_UserNumber + '、';
StrInfo := StrInfo + '' + SubmitRecord.F_SPNumber + '、';
StrInfo := StrInfo + '' + string(SubmitRecord.F_ServiceType) + '、';
StrInfo := StrInfo + '' + IntToStr(SGIPSubmit_Resp.Submit_Resp.Result) + ' .';
self.WriteInfo(StrInfo);
F_SendCount := F_SendCount + 1;
self.MainStatusBar.Panels.Items[2].Text := '发送短信数 ' + IntToStr(F_SendCount) + ' 条';
try
begin
upQueryData := TADOQuery.Create(self);
upQueryData.Connection := self.ADOConn;
with StoredProc.Parameters do
begin
msgid2 := inttostr(SequenceNumber1) + inttostr(SequenceNumber2) + inttostr(SequenceNumber3);
while pos('-', msgid2) > 0 do
begin
System.delete(msgid2, Pos('-', msgid2), Length('-'));
end;
try
upQueryData.Close;
upQueryData.SQL.Text := 'Insert into sms_report(ReportType,Submit_Time,Done_Time,RawState,ErrorCode,State,SubmitID,MsgID,UserNumber) values(0,' + '''' + '''' + ',' + '''' + '''' + ',0,' + '''' + '''' + ',3,' + inttostr(SubmitRecord.F_RecordID)+ ',' + msgid2 + ',' + SubmitRecord.F_UserNumber + ')';
upQueryData.ExecSQL;
except
end;
end;
end;
finally
FreeAndNil(upQueryData);
end;
Result := true; //////////////////成功
end else
begin
StrInfo := '' + SubmitRecord.F_UserNumber;
StrInfo := StrInfo + '' + SubmitRecord.F_SPNumber + '、';
StrInfo := StrInfo + '' + SubmitRecord.F_ServiceType + '、';
StrInfo := StrInfo + 'ErrorCode:' + IntToStr(SGIPSubmit_Resp.Submit_Resp.Result) + '(' + SGIP12_Protocol.GetSGIP12ErrorInfo(SGIPSubmit_Resp.Submit_Resp.Result) + ')';
self.WriteInfo(StrInfo);
F_ErrorCount := F_ErrorCount + 1;
self.MainStatusBar.Panels.Items[3].Text := '出错短信数 ' + IntToStr(F_SendCount) + ' 条';
try
QueryError := TADOQuery.Create(self);
QueryError.Connection := self.ADOConn;
with StoredProc.Parameters do
begin
msgid2 := inttostr(SequenceNumber1) + inttostr(SequenceNumber2) + inttostr(SequenceNumber3);
while pos('-', msgid2) > 0 do
begin
System.delete(msgid2, Pos('-', msgid2), Length('-'));
end;
QueryError.Close;
QueryError.SQL.Text :='Insert into sms_report(ReportType,Submit_Time,Done_Time,RawState,ErrorCode,State,SubmitID,MsgID,UserNumber) values(0,' + '''' + '''' + ',' + '''' + '''' + ',0,' + '''' + '''' + ',b,' + inttostr(SubmitRecord.F_RecordID) + ',' + msgid2 + ',' + SubmitRecord.F_UserNumber + ')';
QueryError.ExecSQL;
end;
Result := true;
finally
FreeAndNil(QueryError);
end;
end;
end else
begin
self.WriteInfo('发信失败,指令发送等待超时,请查检网络状况.');
Result := false;
end;
except
on E: ESocketError do
begin
self.WriteInfo(E.Message);
Result := false;
end;
end;
finally
FreeAndNil(ScktStream);
end;
end;
function TMainForm.SGIP12_GetLSH(): LongWord;
begin
if (F_Sequence_Number < 1) or (F_Sequence_Number > 4294967295) then
begin
F_Sequence_Number := 2;
end;
Result := F_Sequence_Number;
inc(F_Sequence_Number);
end;
function TMainForm.OpenSocket(): boolean;
var
IniFile: TIniFile;
TmpTime: TDateTime;
begin
try
try
self.SGIPSocket.Close;
IniFile := TIniFile.Create(self.GetIniConfigFile);
self.SGIPSocket.Address := IniFile.ReadString('Option', 'IP', '127.0.0.1');
self.SGIPSocket.Port := IniFile.ReadInteger('Option', 'ClientPORT', 8001);
self.SGIPSocket.Open;
TmpTime := Now();
while true do
begin
Application.ProcessMessages;
if SGIPSocket.Active then break;
if (Now - TmpTime) > 5 / 86400 then break;
end;
self.WriteInfo('SOCKET环境初使化成功...');
Result := true;
except
self.WriteInfo('SOCKET连接失败,请检查网络及配置文件!');
Result := false;
end;
finally
FreeAndNil(IniFile);
end;
end;
procedure TMainForm.CloseSocket();
begin
try
self.SGIPSocket.Close;
self.WriteInfo('服务器断开连接成功.');
except
self.WriteInfo('服务器断开连接失败.');
end;
end;
procedure TMainForm.LoadParameters();
var
IniFile: TIniFile;
begin
try
IniFile := TIniFile.Create(self.GetIniConfigFile);
F_DelayTime := iniFile.ReadInteger('Option', 'DelayTime', 20);
F_SPCode := trim(iniFile.ReadString('Option', 'SPCode', ''));
F_NodId := trim(iniFile.ReadString('Option', 'NodId', ''));
F_SPCorpID := trim(iniFile.ReadString('Option', 'SPCorpID', ''));
F_User := trim(iniFile.ReadString('Option', 'User', ''));
F_Passwd := trim(iniFile.ReadString('Option', 'Passwd', ''));
F_MaxRecCount := iniFile.ReadInteger('Option', 'MaxRecCount', 500);
F_BaseFetchFlag := iniFile.ReadString('Option', 'BaseFetchFlag', '1');
F_ClearInfoLineCount := iniFile.ReadInteger('Option', 'ClearInfoLineCount', 1000);
F_CanWriteLog := iniFile.ReadInteger('Option', 'CanWriteLog', 0);
F_CanPackBadWord := iniFile.ReadInteger('Option', 'CanPackBadWord', 0);
self.LoopTimer.Interval := iniFile.ReadInteger('Option', 'LoopTime', 1000);
finally
FreeAndNil(IniFile);
end;
if self.F_CanPackBadWord = 1 then
begin
BadWordList.Clear;
BadWordList.LoadFromFile(ExtractFilePath(Application.ExeName) + 'BadWord.DB');
end;
end;
procedure TMainForm.DelayTime();
var
IStart: LongInt;
begin
IStart := GetTickCount();
while ((LongInt(GetTickCount()) - IStart) <= self.F_DelayTime) do
begin
Application.ProcessMessages; //100为毫秒
end;
end;
function TMainForm.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;
function TMainForm.ConnectToDB(): boolean;
var
ini: TIniFile;
begin
try
ini := TIniFile.Create(self.GetIniConfigFile);
try
self.ADOConn.Close;
self.ADOConn.ConnectionString := ini.ReadString('Option', 'AdoConn', '');
self.ADOConn.Open;
self.MainStatusBar.Panels.Items[0].Text := '数据库:已连接';
self.WriteInfo('数据库连接成功...');
//init DB StoredProc
Result := true;
except
self.MainStatusBar.Panels.Items[0].Text := '数据库:连接失败';
self.WriteInfo('数据库连接失败.');
Result := false;
end;
finally
FreeAndNil(Ini);
end;
end;
function TMainForm.GetIniConfigFile(): string;
begin
Result := ExtractFilePath(Application.ExeName) + 'SGIP12.ini';
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
BadWordList := TStringList.Create;
Application.Title := '杭州亚大通讯--联通发送通讯平台 V2.1 0119 注册版';
self.Caption := Application.Title;
self.LoadParameters;
F_Sequence_Number := 1;
F_SendCount := 0;
F_ErrorCount := 0;
end;
procedure TMainForm.Button_StartClick(Sender: TObject);
begin
self.LoopTimer.Enabled := false;
if self.SGIPSocket.Active then
begin
self.SGIP12_TERMINATE;
self.CloseSocket;
end;
if self.ADOConn.Connected then self.ADOConn.Close;
self.InfoMemo.Clear;
if not self.ConnectToDB() then Abort;
if self.F_CanPackBadWord = 1 then
self.WriteInfo('非法字符过滤引荐载入成功...');
self.WriteInfo('网络初使化成功,等待发信...');
self.LoopTimer.Enabled := true;
end;
procedure TMainForm.Button_ShutClick(Sender: TObject);
begin
self.LoopTimer.Enabled := false;
if self.SGIPSocket.Active then
begin
self.SGIP12_TERMINATE;
self.CloseSocket;
end;
self.ADOConn.Close;
end;
procedure TMainForm.SGIPSocketConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
self.MainStatusBar.Panels.Items[1].Text := '网络:已连接';
self.MainStatusBar.Update;
end;
procedure TMainForm.SGIPSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
self.MainStatusBar.Panels.Items[1].Text := '网络:正在连接...';
self.MainStatusBar.Update;
end;
procedure TMainForm.SGIPSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
self.MainStatusBar.Panels.Items[1].Text := '网络:已断开';
self.MainStatusBar.Update;
end;
procedure TMainForm.SGIPSocketError(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.SpeedButton3Click(Sender: TObject);
begin
WinExec(PChar('notepad.exe ' + self.GetIniConfigFile), SW_SHOW);
end;
procedure TMainForm.InfoMemoChange(Sender: TObject);
begin
if self.InfoMemo.Lines.Count > F_ClearInfoLineCount then
begin
self.InfoMemo.Clear;
end;
end;
procedure TMainForm.ADOConnAfterConnect(Sender: TObject);
begin
self.MainStatusBar.Panels.Items[0].Text := '数据库:已连接';
self.MainStatusBar.Update;
end;
procedure TMainForm.ADOConnAfterDisconnect(Sender: TObject);
begin
self.MainStatusBar.Panels.Items[0].Text := '数据库:已断开';
self.MainStatusBar.Update;
end;
procedure TMainForm.ADOConnBeforeConnect(Sender: TObject);
begin
self.MainStatusBar.Panels.Items[0].Text := '数据库:正在连接,请稍候...';
self.MainStatusBar.Update;
end;
procedure TMainForm.ADOConnBeforeDisconnect(Sender: TObject);
begin
self.MainStatusBar.Panels.Items[0].Text := '数据库:正在断开,请稍候...';
self.MainStatusBar.Update;
end;
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
self.Button_Shut.Click;
end;
procedure TMainForm.ApplicationEvents_XException(Sender: TObject;
E: Exception);
begin
if E is ESocketError then
begin
ApplicationEvents_X.CancelDispatch;
end;
end;
procedure TMainForm.LoopTimerTimer(Sender: TObject);
begin
// if (YearOf(Now) > 2005) or (MonthOf(Now) > 11) then
// begin
// Application.MessageBox('杭州亚大通讯--联通发送通讯平台 V1.2 Time END', PChar(Application.Title), MB_OK + MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
// end;
try
self.LoopTimer.Enabled := false;
self.SGIP12_SUBMITEx_BEGINWORK();
finally
self.LoopTimer.Enabled := true;
end;
end;
procedure TMainForm.SpeedButton4Click(Sender: TObject);
begin
Application.MessageBox('杭州亚大通讯--联通发送通讯平台 V2.1 0119', PChar(Application.Title), MB_OK + MB_ICONASTERISK + MB_DEFBUTTON1 + MB_APPLMODAL);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -