Named pipe strategies with heap?

Hokai, so I have an application where I need IPC ... I think named pipes are the way to go because they are so easy to use.

In any case, my question is how to deal with dynamic memory using named pipes.

Let's say I have a class like:

class MyTestClass {
public:
    MyTestClass() { _data = new int(4); }

    int GetData() { return *_data; }
    int GetData2() { return _data2; }

private:
    int* _data;
    int _data2;
};

      

Now when I create a buffer full of objects MyTestClass

, then send them over the pipe, I obviously lose _data in the assignment process and get garbage. Is there some strategy for this that I should use? I can use value types for simple cases, but for many complex classes I need to use some kind of dynamic memory and I like pointers.

Or should I just take a look at shared memory usage? Thanks to

+2


source to share


2 answers


Both named pipes and shared memory have similar problems: you need to serialize the contents of the structure on the send side and deserialize the structure on the receiving side.

The serialization process is essentially the same whether you are using named pipes or shared memory. For inline pointers (like _data and _data2), you need to serialize the contents of the pointer sequentially.



There are many serialization strategies you can use, depending on how your structures are laid out in memory and how efficient your IPC is. Or you can use DCE RPC and let the RPC marshaling code handle the complexity for you.

+3


source


To send data over a named pipe, you must serialize (or marshal) the data at the sending end and deserialize (or discard) the data at the receiving end.

Sounds fishy as if you are just writing a copy of the bytes in the data structure. This is not good. You are not copying the allocated data (it is not stored between the first and last bytes of the data structure, but somewhere else) and you are copying a pointer ( _data

) from one computer (or process) to another, and the memory address in the local process is not guaranteed to be significant in friend.

Define yourself as a wired protocol (if it falls out, look at ASN.1 - no, think about it, don't get that desparate) that defines the layout of the data to be transmitted over the wire. Then execute the sender and receiver functions (or seralizer and deserializer). Or find some other code that does this already.



Also remember to deal with endian-ness - you have to determine which sequence of bytes is sent over the named pipe.

For example, you could define that the message sent consists of a 4-byte unsigned integer in network byte order specifying how many structures to follow, and each structure could be a sequence of 4 signed 4-byte integers for an array, followed by one signed 4-byte integer for _data2

(also sent in network byte order).

Note that choosing a named pipe as an IPC mechanism is largely irrelevant; if you are not using shared memory (essentially on the same machine) then the final problem needs to be solved, and even with shared memory you need to deal with serialization.

+1


source







All Articles