[英]Incompatible parameter list when converting from Delphi 7 to Delphi 10.3
I have this app that I'm trying to convert, but I have some issues with server communication.我正在尝试转换这个应用程序,但我在服务器通信方面遇到了一些问题。 This line:这一行:
procedure UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
that gives me this error:这给了我这个错误:
The UDPServerUDPRead method referenced by UDPServer.OnUDPRead has an incompatible parameter list. UDPServer.OnUDPRead 引用的 UDPServerUDPRead 方法具有不兼容的参数列表。 Remove the reference?删除引用?
That procedure is used like this:该程序是这样使用的:
procedure TFrmMain.UDPServerUDPRead(Sender: TObject; AData: TStream; ABinding: TIdSocketHandle);
var
Buffer: Tarray1024ofChar ;
count: Integer;
begin
count := AData.Size;
if count > Length(Buffer) then exit;
AData.Read(Buffer, count);
if UDPServerActive then DataReceived(count,ABinding.PeerIP,ABinding.PeerPort,Buffer);
end;
What is wrong with it?它有什么问题? What should I change?我应该改变什么?
The signature of the TIdUDPServer.OnUDPRead
event changed from Indy 9 to Indy 10. TIdUDPServer.OnUDPRead
事件的签名从 Indy 9 更改为 Indy 10。
In Indy 9, when a data packet arrives, you are given a TStream
object wrapping the raw data.在 Indy 9 中,当数据包到达时,您将获得一个包装原始数据的TStream
对象。
In Indy 10, when a data packet arrives, you are given a TIdBytes
byte array of the raw data.在 Indy 10 中,当数据包到达时,您将获得原始数据的TIdBytes
字节数组。
So you need to update your code accordingly, eg:所以你需要相应地更新你的代码,例如:
type
// Char is a 1-byte AnsiChar in D7, but is a 2-byte WideChar in D2009+!
Tarray1024ofChar = array[0..1023] of AnsiChar{Char} // or, use Byte instead...
...
procedure TFrmMain.UDPServerUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
var
Buffer: Tarray1024ofChar;
count: Integer;
begin
count := Length(AData);
if count > Length(Buffer) then exit;
BytesToRaw(AData, Buffer, count);
if UDPServerActive then
DataReceived(count, ABinding.PeerIP, ABinding.PeerPort, Buffer);
end;
If you can change DataReceived()
to accept a PAnsiChar
(or PByte
) instead of a Tarray1024ofChar
, you can simplify the code further by getting rid of the local Buffer
variable altogether:如果您可以更改DataReceived()
以接受PAnsiChar
(或PByte
)而不是Tarray1024ofChar
,您可以通过完全摆脱本地Buffer
变量来进一步简化代码:
procedure TFrmMain.UDPServerUDPRead(AThread: TIdUDPListenerThread; const AData: TIdBytes; ABinding: TIdSocketHandle);
begin
if UDPServerActive then
DataReceived(Length(AData), ABinding.PeerIP, ABinding.PeerPort, PAnsiChar{PByte}(AData));
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.