[twsocket] how to legally close and free client

2011-07-21 Thread Lukas Skala

hello,
I have server application based on TWSocketServer, clients are derived 
from TWSocketClient class.
I need disconnect inactive clients. I'm not sure how to disconnect 
client, I'm using this:


procedure DisconnectMyClient(i: Integer);
var
  C: TTcpSrvClient;
  {TTcpSrvClient = class(TWSocketClient)}
begin
C := Server.Client[I] as TTcpSrvClient;
C.Close;
C.Free; {Is this correct? sometimes it will throw access violation here}
end;


I'm using ICS v6.

I did notice that Server.OnClientDisconnect event is not triggered when 
client is not closed correctly (e.g. client is plugged of ethernet). Is 
there any possibility to detect and/or disconnect these clients 
automatically?


thank you for any answer and your time.
lukas



--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] how to legally close and free client

2011-07-21 Thread Arno Garrels
Lukas Skala wrote:
 hello,
 I have server application based on TWSocketServer, clients are derived
 from TWSocketClient class.
 I need disconnect inactive clients. I'm not sure how to disconnect
 client, I'm using this:
 
 procedure DisconnectMyClient(i: Integer);
 var
   C: TTcpSrvClient;
   {TTcpSrvClient = class(TWSocketClient)}
 begin
 C := Server.Client[I] as TTcpSrvClient;
 C.Close;

That works, though Server.Client[I].Close would be enough.

If want to close clients while iterating over the client list
make sure you iterate from high index down to zero like:
 
for I := Server.ClientCount -1 downto 0 do
  Server.Client[I].Close;

A more brutal method to enforce client close was
Server.Disconnect().

 C.Free; {Is this correct? sometimes it will throw access violation
 here} end;

That's wrong, don't do that. Client objects are freed and 
removed from the internal client list when they are closed
or disconnected automatically by the server.

 
 
 I'm using ICS v6.

You should update to ICSv7.

 
 I did notice that Server.OnClientDisconnect event is not triggered
 when client is not closed correctly (e.g. client is plugged of
 ethernet). 

That's the expected behaviour. 

 Is there any possibility to detect and/or disconnect these
 clients automatically?

Implement an idle timeout at the server side. Let the client send
some keep alive data in intervals so the server knows a client is
still there.   

-- 
Arno Garrels
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] how to legally close and free client

2011-07-21 Thread Lukas Skala


hello,
thank for quick and useful response.
i'll try to upgrade to V7 now because i'm still getting error (catched by
Server.OnBgException event), I'm getting AccessViolation or
InvalidOperation exceptions.
These errors occurs after disconnecting client by server (i'm using
timer - in same way as you adviced me). I have tried to add C.Free call
but I have thought it was nonse (you have confirmed it me).

One question for sure:

When I call Client.Close the Client will be freed automatically
(destructor of Client will be called)?

thank you again
have a nice day
lukas skala



Dne 21.7.2011 17:29, Arno Garrels napsal(a):

 Lukas Skala wrote:

 hello,
 I have server application based on TWSocketServer, clients are derived
 from TWSocketClient class.
 I need disconnect inactive clients. I'm not sure how to disconnect
 client, I'm using this:

 procedure DisconnectMyClient(i: Integer);
 var
C: TTcpSrvClient;
{TTcpSrvClient = class(TWSocketClient)}
 begin
 C := Server.Client[I] as TTcpSrvClient;
 C.Close;

 That works, though Server.Client[I].Close would be enough.

 If want to close clients while iterating over the client list
 make sure you iterate from high index down to zero like:

 for I := Server.ClientCount -1 downto 0 do
Server.Client[I].Close;

 A more brutal method to enforce client close was
 Server.Disconnect().


 C.Free; {Is this correct? sometimes it will throw access violation
 here} end;

 That's wrong, don't do that. Client objects are freed and
 removed from the internal client list when they are closed
 or disconnected automatically by the server.



 I'm using ICS v6.

 You should update to ICSv7.


 I did notice that Server.OnClientDisconnect event is not triggered
 when client is not closed correctly (e.g. client is plugged of
 ethernet).

 That's the expected behaviour.


 Is there any possibility to detect and/or disconnect these
 clients automatically?

 Implement an idle timeout at the server side. Let the client send
 some keep alive data in intervals so the server knows a client is
 still there.








--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] how to legally close and free client

2011-07-21 Thread Arno Garrels
Lukas Skala wrote:
 hello,
 thank for quick and useful response.
 i'll try to upgrade to V7 now because i'm still getting error
 (catched by Server.OnBgException event), I'm getting AccessViolation
 or InvalidOperation exceptions.

Sounds like you are accessing pointers or objects when they have been
already freed. 

 These errors occurs after disconnecting client by server (i'm using
 timer - in same way as you adviced me).

Huh? I haven't advised how to use a TTimer.
Using a timer might be tricky and the reason for your problem.
An example of using a timer to detect client timeouts is
THttpServer that encapsulates a TWSocketServer component.
It uses method TWSocketServer.Disconnect() to close timed out
clients in procedure THttpServer.HeartBeatOnTimer(Sender: TObject);. 

 I have tried to add C.Free
 call but I have thought it was nonse (you have confirmed it me).

Yes, the server gives and the server takes ;)

 
 One question for sure:
 
 When I call Client.Close the Client will be freed automatically
 (destructor of Client will be called)?

Yes, TWSocketServer manages the client objects, do not free them,
just Close or Disconnect them, that's all. 

-- 
Arno Garrels

 
 thank you again
 have a nice day
 lukas skala
 
 
 
 Dne 21.7.2011 17:29, Arno Garrels napsal(a):
  Lukas Skala wrote:
  hello,
  I have server application based on TWSocketServer, clients are
  derived from TWSocketClient class.
  I need disconnect inactive clients. I'm not sure how to disconnect
  client, I'm using this:
 
  procedure DisconnectMyClient(i: Integer);
  var
 C: TTcpSrvClient;
 {TTcpSrvClient = class(TWSocketClient)}
  begin
  C := Server.Client[I] as TTcpSrvClient;
  C.Close;
  That works, though Server.Client[I].Close would be enough.
 
  If want to close clients while iterating over the client list
  make sure you iterate from high index down to zero like:
 
  for I := Server.ClientCount -1 downto 0 do
 Server.Client[I].Close;
 
  A more brutal method to enforce client close was
  Server.Disconnect().
 
  C.Free; {Is this correct? sometimes it will throw access violation
  here} end;
  That's wrong, don't do that. Client objects are freed and
  removed from the internal client list when they are closed
  or disconnected automatically by the server.
 
 
  I'm using ICS v6.
  You should update to ICSv7.
 
  I did notice that Server.OnClientDisconnect event is not triggered
  when client is not closed correctly (e.g. client is plugged of
  ethernet).
  That's the expected behaviour.
 
  Is there any possibility to detect and/or disconnect these
  clients automatically?
  Implement an idle timeout at the server side. Let the client send
  some keep alive data in intervals so the server knows a client is
  still there.
--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be


Re: [twsocket] how to legally close and free client

2011-07-21 Thread Wilfried Mestdagh
 When I call Client.Close the Client will be freed automatically
 (destructor of Client will be called)?

Yes. Sidenote: if you do this from withing an event then call CloseDelayed.

-- 
mvg, Wilfried
http://www.mestdagh.biz
http://www.comfortsoftware.be
http://www.expertsoftware.be


 -Oorspronkelijk bericht-
 Van: twsocket-boun...@elists.org [mailto:twsocket-boun...@elists.org]
 Namens Lukas Skala
 Verzonden: donderdag 21 juli 2011 20:23
 Aan: twsocket@elists.org
 Onderwerp: Re: [twsocket] how to legally close and free client
 
 
 hello,
 thank for quick and useful response.
 i'll try to upgrade to V7 now because i'm still getting error (catched
 by
 Server.OnBgException event), I'm getting AccessViolation or
 InvalidOperation exceptions.
 These errors occurs after disconnecting client by server (i'm using
 timer - in same way as you adviced me). I have tried to add C.Free call
 but I have thought it was nonse (you have confirmed it me).
 
 One question for sure:
 
 When I call Client.Close the Client will be freed automatically
 (destructor of Client will be called)?
 
 thank you again
 have a nice day
 lukas skala
 
 
 
 Dne 21.7.2011 17:29, Arno Garrels napsal(a):
   Lukas Skala wrote:
   hello,
   I have server application based on TWSocketServer, clients are
 derived
   from TWSocketClient class.
   I need disconnect inactive clients. I'm not sure how to disconnect
   client, I'm using this:
 
   procedure DisconnectMyClient(i: Integer);
   var
  C: TTcpSrvClient;
  {TTcpSrvClient = class(TWSocketClient)}
   begin
   C := Server.Client[I] as TTcpSrvClient;
   C.Close;
   That works, though Server.Client[I].Close would be enough.
 
   If want to close clients while iterating over the client list
   make sure you iterate from high index down to zero like:
 
   for I := Server.ClientCount -1 downto 0 do
  Server.Client[I].Close;
 
   A more brutal method to enforce client close was
   Server.Disconnect().
 
   C.Free; {Is this correct? sometimes it will throw access violation
   here} end;
   That's wrong, don't do that. Client objects are freed and
   removed from the internal client list when they are closed
   or disconnected automatically by the server.
 
 
   I'm using ICS v6.
   You should update to ICSv7.
 
   I did notice that Server.OnClientDisconnect event is not triggered
   when client is not closed correctly (e.g. client is plugged of
   ethernet).
   That's the expected behaviour.
 
   Is there any possibility to detect and/or disconnect these
   clients automatically?
   Implement an idle timeout at the server side. Let the client send
   some keep alive data in intervals so the server knows a client is
   still there.
 
 
 
 
 
 
 
 --
 To unsubscribe or change your settings for TWSocket mailing list
 please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
 Visit our website at http://www.overbyte.be

--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be