Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Apr : Re: Delphi 7 & Indy 10 TidCmdTCPServer OnCommand Event
| Subject: | Re: Delphi 7 & Indy 10 TidCmdTCPServer OnCommand Event |
| Posted by: | "Codeman II" (thegentlem..@webmail.co.za) |
| Date: | Wed, 11 Apr 2007 12:28:03 |
> Then you are doing something wrong. Please show your actual code. It
> is probably not thread-safe.
Here is my code for the OnCommand event of one of my commands.
CODE BEGIN
---------------------------------------------------------------------------
procedure TfrmMainServer.idCommandcmdFlagUpdateCommand(
ASender: TIdCommand);
var
Sync :TidSync;
X :Integer;
ACommand :String;
begin
{
Here I receive a command to update an item's Flag.
Broadcast this message to ALL NETWORK computers except to the IP this
message was received from
}
Sync := TIdSync.Create;
Try
Try
ResponseString.Clear;
ParamsList.Clear;
//Receive
SenderIP := ASender.Context.Binding.PeerIP;
ACommand := ASender.CommandHandler.Command + ' ';
For X := 0 to ASender.Params.Count - 1 do ACommand := ACommand +
ASender.Params.Strings[X] + ' ';
ACommand := Trim(ACommand);
//fADirection, fALine, fAIPAddress are public variables to hold values
for the SynchronizeMethod() method
//which update the log file display using a TRichEdit component.
fADirection := dirIN; fALine := ASender.CommandHandler.Command;
fAIPAddress := SenderIP;
Sync.SynchronizeMethod(WriteToLog);
//Sent
ResponseString.Add('FlagUpdate Received.');
ASender.Response.AddStrings(ResponseString);
fADirection := dirOUT; fALine := ResponseString.Text; fAIPAddress :=
LocalIP;
Sync.SynchronizeMethod(WriteToLog);
For X := 0 to ComputerIPS.Count - 1 do
begin
If ComputerIPS.Strings[X] <> SenderIP then
begin
//fACommand, fAHost holds values for the SynchronizeMethod() to
broadcast command to the rest of the clients.
fACommand := ACommand; fAHost := ComputerIPS.Strings[X];
Sync.SynchronizeMethod(SendCommand);
fADirection := dirOUT; fALine := 'Broadcasting to: ' + fAHost;
fAIPAddress := LocalIP;
Sync.SynchronizeMethod(WriteToLog);
end;
end;
//Blank line
fADirection := dirNone; fALine := ''; fAIPAddress := '';
Sync.SynchronizeMethod(WriteToLog);
Except
On E:Exception do
begin
//Error
fADirection := dirError; fALine := 'VCL Error: ' + E.Message;
fAIPAddress := LocalIP + ' --> ' + SenderIP;
Sync.SynchronizeMethod(WriteToLog);
end;
On E:EidException do
begin
//Error
fADirection := dirError; fALine := 'Indy Error: ' + E.Message;
fAIPAddress := LocalIP + ' --> ' + SenderIP;
Sync.SynchronizeMethod(WriteToLog);
end;
end;
Finally
Sync.Free;
end;
end;
---------------------------------------------------------------------------
CODE END
Thanks!