 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marais Guest
|
Posted: Tue May 15, 2007 6:55 pm Post subject: Passing a connection reference in PostThreadMessage |
|
|
I'm using TIdTCPServer to build an application which accepts multiple
connections. Each client, after connecting, is to receive data from time to
time sent by the server. Some data will go to one client, some to another.
Commands from the clients tell the server which data they are interested in
receiving.
My model is that the command handlers use PostThreadMessage to tell the main
thread when a client has expressed an interest in receiving updates to which
data.
I was wondering how best to encode something identifying the client in the
PostThreadMessage call. There appears to be a longword parameter (is this
what lParam means?), so presumably I could use this to send a pointer to the
connection. I could then dereference this in the main thread and use methods
like Connection.writeln to send data to the appropriate client.
This seems rather clumsy. I'm afraid that my ignorance of pointers is
showing here. Is there a canonical way of doing this?
Peter |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed May 16, 2007 12:38 am Post subject: Re: Passing a connection reference in PostThreadMessage |
|
|
"Marais" <marais (AT) x (DOT) y> wrote in message
news:xZi2i.14687$Xh3.10698@edtnps90...
| Quote: | My model is that the command handlers use PostThreadMessage
to tell the main thread when a client has expressed an interest in
receiving updates to which data.
|
How about simply storing the data in a global thread-safe container,
and then let each client access the data directly when needed?
| Quote: | I was wondering how best to encode something identifying the
client in the PostThreadMessage call.
|
There is no unique ID provided by Indy, so you need to create your
own, and then associate it with each client as needed. You could use
the Data property of the TIdPeerThread (Indy 9) or TIdContext (Indy
10) object for storing that, and then loop through the server's
Threads (Indy 9) or Contexts (Indy 10) list when you need to find a
particular client.
| Quote: | There appears to be a longword parameter (is this what lParam
means?) |
Yes.
| Quote: | so presumably I could use this to send a pointer to the connection.
|
Technically, yes. But keep in mind that PostThreadMessage() is
asynchronous, so you would have to validate that the pointer is still
valid when you process the message before accessing the connection.
| Quote: | I could then dereference this in the main thread and use methods
like Connection.writeln to send data to the appropriate client.
|
That is not thread-safe, so you would have to provide your own locking
mechanism to ensure that the server can't read/write to a connection
at the same time that the main thread is.
| Quote: | This seems rather clumsy.
|
It can be, which leads to the question of why you are using
PostThreadMessage() to begin with. What does your overall design look
like?
Gambit |
|
| Back to top |
|
 |
Marais Guest
|
Posted: Wed May 16, 2007 4:09 am Post subject: Re: Passing a connection reference in PostThreadMessage |
|
|
| Quote: | This seems rather clumsy.
It can be, which leads to the question of why you are using
PostThreadMessage() to begin with. What does your overall design look
like?
|
There are three types of processes, which communicate using TCP.
P1: There are several of these. They establish TCP connections to P2,
gather data from the outside world (temperatures, pressures, on-off data)
and send them to P2. The data are sent to P2 asynchronously, i.e. without
prompting from P2.
P2: Just one of these, and this is the process I am working on. It accepts
data from the P1 processes and, based on various criteria, forwards them
asynchronously to the P3 processes. It accepts connections from both the P1
and the P3 processes.
P3: There are several of these. They connect to P2, tell P2 which data
streams they are interested in, and wait for P2 to send the data. The data
are then presented to the user, typically graphically.
So, P2 is sort of a "post office", accepting data from several processes and
then forwarding it on to other processes. The data arrive at P2
asynchronously, and are asynchronously sent to the users, who are running
the P3 processes. Each user is typically interested in a different set of
data points, so P2 needs to know what data points each P3 wants to receive.
My intention was to have a data structure in P2, managed by the main thread,
which was essentially an array of the various data points, along with a list
of TCP connections to send the data to. For example, if a P1 sends a new
value for, say, "main exhaust temperature", then the main thread would
examine this data structure and send the new value to every P3 which had
expressed an interest in "main exhaust temperature".
So, in this setup, the main thread has to have some way to send data to a
particular client and not others. The event is triggered by the arrival of
data from one of the P1 processes.
A lot of hand-waving here! If you can suggest some architecture which fits
more cleanly into the threaded model, I'd be very grateful.
Thanks
Peter |
|
| Back to top |
|
 |
Marais Guest
|
Posted: Wed May 16, 2007 6:21 pm Post subject: Re: Passing a connection reference in PostThreadMessage |
|
|
It's clear that I am wallowing in ignorance of thread programming in Delphi.
After a little searching, I discovered this excellent reference on the web:
http://www.eonclash.com/Tutorials/Multithreading/MartinHarvey1.1/ToC.html
which I intend to work through before going further with this project. If
you're interested in using Indy components in more than one thread, this
would probably be worth a look.
Regards to all
Marais |
|
| Back to top |
|
 |
|
|
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
|
|