对不起,前几日有事,原码如下:
type
THBServerThread = class(TServerClientThread)
private
Dm: TDM1;
procedure HandleRequest(const pRecvD: PChar);
public
constructor Create(CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
procedure Destroy; override;
procedure ClientExecute; override;
end;
var
Guard: Integer;
Numbers: Integer;
{ Thread safe increment of Numbers to guarantee the result is unique }
function GetUniqueNumber: Integer;
asm
@@1: MOV EDX,1
XCHG Guard,EDX
OR EDX,EDX
JNZ @@2
MOV EAX,Numbers
INC EAX
MOV Numbers,EAX
MOV Guard,EDX
RET
@@2: PUSH 0
CALL Sleep
JMP @@1
end;
constructor THBServerThread.Create(CreateSuspended: Boolean; ASocket: TServerClientWinSocket);
begin
inherited;
KeepInCache:=False; //本想每次线程执行后释放,以便debug,但不成
end;
procedure Destroy;
begin
dm.free;
inherited;
end;
procedure THBServerThread.ClientExecute;
var
UniqueNumber: Integer;
Data: array[0..63] of Char; //每次Read读出的数据
Temp: array[0..127] of Char;
ifRecv,Loop: boolean;
SocketStream: TWinSocketStream;
begin
{ Ensure the Query has a unique session and database. A unique
session is required for each thread. Since databases are
session specific it must be unique as well }
UniqueNumber := GetUniqueNumber;
Session.SessionName := Format('%s%x', [Session.Name, UniqueNumber]);
Dm:=TDm1.Create(Application);
Dm.DB.SessionName:=Session.SessionName;
Dm.DB.DatabaseName := Format('HbWeb%x', [UniqueNumber]);
Dm.DB.Connected:=True;
Dm.Qry.SessionName:=Dm.DB.SessionName;
Dm.Qry.DatabaseName:=Dm.DB.DatabaseName;
while not Terminated and ClientSocket.Connected do
try
SocketStream := TWinSocketStream.Create(ClientSocket, 60000);
try
Loop:=True;
ifRecv:=False;
FillChar(Temp,SizeOf(Temp),0);
while Loop do begin
//这里有一点奇怪,第一次Read时总是只读一个byte,Why?
FillChar(Data, SizeOf(Data), 0);
if SocketStream.WaitForData(60000) then begin
if SocketStream.Read(Data, SizeOf(Data)) = 0 then begin
ClientSocket.Close;
Terminate;
Break;
end else
StrLCat(Temp,Data,128);
if not (StrPos(Temp,#13#10)=nil) then begin
Loop:=False;
ifRecv:=True;
end;
end
else begin // if SocketStream.WaitForData(60000) then
ClientSocket.Close;
Terminate;
Loop:=False;
ifRecv:=False;
end;
end; //while Loop do
if ifRecv and ClientSocket.Connected then
HandleRequest(Temp); //进行相应处理
finally
SocketStream.Free;
end;
except
//HandleException;
Dm.Free;
Terminate;
end;
end;
procedure THBServerThread.HandleRequest(const pRecvD: PChar);
//此过程用于处理Client的请求,debug无问题