【问题标题】:Upgrade Indy9 to Indy10将 Indy9 升级到 Indy10
【发布时间】:2010-01-11 21:09:56
【问题描述】:

我想使用 Delphi 2007 将我的应用程序从 Indy 9 升级到 10。 在这个线程中有一个对 Indy9 TIdUDPBase.SendBuffer 的调用,但这不会在 Indy10 中编译,因为方法参数不存在。第三个参数 aBuffer 是一个 var 参数,我在 Indy10 中没有找到任何这样的方法签名。 有什么替代方法可以调用吗?

procedure TSenderThread.Execute;
var
  vTimeData: TTimeDataRecord;
  I: Integer;
  FElapsed: Int64;
  FTimerElappsed,
  vLastTimerElappsed: Int64;
begin
  vTimeData.Size := SizeOf(TTimeDataRecord);
  vTimeData.ClientCount := 1;
  Priority := tpHighest;
  FIdUDPClient := TIdUDPClient.Create(nil);
  FIdUDPClient.BroadcastEnabled := True;
  try
    while not (Terminated or Application.Terminated) do
    begin
      Sleep(1000);
      //Measure Time frame
      vLastTimerElappsed := FTimerElappsed;
      QueryPerformanceCounter(FTimerElappsed);
      FElapsed := ((FTimerElappsed-vLastTimerElappsed)*1000000) div FFrequency;
      vTimeData.TotalTimeFrame := FElapsed;
      if FRunning then
      begin
        FElapsed := ((FTimerElappsed-FStart)*1000000) div FFrequency;
        vTimeData.CurrentMessageTime := FElapsed;
      end
      else
        vTimeData.CurrentMessageTime := 0;
      //Copy Values
      vTimeData.AccumulatedTime := InterlockedExchange(TimeData.AccumulatedTime,0);
      vTimeData.MessageCount := InterlockedExchange(TimeData.MessageCount,0);
      for I := 0 to TimeClassMax do
        vTimeData.TimeClasses[I] := InterlockedExchange(TimeData.TimeClasses[I],0);

       // Calls procedure TIdUDPBase.SendBuffer(AHost: string; const APort: Integer; var ABuffer; const AByteCount: integer);
       // This is changed in Indy10, unable to compile  
      FIdUDPClient.SendBuffer('255.255.255.255', UIPerfPort, vTimeData, TimeData.Size);
    end;
  finally
    FreeAndNil(FIdUDPClient);
  end;
end;

编辑: vTimeData 基本上是一个整数数组。

  TTimeDataRecord = record
    Size: Integer; //Size of record structure is transfered and compared for safty reasons.
    ClientCount: Integer;
    AccumulatedTime: Integer; //This is the accumulated time busy in microseconds
    CurrentMessageTime: Integer; //This is the time the current message has been processed. If several computers report a high value at the same time it indicates a freeze!
    TotalTimeFrame: Integer; //This is the total time measured in microseconds
    MessageCount: Integer;
    TimeClasses: array [0..TimeClassMax] of Integer;
  end;

【问题讨论】:

    标签: delphi indy


    【解决方案1】:

    你有一个同名的方法

    procedure TIdUDPClient.SendBuffer(const AHost: string; const APort: TIdPort;
      const ABuffer: TIdBytes);
    

    它需要一个字节数组,而不是一个无类型的缓冲区。你的数据是什么样的?您只需要将数据写入字节数组。比如:

    var
     Buffer: TIdBytes;
    begin
     SetLength(Buffer, YourSizeOfData);
     Move(YourData, Buffer[0], YourSizeOfData);
     FIdUDPClient.SendBuffer('255.255.255.255', UIPerfPort, Buffer);
    end;
    

    但正如我所说,这取决于数据的类型。不过这个方法还可以。

    编辑:

    现在我可以看到您有记录,您有两个选择:

    只需将整个记录移动到字节数组中。

    Move(@aRecord, Buffer[0], (6 + TimeClassMax) * SizeOf(Integer));
    

    在您的记录中有一个 CopyToBytes 方法来执行实际复制。我猜更笼统。

    TTimeDataRecord = record
      Size: Integer; //Size of record structure is transfered and compared for safty reasons.
      ClientCount: Integer;
      AccumulatedTime: Integer; //This is the accumulated time busy in microseconds
      CurrentMessageTime: Integer; //This is the time the current message has been  processed. If several computers report a high value at the same time it indicates a freeze!
      TotalTimeFrame: Integer; //This is the total time measured in microseconds
      MessageCount: Integer;
      TimeClasses: array [0..TimeClassMax] of Integer;
      procedure CopyToBytes(var Buffer: TIdBytes);
    end
    

    CopyToBytes 的实现

    procedure TTimeDataRecord.CopyToBytes(var Buffer: TIdBytes);
    begin
      // copy the data however you see fit
    end;
    

    【讨论】:

    • 这是一条记录,查看更新,但它只包含整数。也许我可以将其类型转换为 TIdBytes 还是必须将其复制到另一个 TIdBytes 类型的缓冲区?
    • 仅供参考,Indy 10 具有 RawToBytes() 和 BytesToRaw() 函数,用于将 TIdBytes 值从/转换为原始内存块。
    猜你喜欢
    • 1970-01-01
    • 2012-03-16
    • 2015-10-17
    • 2015-03-04
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多