IdTCPServer and Disconnect

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

IdTCPServer and Disconnect

Postby Lena » Tue Mar 24, 2015 2:26 am

Is it corret way make IdTCPServer1->Active = false; in IdTCPServer1Execute?
Code: Select all
bool ParseJSONLoginAutTime(String chek)
{
 //parse chek
 //if not correct return false

 //may be here IdTCPServer1->Active = false; ?
}

void __fastcall TForm1Main::IdTCPServer1Execute(TIdContext *AContext)
{

  try
    {
//***
        String Sdata = AContext->Connection->IOHandler->ReadLn();
        bool LoginAutTime = ParseJSONLoginAutTime(Sdata);
        if(LoginAutTime == false)
         {
          IdTCPServer1->Active = false;
          return;
         }
//*** other code
Lena
BCBJ Master
BCBJ Master
 
Posts: 567
Joined: Sun Feb 06, 2011 1:28 pm

Re: IdTCPServer and Disconnect

Postby rlebeau » Tue Mar 24, 2015 2:58 pm

You cannot deactivate TIdTCPServer from inside its own event handlers. The TIdTCPServer::Active property setter disconnects active client sockets and waits for their worker threads to fully terminate, but the worker thread that is deactivating the server is blocked waiting for the deactivation to finish. Deadlock.

If you want TIdTCPServer to deactivate itself, it must delegate the deactivation to another thread, whether that be the main UI thread via TIdNotify or TThread::Queue(), or a separate worker thread.

Why would you want to deactivate the entire server if a JSON packet from one client fails to parse? Why not just Disconnect() that one client instead?

Code: Select all
void __fastcall TForm1Main::IdTCPServer1Execute(TIdContext *AContext)
{
    //***
    String Sdata = AContext->Connection->IOHandler->ReadLn();
    bool LoginAutTime = ParseJSONLoginAutTime(Sdata);
    if (LoginAutTime == false)
    {
        AContext->Connection->Disconnect(false);
        return;
    }
    //***
}
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1528
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: IdTCPServer and Disconnect

Postby Lena » Wed Mar 25, 2015 12:19 am

AContext->Connection->Disconnect(false);


Thank you very much!
Lena
BCBJ Master
BCBJ Master
 
Posts: 567
Joined: Sun Feb 06, 2011 1:28 pm


Return to Technical

Who is online

Users browsing this forum: Google [Bot] and 12 guests