Giter Club home page Giter Club logo

Comments (5)

synopse avatar synopse commented on June 6, 2024 1

This changes the default behavior of ConnectionCreate, which creates the connection, but don't add it, nor start it. As its name states.
So your patch breaks the Liskov substitution principle of OOP.

For a new client connection, ConnectionCreate + Clients.Start() should be called explicitly.
I have modified the source so that

  • TAsyncConnections.ThreadClientsConnect can be called to create a new client on need;
  • fClients.Start will awake reading thread(s) using the new fClients.OnStart event

from mormot2.

Coldzer0 avatar Coldzer0 commented on June 6, 2024

Hello @synopse

Is there any new update related to this issue ?

Thanks.

from mormot2.

Coldzer0 avatar Coldzer0 commented on June 6, 2024

@synopse

So After sometime reading the code I've some concern regards the TAsyncClient.

I think this class should serve the Connection as only a Async ThreadPoll for Read & Write.

So the Idea behind the the aClientsCount should change.
My idea is you should be able to add your own Socket Connection to the TPollAsyncSockets and still works fine.

Take my new code here as example
https://gist.github.com/Coldzer0/c91b6e144ac0c3227f884c3b355326ac
This should work fine But theโ€‹Fix only works if I add aClientsCount bigger than 0 And It should be available .

So What I propose is the next change and everything should work as expected.

diff --git a/src/net/mormot.net.async.pas b/src/net/mormot.net.async.pas
index d9a500b7..589196ee 100644
--- a/src/net/mormot.net.async.pas
+++ b/src/net/mormot.net.async.pas
@@ -587,7 +587,8 @@ type
   // - will use a TAsyncConnection inherited class to maintain connection state
   // of each connected client
   TAsyncClient = class(TAsyncConnections)
-  protected
+  Protected
+    Function ConnectionCreate(aSocket: TNetSocket; Const aRemoteIp: RawUtf8; out aConnection: TAsyncConnection): Boolean; Override;
     procedure Execute; override;
   public
     /// start the TCP client connections, connecting to the supplied IP server
@@ -1714,12 +1715,7 @@ begin
     raise EAsyncConnections.CreateUtf8('%: %:% connection failure (%)',
       [self, fThreadClients.Address, fThreadClients.Port, ToText(res)^]);
   connection := nil;
-  if ConnectionCreate(client, {ip=}'', connection) then
-  begin
-    if fThreadReadPoll <> nil then
-      fThreadReadPoll.fEvent.SetEvent // awake reading thread(s)
-  end
-  else
+  if not ConnectionCreate(client, {ip=}'', connection) then
     client.ShutdownAndClose({rdwr=}false);
 end;
 
@@ -2282,6 +2278,23 @@ begin
     aLog, aOptions, aThreadPoolCount);
 end;
 
+function TAsyncClient.ConnectionCreate(aSocket: TNetSocket;
+  const aRemoteIp: RawUtf8; out aConnection: TAsyncConnection): Boolean;
+begin
+  result := false;
+  if not Terminated then
+  begin
+    aConnection := fConnectionClass.Create(self, aRemoteIp);
+    if inherited ConnectionAdd(aSocket, aConnection) then
+    begin
+      result := Clients.Start(aconnection); // assign a new connection to the internal reading poll
+      if Result then
+        if fThreadReadPoll <> nil then
+          fThreadReadPoll.fEvent.SetEvent; // awake reading thread(s)
+    end;
+  end;
+end;
+
 procedure TAsyncClient.Execute;
 var
   notif: TPollSocketResult;

Regards.

from mormot2.

Coldzer0 avatar Coldzer0 commented on June 6, 2024

Hello @synopse

Did you check my comment above ?

from mormot2.

Coldzer0 avatar Coldzer0 commented on June 6, 2024

Thanks @synopse for the update.

I'm not professional when it's about OOP so excuse my lake of knowledge in this area. (Thanks for the explanation)
The last commit fix the problem
By using Self.Clients.Start(connection); After connecting.

Or just set the clients count to what we want.

Thanks.

from mormot2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.