[Lazarus] lnet sockets

Bernd prof7bit at gmail.com
Thu Oct 25 13:07:26 CEST 2012


2012/10/25 Antonio Fortuny <a.fortuny at sitasoftware.lu>:

> You got it. This is how the WinCE application runs:
> user reacts on a Form
> the appropriate form event is lauched
>   some information is needed from the server before carry on any further
> processing
>   ask the net server:
>     open connection
>     send request
>     wait for answer
>     close connection
>     decide what to do next in case of any error
>     process answer and do whatever has to be done
> this means that the application Q cannot be used: the user event method
> cannot be exited before the answer to the remote server. The application is
> freezed, ok.

Here is an alternative way to implement it (this one can even reply
and have a conversation of multiple commands going forth an back
between the client and server):

pocedure OnEverythingGetsStarted;
begin
   if State = WAITING then exit;
   State := WAITING
   maybe also disable some buttons that might interfere
   open the connection
   send the request
   start the timeout timer
   // return from that event
end;

procedure OnTimer;
begin
  if State = WAITING then begin
    Disconnect
    State := IDLE
    enable the disabled buttons
  end;
end;

procedure OnReceive(S: TLSocket);
begin
  Answer := S.GetMessage()
  case Answer of
  'foo': begin
     DoThis();
     SendAnotherCommand();
  end
  'bar': begin
     DoThat();
     SendSomeThingDifferent();
  end
  else begin
     DoWhateverNeedsToBeDone();
     S.Disconnect;
     State := IDLE;
     enable the disabled buttons
  end;
end;

This will never block and never freeze and never eat any unneeded CPU
cycles and it is les code than messing around with the eventers and
CallAction directly and also less code than low level programming
against sockets and/or winsock2 directly.




More information about the Lazarus mailing list