[fpc-pascal] Re: I need an lNet expert: multiple connections and only one central CallAction() - is this possible?

Bernd Kreuss prof7bit at googlemail.com
Tue May 29 14:20:12 CEST 2012


On 28.05.2012 16:02, Bernd wrote:

> What I would have expected would be some API where I can register
> all these connections (outgoing connections, listening sockets,
> etc) and then have only one thread blocking in only one call that
> will watch all these objects at once, but I can't find any
> explanation how this is supposed to be done with lNet.

I can now answer my own question:

It seems it is meant to be used in the following way. The first thing
I have to do is to have my own instance of a TLEventer class:

  FEventer: TLEventer;

and instantiate it:

  FEventer := BestEventerClass.Create;

and then set a short timeout (1000ms or so) to make the eventer
blocking but still be able to check the termination of the thread in
regular intervals and create a thread that calls CallAction in an
infinite loop:

  procedure TEventerThread.Execute;
  begin
    FEventer.TimeOut := 1000;
    repeat
      FEventer.CallAction;
    until Terminated;
  end;

And then I can create as many TLTcp instances as I need and for every
such TLTcp the first thing after creation is to set the Eventer property:

  FLnetListener := TLTcp.Create(self);
  with FLnetListener do begin
    Eventer := FEventer;
    OnAccept := @OnListenerAccept;
    FLnetListener.Listen(FListenPort);
  end;

and even more such objects for outgoing connections (one for each
connection), each setting the Eventer to the same one and only eventer
instance:

constructor TBuddy.Create(AClient: IClient);
  inherited Create;
  FClient := AClient;
  FLnetClient := TLTcp.Create(nil);
  FLNetClient.Eventer := AClient.Eventer;
  ...
end;

procedure TBuddy.InitiateConnect;
begin
  with FLnetClient do begin
    OnConnect := @Self.OnProxyConnect;
    OnReceive := @Self.OnProxyReceive;
    OnDisconnect := @Self.OnProxyDisconect;
    OnError := @Self.OnProxyError;
  end;
  FLnetClient.Connect(FClient.TorHost, FClient.TorPort);
end;


Seems to work quite well, I only have to find out how to properly shut
down incoming connections that were disconnected by the other side and
then start rapidly firing recv events with 0 bytes (CPU goes 100%),
the only way seems to be to set the Dispose property of the TLHandle
to true once I receive 0 bytes for the first time on this connection
and then pretend the connection is closed but I am not sure if this is
allowed and I am not leaking OS handles that way. Also with my code I
seem to be able to produce strange crashes inside heaptrc that I have
never seen before, still investigating how to reproduce.

Bernd




More information about the fpc-pascal mailing list