🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #476

Re: Keep Alive & reconnecting

#476
From: Remy Lebeau (TeamB)
Date: Wed, 27 Feb 2008 18:27
93 lines
3940 bytes
"JohnG" <whatis@email.com> wrote in message 
news:47c57942$1@newsgroups.borland.com...

> I believe the "Keep Alive" code I have implemented in
> negatively affecting the TIdTCPClients ability to reconnect
> to the TidTCPServer. When a client reconnects prior to
> the server realizing that the connection was severed, the
> client disconnects immediately after reconnecting.

You should not be calling Connected() in your loop at all.  Just send the 
ping and catch any error that may occur.

> I believe (guess) that the Keep Alive code is in the process
> of failing as the client is reconnecting.

That would not have any effect on it.  Your loop knows which threads to send 
to.  If a client reconnects, it is going to have a completely new thread. 
Sending a ping to a previous thread will not effect the new thread at all.

> I have the following code in a TimerEvent ( every 10 seconds)
> as my implementation of "Keep Alive" code between clients
> (TidTCPClient) and server

I would not suggest using such a timed loop at all.  Let the OnExecute event 
handler perform the actual keepalive on a per-client basis for you.  You 
shouldn't be sending a ping unless a connection has actually been idle for 
awhile.  For example (untested):

    class TMyPeerThread : public TIdPeerThread
    {
    public:
        TDateTime LastRecv;
        __fastcall TMyPeerThread(bool ACreateSuspended = true);
    };

    __fastcall TMyPeerThread::TMyPeerThread(bool ACreateSuspended)
        : TIdPeerThread(ACreateSuspended)
    {
    }


    __fastcall TForm1::TForm1(TComponent *Owner)
        : TForm(Owner)
    {
        IdTCPServer1->ThreadClass = __classid(TMyPeerThread);
    }

    void __fastcall TForm1::IdTCPServer1Connect(TIdPeerThread *AThread)
    {
        AThread->Connection->ReadTimeout = 10000;
        static_cast<TMyPeerThread*>(AThread)->LastRecv = Now();
    }

    void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
    {
        TMyPeerThread *LThread = static_cast<TMyPeerThread*>(AThread);
        TDateTime dtNow = Now();

        if( LThread->Connection->InputBuffer->Size == 0 )
        {
            if( LThread->Connection->ReadFromStack(true, IdTimeoutDefault, 
false) < 0 )
            {
                if( SecondsBetween(dtNow, LThread->LastRecv) < 30 )
                {
                    // write PING command as needed. do not wait for a 
reply...
                }
                else
                    AThread->Connection->Disconnect();

                return;
            }
        }

        LThread->LastRecv = dtNow;

        // handle inbound data as needed...
    }

> catch(...)
> {
>    IdTCPServer->Threads->UnlockList();
> }
>
> IdTCPServer->Threads->UnlockList();

If an exception occurs in your loop, you are calling UnlockList() twice. 
Don't do that.


Gambit 
Technical Details
Message-ID: <47c5ab8e$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <47c57942$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,137065672a0c16bb
X-Google-Attributes: gid11508e,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <47c57942$1@newsgroups.borland.com>
Subject: Re: Keep Alive & reconnecting
Date: Wed, 27 Feb 2008 10:27:45 -0800
Lines: 95
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-RFC2646: Format=Flowed; Original
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
NNTP-Posting-Host: 207.178.157.90
Message-ID: <47c5ab8e$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1204136846 207.178.157.90 (27 Feb 2008 10:27:26 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail