1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
type
TClientEventWithStr = procedure(IdThread:Cardinal; Str:String) of object;
TClientEvent = procedure(IdThread:Cardinal) of object;
type
TCustomClientThread = class(TThread)
private
{ Déclarations privées }
fOnError,
fOnStateChange,
fOnDataSended,
fOnDataReceived : TClientEventWithStr;
fOnConnected,
fOnDisconnected : TClientEvent;
fClient : TSocket;
PfClient : PInteger;
fAdresse : TClientAdresse;
fBuf : PChar;
MsgStr : String;
fSockAddrServeur : TSockAddr;
procedure PrSendBuf;
procedure PrReceiveBuf;
procedure PrConnection;
procedure PrDeconnection;
protected
procedure Execute; override;
public
Constructor Create( Adresse : PClientAdresse;
Client:TSocket); overload;
Constructor Create( Socket:TSocket); overload;
Destructor Destroy; override;
end;
Procedure TCustomClientThread.Execute;
var
Msg : TMsg ;
fpHandles: ^THandle;
begin
fpHandles:=nil;
While (Not Terminated) Do
Begin
MsgWaitForMultipleObjects(0,fpHandles,FALSE,500,QS_ALLINPUT);
While (Not Terminated) And (PeekMessage(Msg,HWND(-1),0,0,PM_REMOVE)) Do
If Msg.message = CLIENT_THREAD Then
Case Msg.wParam of
CONNECTION : PrConnection;
DECONNECTION : PrDeconnection;
RECEPTION : PrReceiveBuf;
ENVOIE : Begin
fBuf:=AllocMem(Length(PChar(Msg.lParam))+1);
StrCopy(fBuf,PChar(Msg.lParam));
PrSendBuf;
End;
SET_PROC_ERROR : @fOnError := Pointer(Msg.lParam);
SET_PROC_STATE : @fOnStateChange := Pointer(Msg.lParam);
SET_PROC_DATASENDED : @fOnDataSended := Pointer(Msg.lParam);
SET_PROC_DATARECEIVED : @fOnDataReceived := Pointer(Msg.lParam);
SET_PROC_CONNECTED : @fOnConnected := Pointer(Msg.lParam);
SET_PROC_DISCONNECTED : @fOnDisconnected := Pointer(Msg.lParam);
End
Else Begin
TranslateMessage(Msg);
DispatchMessage(Msg);
End;
End;
End;
//Bon pour la fiche c'est plus laborieux
type
TForm1 = class(TForm)
Memo1: TMemo;
TestEvent: TButton;
procedure TestEvent(Sender: TObject);
private
public
procedure threadStr(IdThread:Cardinal; Str:String);
end;
procedure TForm1.threadStr(IdThread:Cardinal; Str:String);
begin//pour acceder au memo obligation de préciser Form1
Form1.memo1.Lines.Append(Str);
end;
procedure TForm1.TestEvent(Sender: TObject);
var addrProc:TClientEventWithStr;
begin//obligation de passer par une variable
addrProc:=threadStr;
PostThreadMessage(ThreadID,CLIENT_THREAD,SET_PROC_STATE,Integer(@addrProc));
end; |
Partager