Article View: borland.public.cppbuilder.internet.socket
Article #2017Re: Would Indy's TIdTCPServer have any problem with connections I never read from?
From: Joe Pasquariello
Date: Sun, 10 Sep 2006 18:04
Date: Sun, 10 Sep 2006 18:04
58 lines
4076 bytes
4076 bytes
Hi Lesley,
It sounds like you've already got something working, but I thought I would
toss in a comment regarding communication between your app's main thread and
secondary threads. The conversations between you and Remy have been very
useful and informative. I've learned a lot about Indy be reading them, and
you are doing similar things to what I want to do. I've long been using
threads to encapsulate serial comm connections, and I don't use
Synchronize() to pass things back and forth, but rather I pass pointers to
structures back and forth via standard WIN32 message queue functions. An
earlier posting by Remy said that you would need to create your own message
queues, but they actually sort of create themselves. All you have to do is
call a function to read from a queue, and WIN32 will create the queue for
you. The Execute() function below shows how this works. The main thread uses
PostMessage() to write to secondary threads' message queues, and the
secondary threads use NotifyMessage() to post back to the main thread's
message queue. This makes all of the communication asynchronous. The nested
if statements implement a simple priority queue based on the message ID. All
messages to the thread contain a Transaction pointer, which is just an
application-specific structure that contains source, destination, data, etc.
The main thread allocates and deallocates the Transactions. One thing to be
aware of is that your secondary threads need a little time (less than a
second) for WIN32 to create their queues before you write to them. If you
call PostMessage() before the queue exists, that message will be lost and
you won't get a reply from the secondary thread. I'm using this method in
many applications, and it's so reliable that there is no need to have
timeouts in the main thread. If you write to a secondary thread, and you
implement whatever timeouts you want within those threads, the main thread
is guaranteed to get a response containing either the data you want or a
timeout indication.
Best regards,
Joe
void __fastcall TMyThread::Execute()
{
MSG msg, tmp;
Transaction *t;
for (;;) {
msg.message = 0;
while (!msg.message) {
if (!PeekMessage(&msg, NULL, FromClient4, FromClient4, PM_REMOVE))
if (!PeekMessage(&msg, NULL, FromClient3, FromClient3, PM_REMOVE))
if (!PeekMessage(&msg, NULL, FromClient2, FromClient2, PM_REMOVE))
if (!PeekMessage(&msg, NULL, FromClient1, FromClient1,
PM_REMOVE))
if (!PeekMessage(&msg, NULL, FromClient0, FromClient0,
PM_REMOVE))
if (!PeekMessage(&tmp, NULL, FromClient0, FromClient4,
PM_NOREMOVE))
WaitMessage();
}
t = (Transaction*)msg.lParam;
DoThreadWord( t );
SendNotifyMessage( (void*)t->client, FromMyThread, NULL, (long)t );
}
}
Technical Details
Message-ID:
<450453cb$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!feeder.news-service.com!newsfeed.freenet.de!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: "Joe Pasquariello" <joep@fenway.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: Sun, 10 Sep 2006 11:04:47 -0700 Lines: 60 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1807 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1807 NNTP-Posting-Host: 71.131.40.62 Message-ID: <450453cb$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1157911499 71.131.40.62 (10 Sep 2006 11:04:59 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!news4.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!feeder.news-service.com!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail