BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

TIdTCPServer, writing to all clients (Indy + BCB6)

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket)
View previous topic :: View next topic  
Author Message
Boris Epel
Guest





PostPosted: Sat Mar 25, 2006 10:03 am    Post subject: TIdTCPServer, writing to all clients (Indy + BCB6) Reply with quote



Hello All,
I have to control the device remotely. Multiple clients are connected to
server, which interacts with device directly. Clients request
information from device on demand. Device executes timely procedures and
afterwards reports status.

Therefore server has to be able to send messages to all clients. I did
not realize how to do it using TIdTCPServer component (how to access
Connection from outside of the threads).

Connected question: 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)?

Boris
PS Thanks for previous replies!
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sun Mar 26, 2006 10:03 pm    Post subject: Re: TIdTCPServer, writing to all clients (Indy + BCB6) Reply with quote



"Boris Epel" <boep777 (AT) yahoo (DOT) com> wrote in message
news:44250f70$1 (AT) newsgroups (DOT) borland.com...

Quote:
Therefore server has to be able to send messages to all clients. I did
not realize how to do it using TIdTCPServer component (how to access
Connection from outside of the threads).

Use the server's Threads property, ie:

TList *List = IdTCPServer1->Threads->LockList();
try
{
for(int x = 0; x < Threads->Count; ++x)
{
TIdPeerThread *Thread = (TIdPeerThread*) List->Items[x];
// use Thread->Connection as needed ...
}
}
__finally
{
IdTCPServer1->Threads->UnlockList();
}

Quote:
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
Back to top
Boris Epel
Guest





PostPosted: Mon Mar 27, 2006 5:03 pm    Post subject: Re: TIdTCPServer, writing to all clients (Indy + BCB6) Reply with quote



Remy Lebeau (TeamB) wrote:
Quote:
"Boris Epel" <boep777 (AT) yahoo (DOT) com> wrote in message
news:44250f70$1 (AT) newsgroups (DOT) borland.com...

....skipped... Thanks for advice

Quote:
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);
};
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Internet Socket) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.