Turi Create
4.0
|
#include <core/system/platform/shmipc/shmipc.hpp>
Public Member Functions | |
bool | bind (const std::string &ipcfile="", size_t buffer_size=1024 *1024) |
size_t | buffer_size () const |
std::string | get_shared_memory_name () const |
bool | wait_for_connect (size_t timeout=10) |
bool | send (const char *c, size_t len) |
bool | receive (char **c, size_t *clen, size_t &receivelen, size_t timeout) |
bool | receive_direct (char **c, size_t *len, size_t &receivelen, size_t timeout) |
void | shutdown () |
The SHM IPC server/client defines a simple unsynchronized single server / single client communication system over interprocess shared memory.
The communication is mostly unsynchronized between server and client, so users of the server/client implementations have to be careful about who is sending and who is receiving.
The class uses Posix Shared Memory segments. Essentially you define a "name" (on Linux this name shows up in /dev/shm, on Mac this is unfortunately not enumerable).
Within the shared memory segment is essentially a buffer, and a pair of condition variables used to wake a client receiver, or a server receiver.
The server creates a name, and a size and waits for a client to connect to it. Once a client connects, the shared memory segment is deleted (unlink). This means that once both server and client terminate (or crash), the shared memory segment is released. (otherwise the named segment will hang around until a reboot).
However, this does mean that program crash prior to connection can result in leaked segments. And that is bad. Hence we need a "garbage collection" mechanism.
Definition at line 49 of file shmipc.hpp.
bool turi::shmipc::server::bind | ( | const std::string & | ipcfile = "" , |
size_t | buffer_size = 1024 *1024 |
||
) |
Binds a server to an ipc name. This SHM name will show up in /dev/shm on linux machines. Every server must bind to a different file. If ipcfile is an empty string, a file is automatically constructed and get_shared_memory_name() can be used to get the shared memory name.
size_t turi::shmipc::server::buffer_size | ( | ) | const |
Returns the maximum amount of data that can be sent or received.
std::string turi::shmipc::server::get_shared_memory_name | ( | ) | const |
Returns the shared memory object name.
bool turi::shmipc::server::receive | ( | char ** | c, |
size_t * | clen, | ||
size_t & | receivelen, | ||
size_t | timeout | ||
) |
Receives a bunch of bytes into (*c) and (*clen). (*c) and (*clen) may be resized as required to fit the data. if c == nullptr and clen == nullptr, the return data is discarded.
bool turi::shmipc::server::receive_direct | ( | char ** | c, |
size_t * | len, | ||
size_t & | receivelen, | ||
size_t | timeout | ||
) |
receives a direct buffer to the data. It is up to the caller to make sure that no other sends/receives happen while the caller accesses the data.
bool turi::shmipc::server::send | ( | const char * | c, |
size_t | len | ||
) |
Sends a bunch of bytes.
void turi::shmipc::server::shutdown | ( | ) |
shutsdown the server
bool turi::shmipc::server::wait_for_connect | ( | size_t | timeout = 10 | ) |
Waits up to timeout seconds for a connection.