🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems

#4029
From: Andy Blackmore
Date: Mon, 11 Apr 2005 09:40
71 lines
2998 bytes
Thanks for that info, that's just what I was looking for

IdTCPServer1->ThreadClass = __classid(TMyIdThread);

Andy


"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message 
news:4256bffa@newsgroups.borland.com...
>
> "Andy Blackmore" <andy@number18.co.uk> wrote in message
> news:425669ba@newsgroups.borland.com...
>
>> But I found that the variable listed here (Count) is global to all
> threads.
>
> Yes.
>
>> But I don't know how I link this new class into the IdTCPServer 
>> component.
>
> You need to assign the server's ThreadClass propety before activating the
> server, such as in the form's constructor.
>
>    __fastcall TFArt::TFArt(TComponent *Owner)
>        : TForm(Owner)
>    {
>        IdTCPServer1->ThreadClass = __classid(TMyIdThread);
>    }
>
> Then you can cast the TIdPeerThread pointer in your event handler:
>
>    void __fastcall TFArt::IdTCPServer1Execute(TIdPeerThread *AThread)
>    {
>        //...
>        static_cast<TMyIdThread*>(AThread)->Count++;
>        //...
>    }
>
> Alternatively, if the Count is the only value you want stored with each
> thread, you can make use of the Thread's existing Data property instead, 
> ie:
>
>    void __fastcall TFArt::IdTCPServer1Connect(TIdPeerThread *AThread)
>    {
>        AThread->Data = NULL;
>    }
>
>    void __fastcall TFArt::IdTCPServer1Disconnect(TIdPeerThread *AThread)
>    {
>        // IMPORTANT TO DO SO HERE!!!  The thread owns the Data property.
>        // If the property is not NULL when the thread terminates, it will
> treat the
>        // property value as an object and try to free it, which will crash
> since we are
>        // not storing actual objects in it to begin with.
>        AThread->Data = NULL;
>    }
>
>    void __fastcall TFArt::IdTCPServer1Execute(TIdPeerThread *AThread)
>    {
>        //...
>        int count = reinterpret_cast<int>(AThread->Data);
>        AThread->Data = reinterpret_cast<TObject*>(++count);
>        //...
>    }
>
>
> Gambit
>
> 
Technical Details
Message-ID: <425a460e@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <425669ba@newsgroups.borland.com> <4256bffa@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3ba22e438513e68b
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Andy Blackmore" <andy@number18.co.uk>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <425669ba@newsgroups.borland.com> <4256bffa@newsgroups.borland.com>
Subject: Re: TIdPeerThread / IdTCPServer1 Multi Treaded problems
Date: Mon, 11 Apr 2005 10:40:25 +0100
Lines: 73
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: 193.195.12.161
Message-ID: <425a460e@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1113212430 193.195.12.161 (11 Apr 2005 02:40:30 -0800)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail