Article View: borland.public.cppbuilder.internet.socket
Article #1977Re: Would Indy's TIdTCPServer have any problem with connections I never read from?
From: Remy Lebeau (TeamB)
Date: Thu, 31 Aug 2006 17:27
Date: Thu, 31 Aug 2006 17:27
201 lines
6602 bytes
6602 bytes
"Lesley Anne" <mspfila-ng@yahoo.com> wrote in message
news:44f70595$1@newsgroups.borland.com...
> now it looks like I'm going to have to change my OnException and
> OnListenException handlers, if they take place within their respective
> threads as well.
Yes, they do. OnException is triggered in the context of the connection's
thread. OnListenException is triggered in the context of the thread that
TIdTCPServer creats for a particular Binding that is listening for incoming
connections.
> I've worked occasionally with secondary threads I wrote myself (as
> TThread descendants), so in that type of case I know how to send
> messages from a secondary thread back to my main thread using
> thread-global variables and Synchronize().
The same applies to Indy's threads as well. They are also TThread
descendants. Indy also provides other synchronization options, such as the
TIdSync and TIdNotify classes.
> I don't have access to adding variables to the thread class.
Actually, you do. In Indy 9, simply derive from TIdPeerThread and then set
the server's ThreadClass property before activating the server.
TIdPeerThread is a TThread descendant, so you have access to its
Synchronize() method. For example:
class TMyPeerThread : public TIdPeerThread
{
private:
AnsiString FStr;
void __fastcall DoSendStr();
public:
__fastcall TMyPeerThread(bool ACreateSuspended);
void __fastcall SendStr(const AnsiString &AStr);
};
__fastcall TMyPeerThread::TMyPeerThread(bool ACreateSuspended)
: TIdPeerThread(ACreateSuspended)
{
}
void __fastcall TMyPeerThread::SendStr(const AnsiString &AStr)
{
FStr := AStr;
Synchronize(DoSendStr);
}
void __fastcall TMyPeerThread::DoSendStr()
{
// use FStr as needed ...
}
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
IdTCPServer1->ThreadClass = __classid(TMyPeerThread);
}
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
//...
static_cast<TMyPeerThread*>(AThread)->SendStr("Hello World");
//...
}
In Indy 10, derive from TIdContext instead of TIdPeerThread. However,
TIdContext is not a TThread descendant, so you will have to use TIdSync when
you need to call Synchronize(). For example:
class TMyContext : public TIdContext
{
private:
AnsiString FStr;
void __fastcall DoSendStr;
public:
__fastcall TMyContext(TIdTCPConnection *AConnection, TIdYarn *AYarn,
TIdThreadList *AList);
void __fastcall SendStr(const AnsiString &AStr);
};
__fastcall TMyContext::TMyContext(TIdTCPConnection *AConnection, TIdYarn
*AYarn, TIdThreadList *AList)
: TIdContext(AConnection, AYarn, AList)
{
}
void __fastcall TMyContext::SendStr(const AnsiString &AStr)
{
FStr := AStr;
TIdSync::SynchronizeMethod(__classid(TIdSync), &DoSendStr);
}
void __fastcall TMyContext::DoSendStr()
{
// use FStr as needed ...
}
__fastcall TForm1::TForm1(TComponent *Owner)
: TForm(Owner)
{
IdTCPServer1->ContextClass = __classid(TMyContext);
}
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
//...
static_cast<TMyContext*>(AContext)->SendStr("Hello World");
//...
}
Alternatively, derive from TIdSync instead of TIdPeerThread/TIdContext, and
override the virtual DoSynchronize() method. This approach works (almost)
equally in both Indy 9 and 10. For example:
--- Indy 9 ---
class TMySync : public TIdSync
{
private:
AnsiString FStr;
public:
__fastcall TMySync(TIdThread *AThread, const AnsiString &AStr);
virtual void __fastcall DoSynchronize();
static void __fastcall SendStr(TIdThread *AThread, const AnsiString
&AStr);
};
__fastcall TMySync::TMySync(TIdThread *AThread, const AnsiString &AStr)
: TIdSync(AThread), FStr(AStr)
{
}
void __fastcall TMySync::SendStr(TIdThread *AThread, const AnsiString
&AStr)
{
TMySync *Sync = new TMySync(AThread, AStr);
try {
Sync->Synchronize();
}
__finally {
delete Sync;
}
}
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
//...
TMySync::SendStr(AThread, "Hello World");
//...
}
--- Indy 10 ---
class TMySync : public TIdSync
{
private:
AnsiString FStr;
protected:
virtual void __fastcall DoSynchronize();
public:
__fastcall TMySync(const AnsiString &AStr);
static void __fastcall SendStr(const AnsiString &AStr);
};
__fastcall TMySync::TMySync(const AnsiString &AStr)
: TIdSync(), FStr(AStr)
{
}
void __fastcall TMySync::SendStr(const AnsiString &AStr)
{
TMySync *Sync = new TMySync(AStr);
try {
Sync->Synchronize();
}
__finally {
delete Sync;
}
}
void __fastcall TForm1::IdTCPServer1Execute(TIdContext *AContext)
{
//...
TMySync::SendStr("Hello World");
//...
}
Gambit
Technical Details
Message-ID:
<44f71c6c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<44f480ff@newsgroups.borland.com> <44f49d01$1@newsgroups.borland.com> <44f5c67a$1@newsgroups.borland.com> <44f5e682$1@newsgroups.borland.com> <44f5ef4f@newsgroups.borland.com> <44f6068c$1@newsgroups.borland.com> <44f70595$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,c51ec25c5d4e68e1 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <44f480ff@newsgroups.borland.com> <44f49d01$1@newsgroups.borland.com> <44f5c67a$1@newsgroups.borland.com> <44f5e682$1@newsgroups.borland.com> <44f5ef4f@newsgroups.borland.com> <44f6068c$1@newsgroups.borland.com> <44f70595$1@newsgroups.borland.com> Subject: Re: Would Indy's TIdTCPServer have any problem with connections I never read from? Date: Thu, 31 Aug 2006 10:27:36 -0700 Lines: 203 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <44f71c6c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1157045356 68.69.17.41 (31 Aug 2006 10:29:16 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail