Search code examples
multithreadingdelphidelphi-7indy

Multithread - How to make timeout and how to make "interlocking"?


I have a thread. Inside of them, i have a loop to send UDP packet. In this loop i need wait the UDP response of the device (that received the previous packet [if OK or not]) before send the next message. If OK i send the next message of the StringList, if error i stop the thread. How to make it? And i need more: I need implement a timeout of the 3 seconds if the software doesn't receive the response of the device.

I'm using the TIdUDPServer to listen the udp packages.

Here is my code:

TEnvioFirmware = class(TThread)
  private
    I: integer;
    Lock: TCriticalSection;
    mMac: string;
    mVersao: integer;
    mProduto: integer;
    _max: integer;
    _min: integer;
    mIP: string;
    firmware_string: TStringList;
    idpclnt: TIdUDPClient;
    progress: TProgressBar;
    lbPorc: TLabel;
    bAck: Boolean;
    procedure Executar;
    procedure OnVerificarTimeOut(Sender: TObject);
  public
    tmrTimeout: TTimer;
    bContinua: Boolean;
    procedure Execute; override;
    constructor Create(CreateSuspended: Boolean; firmware_string: TStringList;
      edtIP: string; idpclnt: TIdUDPClient; max, atual: integer;
      var pb: TProgressBar; var lblPorcentagem: TLabel; abAck: Boolean);

  end;

procedure TEnvioFirmware.Execute;
begin
  inherited;
  // Synchronize(Executar);
  Executar;
end;

procedure TEnvioFirmware.Executar;
var
  I, J: integer;
  X: pacote;
  Y: String;
  B: String;
  B2: String;
  msgCount: integer;
  Buffer: TBytes;
  // array[0..5] of Byte;
  max, atual: integer;
  BufferSend: TBytes;
begin

  SetLength(Buffer, 6);

  idpclnt.Host := mIP;
  idpclnt.Active := true;
  msgCount := firmware_string.Count;
  max := msgCount - 1; // Progress Bar
  // pb.Max := max;
  self.progress.Min := 0;
  self.progress.max := max;

  B := '12345';
  B2 := '0';

  if bAck then
  begin           //trying to make timeout
    tmrTimeout := TTimer.Create(nil);
    tmrTimeout.Interval := 3000;
    tmrTimeout.OnTimer := OnVerificarTimeOut;
  end;

  bContinua := True;


  for I := 0 to msgCount - 1 do
  begin

    B[1] := chr(15);
    B[2] := chr(0);
    B[3] := chr(2);
    B[4] := chr((I) mod 256);
    B[5] := chr((I) div 256);

    B2[1] := chr(255);

    Y := B + firmware_string.Strings[I] + B2;

    SetLength(BufferSend, Length(Y));

    for J := 0 to Length(BufferSend) - 1 do
    begin

      BufferSend[J] := Ord(Y[J + 1]);

    end;



    if bContinua then
      idpclnt.SendBuffer(BufferSend);  //after send the device will response

    if not bAck then
    begin
      Sleep(200);
      bContinua := true;
      progress.Position := I + 1;
      lbPorc.Caption :=
        IntToStr(Round((100 * progress.Position) / progress.max));
      atual := I;
    end
    else
    begin

      tmrTimeout.Enabled := True;

    end;

  end;

  Buffer[0] := $0F;
  Buffer[1] := 00;
  Buffer[2] := 03;
  Buffer[3] := (msgCount) mod 256;
  Buffer[4] := (msgCount) div 256;
  Buffer[5] := 255;

  idpclnt.SendBuffer(Buffer);

  Sleep(150);

end;

On UDPRead

if (AData[0] = 15) and (AData[1] = 1) and (AData[2] = 2) and (Count = 5)
  then
  begin

    if AData[3] = 0 then
    begin
      MainEstrutura.objFirmwareUpdater.bContinua := true;
      MainEstrutura.objFirmwareUpdater.tmrTimeout.Enabled := false;
    end
    else
    begin
      MainEstrutura.objFirmwareUpdater.bContinua := false;
      MainEstrutura.objFirmwareUpdater.tmrTimeout.Enabled := false;
    end;

  end;

Solution

  • Since you need to serialize your packets, you should use a TIdUDPClient inside the thread instead of using a TIdUDPServer outside of the thread. Then you don't have to deal with synchronizing the OnUDPRead event or using a timer at all. Simply have the thread SendBuffer() an outgoing packet, then immediately ReceiveBuffer() the inbound response with a timeout specified, and continue looping as needed until finished. If the timeout elapses, either re-sent the outbound packet, or terminate the thread, depending on your needs.

    The only reason to use a TIdUDPServer in this situation is if you have multiple firmware threads running at the same time and they all respond to the same IP/Port, in which case it would make sense to have a single TIdUDPServer receiving all of those responses and delgating them to each appropriate firmware thread as needed.