📄 main.~pas
字号:
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ScktComp, md5, CMPP_Protocol, cmpp, winsock,
DB, DBTables,IniFiles, ComCtrls;
type
client_record=record
CHandle: integer; //客户端套接字句柄
CSocket:TCustomWinSocket; //客户端套接字
CName:string; //客户端计算机名称
CAddress:string; //客户端计算机IP地址
CUsed: boolean; //客户端联机标志
CSP:string;
IsLogin:boolean; //已经登录
end;
Tclient=array[0..MAXCON] of client_record;
TFrmMain = class( TForm )
Memo1: TMemo;
Panel2: TPanel;
bInit: TButton;
bTerminate: TButton;
bSubmit: TButton;
bActiveTest: TButton;
ServerSocket1: TServerSocket;
DB_main: TDatabase;
Qry_source_data: TQuery;
Qry_waiting_data: TQuery;
Qry_now_data: TQuery;
Timer1: TTimer;
Qry_ivr_tmp_data: TQuery;
Qry_ivr_tmp_data_upt: TQuery;
TNowData: TTable;
QWating: TQuery;
BatchMove1: TBatchMove;
T_waittonow: TTimer;
Qdelewait: TQuery;
UpdateSQL1: TUpdateSQL;
StatusBar1: TStatusBar;
Timer_active_test: TTimer;
Bt_msg_id: TButton;
Label1: TLabel;
Label2: TLabel;
Ed_high: TEdit;
Ed_low: TEdit;
Memo_bit: TMemo;
Label3: TLabel;
Label4: TLabel;
Memo_msg_id: TMemo;
Qry_sp_login: TQuery;
procedure bInitClick( Sender: TObject );
procedure bTerminateClick( Sender: TObject );
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure T_waittonowTimer(Sender: TObject);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure bSubmitClick(Sender: TObject);
procedure bActiveTestClick(Sender: TObject);
procedure Timer_act_test(Sender: TObject);
procedure Bt_msg_idClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
difftime:integer;
Cmpp_Sequence_ID: integer;
aryclient: Tclient; //客户端连接数组
aryclientnum: integer; //客户端连接数
del_now_time:integer;
function Cmpp_lsh: integer;
procedure bCmpp_Init( cSocket: TCustomWinSocket );
procedure bCmpp_DELIVER( cSocket: TCustomWinSocket; sp_code:string;Sequence_Id: integer;cCMPP_DELIVER: tCMPP_DELIVER_tag ;msg_id_high:string;msg_id_low:string;flow_id:longint);
procedure CmppRead_CONNECT( ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag );
// procedure CmppRead_DELIVER(ReadSock: TCustomWinSocket;
// Cmpp_Head: tCMPP_HEAD_tag);
procedure CmppRead_DELIVER_RESP(ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
procedure CmppRead_SUBMIT(ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
procedure CmppRead_TERMINATE(ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
procedure CmppRead_ACTIVE_TEST(ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
procedure CmppRead_ACTIVE_RESP_TEST(ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
procedure CmppRead_BROCKEN(ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
procedure TFrmMain.bInitClick( Sender: TObject );
begin
bCmpp_Init( ServerSocket1.Socket );
end;
procedure TFrmMain.bCmpp_Init( cSocket: TCustomWinSocket );
var
timestr1: TDateTime;
begin
try
ServerSocket1.Active := False;
ServerSocket1.Port := 8890;
ServerSocket1.Active:=true;
timestr1:= now;
while (now-timestr1)< 5/86400 do
begin
if ServerSocket1.Active then break;
application.ProcessMessages;
end;
if not ServerSocket1.Active then
begin
memo1.Lines.Add( 'socket打开失败!'#13#10 );
exit;
end;
memo1.Lines.Add( 'socket打开成功!'+#13#10 );
except on e: exception do
begin
if LogRec.s_time='' then
begin
logrec.s_funcname:='bCmpp_Init';
logrec.s_oper :='打开socket';
logrec.s_result :='失败';
logrec.s_errcode :='0';
logrec.s_err :=e.Message;
logrec.s_time :=FormatDateTime('yyyy-mm-dd hh:mm:ss',now);
WriteLog();
end;
memo1.Lines.Add( 'socket异常:' + e.Message+#13#10 );
exit;
end;
end;
end;
function TFrmMain.Cmpp_lsh: integer;
begin
Result := Cmpp_Sequence_ID;
inc( Cmpp_Sequence_ID );
end;
procedure TFrmMain.bCmpp_DELIVER( cSocket: TCustomWinSocket;sp_code:string; Sequence_Id: integer;cCMPP_DELIVER: tCMPP_DELIVER_tag;msg_id_high:string;msg_id_low:string;flow_id:longint );
type
T_init = packed record
head: TCMPP_HEAD_tag;
body: TCMPP_DELIVER_tag;
end;
var
bInit: T_init;
int1, int2, int3,i: integer;
isdeliver:boolean;
i_handle:integer;
begin
//ISMG对SP发起的连接请求
try
int1 := SizeOf( TCMPP_HEAD_tag );
int2 := SizeOf( TCMPP_DELIVER_tag );
int3 := int1 + int2;
FillChar( bInit.head, int1, 0 );
FillChar( bInit.body, int1, 0 );
bInit.head.Total_Length := htonl( int3 );
bInit.head.Command_ID := htonl( CMPP_DELIVER );
bInit.head.Sequence_ID := Sequence_ID ;
strpcopy(bInit.body.Src_terminal_Id ,cCMPP_DELIVER.Src_terminal_Id );
strpcopy(bInit.body.Dest_terminal_Id ,cCMPP_DELIVER.Dest_terminal_Id );
strpcopy(bInit.body.Stat ,cCMPP_DELIVER.Stat );
strpcopy(bInit.body.Submit_time ,cCMPP_DELIVER.Submit_time );
bInit.body.Msg_Id :=cCMPP_DELIVER.Msg_Id ;
isdeliver:=false;
for i:=0 to aryclientnum do
begin
//取得匹配的客户端
//if aryclient[i].CHandle = handle then
if (aryclient[i].CSP = sp_code) and (aryclient[i].CUsed) and (aryclient[i].IsLogin) then
begin
if aryclient[i].CSocket.SendBuf(bInit, int3 ) <> int3 then
begin
memo1.Lines.Add( '发送DELIVER操作失败'#13#10 );
exit;
end;
i_handle:=aryclient[i].CHandle ;
isdeliver:=true;
end;
end;
//cCMPP_DELIVER.Msg_Id.HighPart
//int4tostr(cCMPP_DELIVER.Msg_Id.LowPart
if not isdeliver then exit;
memo1.Lines.Add( 'DELIVER信息已经发送!'+
'当前时间:'+formatdatetime('yyyy-mm-dd hh:mm:ss',now)+
'msg_id_high:'+msg_id_high +
'msg_id_low:'+msg_id_low +
'flow_id:'+inttostr(flow_id)+
'SP代码:'+sp_code+
'SOCKET句柄:'+inttostr(i_handle)+#13#10 );
except on e: exception do
begin
if LogRec.s_time='' then
begin
logrec.s_funcname:='bCmpp_DELIVER';
logrec.s_oper :='DELIVER发送操作';
logrec.s_result :='失败';
logrec.s_errcode :='0';
logrec.s_err :=e.Message;
logrec.s_time :=FormatDateTime('yyyy-mm-dd hh:mm:ss',now);
WriteLog();
end;
memo1.Lines.Add( '发送DELIVER操作失败,错误信息' + e.Message+#13#10 );
end;
end; //try-except
end;
procedure TFrmMain.bTerminateClick( Sender: TObject );
begin
try
ServerSocket1.Active:=false;
if not ServerSocket1.Active then
memo1.Lines.Add( '关闭成功'+#13#10 )
else
memo1.Lines.Add( '关闭不成功,请再试试'+#13#10 );
except
on e: exception do
memo1.Lines.Add( 'Socket关闭不成功,异常:' + e.Message+#13#10 );
end;
end;
procedure TFrmMain.CmppRead_CONNECT( ReadSock: TCustomWinSocket;
Cmpp_Head: tCMPP_HEAD_tag);
type
T_init = packed record
head: TCMPP_HEAD_tag;
body: TCMPP_CONNECT_RESP_tag;
end;
var
bInit: T_init;
int1, int2, int3: integer;
ReadBufSize,i:integer;
sp_password:string;
Cmpp_CONNECT_tag:TCMPP_CONNECT_tag;
sp_code,sp_addr:string;
sqlstr:string;
begin
//ISMG对SP发起的连接请求
try
int1 := SizeOf( TCMPP_HEAD_tag );
int2 := SizeOf( TCMPP_CONNECT_RESP_tag );
int3 := int1 + int2;
FillChar( bInit.head, int1, 0 );
FillChar( bInit.body, int2, 0 );
bInit.body.Status := 0;
fillchar(Cmpp_CONNECT_tag,sizeof(Cmpp_CONNECT_tag),0);
ReadBufSize := ReadSock.ReceiveBuf( Cmpp_CONNECT_tag, sizeof( Cmpp_CONNECT_tag ) );
if ReadBufSize<> sizeof( Cmpp_CONNECT_tag ) then
begin
memo1.Lines.Add( '接收CONNECT操作失败'+#13#10 );
exit;
end;
sp_code:=trim(copy(Cmpp_CONNECT_tag.Source_Addr ,1,length(Cmpp_CONNECT_tag.Source_Addr )));
sp_password:=trim(copy(Cmpp_CONNECT_tag.Password,1,length(Cmpp_CONNECT_tag.Password)));
sqlstr:='select * from dg_sp_login where '+
'sp_code='+''''+sp_code+''''+
' and sp_pwd='+''''+sp_password+'''';
with qry_sp_login do
begin
Close;
SQL.Clear;
SQL.Add(SqlStr);
Open;
end;
if qry_sp_login.IsEmpty then
begin
bInit.body.Status := 3;
memo1.Lines.Add('认证出错,没有此用户名和密码-用户名:'+sp_code+'密码:'+sp_password+#13#10);
end;
//发送connect_resp消息
bInit.head.Total_Length := htonl( int3 );
bInit.head.Command_ID := htonl( CMPP_CONNECT_RESP );
bInit.head.Sequence_ID := Cmpp_Head.Sequence_ID ;
if ReadSock.SendBuf( bInit, int3 ) <> int3 then
begin
memo1.Lines.Add( '发送CONNECT操作失败'+#13#10 );
exit;
end;
//认证没有通过
if bInit.body.Status = 3 then exit;
//取sp的source_addr
for i:=0 to aryclientnum do
begin
if (aryclient[i].CHandle = ReadSock.SocketHandle) and (aryclient[i].CUsed ) and (not aryclient[i].IsLogin ) then
begin
aryclient[i].CSP := sp_code;
aryclient[i].IsLogin :=true;
sp_addr:=aryclient[i].CAddress;
memo1.Lines.Add( '收到连接信息,并且通过认证'+
'sp代码:'+sp_code+
'sp地址:'+sp_addr+
'连接句柄:'+inttostr(aryclient[i].CHandle)+
'当前时间:'+formatdatetime('yyyy-mm-dd hh:mm:ss',now)+
'正在发送响应信息'+#13#10 );
end;
end;
if LogRec.s_time='' then
begin
logrec.s_funcname:='CmppRead_CONNECT';
logrec.s_oper :='sp:'+sp_code+' 地址:'+sp_addr+'登录成功!';
logrec.s_result :='成功';
logrec.s_errcode :='0';
logrec.s_err :='';
logrec.s_time :=FormatDateTime('yyyy-mm-dd hh:mm:ss',now);
WriteLog();
end;
except on e: exception do
begin
if LogRec.s_time='' then
begin
logrec.s_funcname:='CmppRead_CONNECT';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -