🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.internet.socket
3 messages
3 total messages Started by Boris Epel Sat, 25 Mar 2006 09:37
TIdTCPServer, writing to all clients (Indy + BCB6)
#2548
Author: Boris Epel
Date: Sat, 25 Mar 2006 09:37
16 lines
1581 bytes
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!
Re: TIdTCPServer, writing to all clients (Indy + BCB6)
#2556
Author: Remy Lebeau (TeamB)
Date: Sun, 26 Mar 2006 21:23
33 lines
1893 bytes
"Boris Epel" <boep777@yahoo.com> wrote in message
news:44250f70$1@newsgroups.borland.com...

> 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();
    }

> 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
Re: TIdTCPServer, writing to all clients (Indy + BCB6)
#2563
Author: Boris Epel
Date: Mon, 27 Mar 2006 16:05
90 lines
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);
    };
}
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads