LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Serialize TCP Reference Usage?

Solved!
Go to solution

Do I need to serialize the usage of my TCP reference?  I.e. Do I need to control the access (using semaphores) to my TCP reference when using the reference in multiple processes?  Does the answer change when only considering read versus write?

 

I realize that I can create a queue and process to manage all my writes to TCP but that method does not work in my situation.

0 Kudos
Message 1 of 6
(3,479 Views)

It isn't the write only TCP that would be a problem.  The problem I can see is the write and then get a response.  You could be interrupting your querry.  And speaking from experience, this is an issue.  My recommendation is to create an Action Engine that does your write and read.  The actions I can think of that you will want are Initialize, Write, Querry (does the write and read), Read, and Close.  This Action Engine works as your semephore and you do not have to pass the TCP reference around since it can just be stored in the Action Engine.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 6
(3,474 Views)
Solution
Accepted by topic author NathanJD

You do not need to serialize access to TCP Write, so long as you don't care about the order in which messages are sent. You won't get pieces from two different writes interleaved with each other. However, as Crossrulz mentioned, you will have a problem with TCP Read if you need to match sent messages with received messages.

 

If you are not using a command-response architecture, it might be reasonable to put TCP Writes wherever needed, but do all TCP Reads in a single loop, and whenever a status update comes through, send it to all interested parts of the code through a notifier or a functional global variable. The only problem with this is that if the TCP connection dies, and you need to reconnect, then you'll need some way to update the TCP refnum everywhere.

0 Kudos
Message 3 of 6
(3,468 Views)

In my application, there are only reads and writes, no querying i.e. I write a stream of data and receive a stream of different data.

 

What I'm currently writing is a hardware abstraction layer using LVOOP with DVRs as my private data containing the references (one device is based on TCP and others are typically going to be device specific DLLs).  I actually just realized that my current code already handles multiple writes because my "Send Message" method is non-reentrant and is the only method used for writing via TCP.  But, that still means that I could have reads and writes occuring at the same time since they are different methods of the class.

0 Kudos
Message 4 of 6
(3,461 Views)

@nathand wrote:

If you are not using a command-response architecture, it might be reasonable to put TCP Writes wherever needed, but do all TCP Reads in a single loop, and whenever a status update comes through, send it to all interested parts of the code through a notifier or a functional global variable. The only problem with this is that if the TCP connection dies, and you need to reconnect, then you'll need some way to update the TCP refnum everywhere.


 

This basically describes my use-case perfectly (order only matters in that M2 must come after M1 but the messages need not be sequential; e.g. CAN communication).  Handling my reference is my LVOOP-based hardware abstraction layer with DVR private data.

 

Thanks!

0 Kudos
Message 5 of 6
(3,457 Views)

NathanJD wrote:

But, that still means that I could have reads and writes occuring at the same time since they are different methods of the class.


That's fine, TCP is full-duplex communication. The TCP stack will not have a problem reading and writing at the same time (the operating system may handle it by serializing access at a low level - but you don't need to care about that). As previously mentioned you do want to avoid multiple reads at the same time since you have no idea which one will complete first. If your read follows the common pattern of reading a length followed by the actual data it could be especially problematic.

0 Kudos
Message 6 of 6
(3,454 Views)