🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: help with Indy Telnet

#2067
From: Andre' Burke
Date: Thu, 31 Aug 2006 08:21
257 lines
8214 bytes
Remy,

Its a fairly simple application.
I am connecting to a buffer-box that gathers data from a PABX.
It uses a simple Telnet server and I connect to this on port 23 and request 
data from it.

I need to disconnect when I am done getting the data so that I can connect 
to the next buffer-box.

Basically I have instantiated and TIdTelnet in the code.
I use a timer to make the connections and request the data.
I then use the OnDataAvailable method to grab the incoming data and insert 
into a TThreadList called Incoming_Queue.
Inside the timer I check to see if there is data in my queue which I then 
process.

Both the connect and the disconnect happen inside the timer.

Now the thing is that in testing this app, I have been connecting and 
disconnecting to the buffer boxes (simulated) every 20 seconds in order to 
simulate a long useage period.  In real life this would happen once an hour.
Sometimes the app will run for 5 hours before it hangs, other times for less 
than an hour.

The reason I recreate the TIdTelnet component every time is because I was 
hoping to sidestep the problem this way.
It *seems* to have helped a little, previously I could not run the app for 
much more than 2 hours.
But it has not removed the problem.

I have included the most important parts of the code while having trimmed 
the timer code so as not to confuse. (--snip-- indicates a cut in the code 
to simplify).
See code below.


I would greatly appreciate any advice.

Regards,
Andre'





void __fastcall Tcdrtelnet::Timer2Timer(TObject *Sender)
{
// This timer is used to send requests to the buffer box(es) for data and to 
control connects and disconnects
  AnsiString branch_string;
  AnsiString branch_ip;
  int pos;

  Timer2->Enabled = false;

  try
  {
    try
    {
      Timer2Counter++;
      EventLogger.logEvent(ET_Info, "Timer2Counter = " + 
IntToStr(Timer2Counter));

      //first we check some stuff once a second

       --snip--

      if (do_count)
      {
        counter++;
        if (state == 10)
        {
          if (counter >= 10)
          {
            try
            {
              EventLogger.logEvent(ET_Info,"No data recieved for 10 counts, 
setting state from 10 to 0");
              Sleep(500);//wait to make sure network coms is done
              MyDisconnect();   //<-----   this is where everything hangs 
when things go wrong
              EventLogger.logEvent(ET_Info, "Closing and renaming file 7: " 
+ filename);
              if (OutStream)
              {
                CloseAndRenameFile(filename);
              }
              state = 0;
              record_state = 0;
            }
            catch(Exception &E)
            {
              EventLogger.logEvent(ET_Error, "Exception occurred on no more 
data: " + E.Message);
            }
          }
        }
      }

      //now we can can carry on with the normal timed events
      //check if its time to connect and request data
      EventLogger.logEvent(ET_Info, "Cycle_Interval = " + 
FloatToStr(Cycle_Interval));
      if (Timer2Counter < Cycle_Interval)
      {//next timer...
        return;
      }
      Timer2Counter = 0;

      NoTimerCounter = 0;
      EventLogger.logEvent(ET_System, "Timer2Timer");
      bool Send_Request = false;


      //here I would retrieve any data from the incoming queue and process 
it
      --snip--

      //here we go and connect to each of the buffer boxes in turn and poll 
each one

      //setup the buffer box connection info
      --snip-- 

       CreateTelnet(branch_ip); //create the new instance of the telnet 
component

       --snip--

          try
          {
            IdTelnet2->Connect();
            AnsiString asMessage = "Connected to: " + branch_name + " " + 
branch_ip;
            timer_state = 2;
          }
          catch(...)
          {
            AnsiString asMessage = "Failed to connect to: " + branch_name + 
" " + branch_ip;
            EventLogger.logEvent(ET_Error, asMessage);
          }

        --snip--

        IdTelnet2->WriteLn("send 99");  //send the request, this asks for up 
to 99 lines of data
        state = 10; //sent a record request

        --snip--

    }
    catch(Exception &E)
    {
      EventLogger.logEvent(ET_Info, "Exception occurred (Timer2Timer): " + 
E.Message);
    }
  }
  __finally
  {
    Timer2->Enabled = true;
    EventLogger.logEvent(ET_Info,"Timer2 Finally");
  }
}//Timer2Timer
//---------------------------------------------------------------------------



void __fastcall Tcdrtelnet::IdTelnet1DataAvailable( TIdTelnet *Sender,
                                                    const AnsiString Buffer)
{
  try
  {
    counter = 0;
    AnsiString * pTTBuffer;
    pTTBuffer = new AnsiString;
    *pTTBuffer = Buffer; //copy the content over into a new AnsiString for 
insertion into the queue
    Incoming_Queue->Add((void *)pTTBuffer);
    Timer2Counter = Cycle_Interval - 1; //give me a second and then fire the 
timer
  }
  catch(Exception &E)
  {
    EventLogger.logEvent(ET_Error, "Exception occurred 
(IDTelnet1DataAvailable): " + E.Message);
  }
  return;
}//IdTelnet1DataAvailable
//---------------------------------------------------------------------------


void __fastcall Tcdrtelnet::MyDisconnect()
{
  try
  {
    if (IdTelnet2->Connected())
    {
      IdTelnet2->Disconnect();  // <-this is where it all hangs
    }
  }
  catch(Exception &E)
  {
    EventLogger.logEvent(ET_Error, "Exception occurred (MyDisconnect): " + 
E.Message);
  }
}//MyDisconnect
//---------------------------------------------------------------------------


void __fastcall Tcdrtelnet::CreateTelnet(AnsiString branch_ip)
{
  try
  {
    EventLogger.logEvent(ET_System, "Creating new IdTelnet2");
    if (IdTelnet2)
    {
      if (IdTelnet2->Connected())
      {
        IdTelnet2->Disconnect();
      }
      delete IdTelnet2;
    }
    IdTelnet2 = new TIdTelnet(NULL);
    IdTelnet2->Host = branch_ip;
    IdTelnet2->OnDataAvailable = IdTelnet1DataAvailable;
    IdTelnet2->ThreadedEvent = true;
  }
  catch(Exception &E)
  {
    EventLogger.logEvent(ET_Error, "Exception occurred (CreateTelnet): " + 
E.Message);
  }
}//CreateTelnet
//---------------------------------------------------------------------------


"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:44f6917c$1@newsgroups.borland.com...
>
> "Andre' Burke" <no.spam@spam.no> wrote in message
> news:44f68cef@newsgroups.borland.com...
>
>> It will run just fine for quite a while (I connect and disconnect a lot)
>> and then suddenly it will stop recieving data (I know this because I
>> have another test app actring as the telnet server and it sends the data)
>> and when I try and disconnect the entire app just stops.
>
> You have likely deadlocked the Telnet component.  It is a multi-threaded
> component, so you have to be careful with it.  Please show your actual 
> code.
>
>
> Gambit
>
> 
Technical Details
Message-ID: <44f69c27@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.code-werk.net!open-news-network.org!news.soft-gems.net!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail
References: <44f68cef@newsgroups.borland.com> <44f6917c$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3d66693bd859a203
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Andre' Burke" <no.spam@spam.no>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <44f68cef@newsgroups.borland.com> <44f6917c$1@newsgroups.borland.com>
Subject: Re: help with Indy Telnet
Date: Thu, 31 Aug 2006 10:21:56 +0200
Lines: 259
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 196.38.124.67
Message-ID: <44f69c27@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1157012519 196.38.124.67 (31 Aug 2006 01:21:59 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!news.code-werk.net!open-news-network.org!news.soft-gems.net!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail