[Lazarus] Synapse CanRead, true on thread terminate

kapibara kapibara.pas at aol.com
Sun Jul 13 15:26:29 CEST 2014


Hi, I'm using Synapse to make a TCP server. Does someone know why the 
function CanRead return true when the server thread terminates? In the 
synapse echo demo, this causes a new ClientThread to be created on 
closing the app or any time the thread is terminated. I cant figure out 
why this should happen?

To avoid that I changed:|

   if CanRead(1000) then|

to :|

   if CanRead(1000) and (not Terminated) then|

Execute of server thread (from synapse echo demo):

procedure TTCPEchoDaemon.Execute;
var
   ClientSock: TSocket;
begin
   with Sock do
     begin
       CreateSocket;
       SetLinger(true,10000);
       Bind('127.0.0.1','7070');
       Listen;
       repeat
         if Terminated then Break;
         if CanRead(1000) then
           begin
             ShowMessage('The thread is dead, long live the thread.');  
//Exception on thread terminate
             ClientSock:= Accept;
             if LastError = 0 then
             begin
               TTCPEchoThrd.Create(ClientSock);
             end;
           end;
       until False;
     end;
end;






It causes the program to go on and create a new ClientThread even when 
Terminated is true. After understanding that this happened, I worked 
around it by changing:

Code: [Select] <javascript:void(0);>

|   if CanRead(1000) then|


to this:

Code: [Select] <javascript:void(0);>

|   if CanRead(1000) and (not Terminated) then|


But is that good? Shouldnt something like that be in one of the demos if 
so? Or I have not understood how it should be done?

|procedure TMyServer.Execute;
var
   ClientSock: TSocket;
   aClientThread: TClientThread;
begin
   with Sock do
   begin
     CreateSocket;  // auto-created by Synapse if not done manually}
     SetLinger(True, 10000);
     Bind('0.0.0.0','7070');
     Listen;
     if LastError = 0 then Log('Server is Online');
     repeat
       if Terminated then
       begin
         if Assigned(aClientThread) then aClientThread.Terminate;
         Break;
       end;
       if CanRead(1000) and (not Terminated) then  //Neglect CanRead if serverthread is terminated!
       begin
         Log('Server: Someone connected');
         ClientSock := Accept;
         if LastError = 0 then
         begin
           aClientThread:= TClientThread.Create(ClientSock);
           aClientThread.OnLog:= Self.OnLog; //Enable logging to main form
           aClientThread.Start;
         end;
       end;
     until False;
   end;
end;|




Also, from comments in Synapse sources:

"CanRead: This function is need only on special cases, when you need use
@link(RecvBuffer) function directly! read functioms what have timeout as
calling parameter, calling this function internally."


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20140713/02e102ee/attachment-0002.html>


More information about the Lazarus mailing list