Article View: borland.public.cppbuilder.internet.socket
Article #2563Re: TIdTCPServer, writing to all clients (Indy + BCB6)
From: Boris Epel
Date: Mon, 27 Mar 2006 16:05
Date: Mon, 27 Mar 2006 16:05
90 lines
3755 bytes
3755 bytes
Remy Lebeau (TeamB) wrote:
> "Boris Epel" <boep777@yahoo.com> wrote in message
> news:44250f70$1@newsgroups.borland.com...
...skipped... Thanks for advice
>>is it legal(clever in my case) to use server threads only for reading from
>>the socket and to perform writing in other thread (which will process
> requests)?
>
> That depends on what those threads are going to be doing in the first place.
> Please provide more details.
>
>
> Gambit
The main job of server is to organize interaction of multiple clients
with the device (one, local to server).
ServerExecute function receives messages (encapsulated into SDataBlock )
the DecodeCommand functions decide about action and interacts with
device controlling thread. Interaction is fast, however the triggered
action can be long and reported much later.
The independent device controlling thread inform about all events
happends as long as they come.
class Server
{
queue<SDataBlock *> OutQueue; // Data to be sent from server
TIdTCPServer * ... // Indy server
Device * ... // Device
SServerWriteThread * // write thread
...
//Function to put messages to this queue
void Server::OutQueueAdd(SDataBlock *)
}
//TCPServer thread to receive messages from client
void __fastcall SServer::ServerExecute(TIdPeerThread *_th)
{
while(_th->Connection->Connected())
{
SDataBlock *block = new ... // allocation of memory for new data
block
_th->Connection->ReadBuffer(block->Header, 22);
if(block->DecodeHeader(block->Header, 22))
{
_th->Connection->ReadBuffer(block->Buffer, block->Size);
DecodeCommand(block);
}
else {/*recovery*/};
delete block;
}
}
// Function to convert messages to control events for device
// or to get some info from device
void SServer::DecodeCommand(SDataBlock *_bl)
{
switch(_bl->ID)
{
...
//in this case we have something to send to client
OutQueueAdd(_bl);
}
}
// Some call from Device controlling thread
Server->OutQueueAdd(new SDataBlock(...))
//Server write thread (created by me)
void __fastcall SServerWriteThread::Execute(void)
{
int SleepTime = 100;
while(!Terminated)
{
SDataBlock *bl = Server->GetOutQueueFirst();
if(bl)
{
... cycle over open connection threads
{
(*th)->Connection->WriteBuffer(bl->Header, 22, false);
(*th)->Connection->WriteBuffer(bl->Buffer, bl->Size, true);
}
delete bl;
Server->OutQueueDiscardFirst();
}
::Sleep(SleepTime);
};
}
Technical Details
Message-ID:
<44280d31@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<44250f70$1@newsgroups.borland.com> <4427065d$5@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,34e362c244f8763d X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 27 Mar 2006 18:05:09 +0200 From: Boris Epel <boep777@yahoo.com> User-Agent: Mozilla Thunderbird 1.0.7 (Windows/20050923) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.internet.socket Subject: Re: TIdTCPServer, writing to all clients (Indy + BCB6) References: <44250f70$1@newsgroups.borland.com> <4427065d$5@newsgroups.borland.com> In-Reply-To: <4427065d$5@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 212.144.127.227 Message-ID: <44280d31@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1143475505 212.144.127.227 (27 Mar 2006 08:05:05 -0700) Lines: 90 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail