思路是这样的,当TIdTCPServer接受到数据包时,分析其内容,然后根据不同的内容对后台数据库进行操作,与数据库的连接采用ADO。下面是一段自己写的代码,由于涉及到多线程的问题,请各位大侠看看能否避免线程之间的冲突,存在什么缺陷。
function TFrmServer.UserLogin(UName, PassW: string): boolean;
begin
UILock.Acquire;
try
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('...'); //此处写SQL语句
Open;
Result := RecordCount > 0;
end;
finally
UILock.Release;
end;
end;procedure TFrmServer.TCPServerExecute(AThread: TIdPeerThread);
var
CommBlock: TCommBlock;
ActClient: PClient;
Comm, GName, UName, Msg: string;
BackBlock: TBackBlock;
Idx: integer;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer(CommBlock, SizeOf(CommBlock));
ActClient := PClient(AThread.Data); if CommBlock.Check = '001' then
begin
BackBlock.Check := '001';
if UserLogin(CommBlock.UserName, CommBlock.PassW) then
BackBlock.Msg1 := 'Ok'
else BackBlock.Msg1 := ''; AThread.Connection.WriteBuffer(BackBlock, SizeOf (BackBlock), True);
end;
end;
end;
function TFrmServer.UserLogin(UName, PassW: string): boolean;
begin
UILock.Acquire;
try
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('...'); //此处写SQL语句
Open;
Result := RecordCount > 0;
end;
finally
UILock.Release;
end;
end;procedure TFrmServer.TCPServerExecute(AThread: TIdPeerThread);
var
CommBlock: TCommBlock;
ActClient: PClient;
Comm, GName, UName, Msg: string;
BackBlock: TBackBlock;
Idx: integer;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
AThread.Connection.ReadBuffer(CommBlock, SizeOf(CommBlock));
ActClient := PClient(AThread.Data); if CommBlock.Check = '001' then
begin
BackBlock.Check := '001';
if UserLogin(CommBlock.UserName, CommBlock.PassW) then
BackBlock.Msg1 := 'Ok'
else BackBlock.Msg1 := ''; AThread.Connection.WriteBuffer(BackBlock, SizeOf (BackBlock), True);
end;
end;
end;
能介绍一下 工作线程池吗?有简单的例子吗?谢谢