Einzelnen Beitrag anzeigen

Benutzerbild von TERWI
TERWI

Registriert seit: 29. Mär 2008
Ort: D-49626
379 Beiträge
 
Delphi 11 Alexandria
 
#9

AW: UDP Broadcast: Fehler bei OnUDPRead

  Alt 10. Feb 2016, 10:06
Im Prinzip das gleiche wie von Gargamel in Posting #1.
Code:
typ
  TWSDiscovery = Class
  public
    constructor Create(); // override;
    destructor Destroy; override;
    procedure  Send_PROBE(RcvPort : word);
    procedure  Send_HELLO();
    procedure  Send_BYE();
    procedure  Send_PROBE_CHINACAM(RcvPort : word);
  private
    ... div var
    FMethod     : TMethod;
    udpSrv_Lstn : TIdUDPServer;
    udpSrv_Send : TIdUDPServer;
    TimeOutTimer : TTimer;
    ...
    procedure udpSrv_LstnRead(AThread : TIdUDPListenerThread;
                              AData   : TidBytes;
                              ABinding : TIdSocketHandle);
    procedure udpSrv_SendRead(AThread : TIdUDPListenerThread;
                              AData   : TidBytes;
                              ABinding : TIdSocketHandle);
    procedure TimeOutTimerOnTimer(Sender: TObject);
    ...
  published
    property ...
  end;

constructor TWSDiscovery.Create();
begin
  inherited Create;
  ...
  TimeOutTimer         := TTimer.Create(nil);
  TimeOutTimer.Enabled := false;
  TimeOutTimer.Interval := 3000;
  TimeOutTimer.OnTimer := TimeOutTimerOnTimer;
  // create the "listening" server for HELLO & BYE
  udpSrv_Lstn := TIdUDPServer.Create;
  // !!! http://www.delphigroups.info/2/11/215650.html
  FMethod.Data := udpSrv_Send;                   // !!!!
  FMethod.Code := @TWSDiscovery.udpSrv_SendRead; // !!!!
  with udpSrv_Send do begin                      // !!!!
    Active          := false;
    OnUDPRead       := TUDPReadEvent(FMethod);  // !!!!
//    OnUDPRead       := udpSrv_LstnRead;         // doesnt't work !!!
    Bindings.DefaultPort := WS_PORT_HELLOBYE;
    DefaultPort     := 20001;
    BufferSize      := 4096;
    ThreadedEvent   := true;
    BroadcastEnabled := true;
    Active          := True;
  end;
  // create the server for sending PROBE & listen for PROBEMATCH
  ...
  // das gleiche noch mal
end;
Eigentlich (!?) hab ich schon ne Lösung gefunden - siehe Link im Code bei Create.
Aber A) verstehe ich die auch nicht wirklich und B) geht mir nicht in den Kopf, warum das was in/auf einer Form funktioniert und ausgerechnet "nur" bei der Zuweisung der OnRead-procedure so ein Theater macht. Compilerfehler ?
Die Timer-Sache fnktioniert jedenfalls wie gewohnt.

Es spielt dabei anscheinend auch keine Geige, ob ich die var- und procedure Deklarationen unter private, protected, public oder published packe.

So wie es Remy Lebeau beschrirben hat und es da oben mit dem Umweg über die Zuweisung mit TMehtod steht, funktioniert es.
Sehr eigenartig ....

Geändert von TERWI (10. Feb 2016 um 11:04 Uhr)
  Mit Zitat antworten Zitat