YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
ShmemPlatform.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef YARP_SHMEM_SHMEMPLATFORM_H
7#define YARP_SHMEM_SHMEMPLATFORM_H
8
9#include <cstddef>
10#include <cstdint>
11#include <string>
12
13/*
14 * Minimal portable wrappers (Win32 and POSIX) around the operating system
15 * primitives used by the shmem carrier: named shared memory segments, named
16 * inter-process semaphores and TCP sockets.
17 */
18namespace shmem {
19
27std::string objectName(const char* kind, int port, int index = -1);
28
33{
34public:
35 SharedMemory() = default;
37
38 SharedMemory(const SharedMemory&) = delete;
40
42 bool create(const std::string& name, size_t size);
44 bool open(const std::string& name);
46 void close();
48 static void unlink(const std::string& name);
49
50 void* data() const { return m_data; }
51
52private:
53 void* m_handle {nullptr};
54 void* m_data {nullptr};
55 size_t m_size {0};
56};
57
64{
65public:
66 NamedSemaphore() = default;
68
71
73 bool create(const std::string& name, unsigned int initial, unsigned int max);
75 bool open(const std::string& name);
76 void close();
78 static void unlink(const std::string& name);
79
81 bool wait();
83 bool waitFor(double timeout);
84 bool post();
85
86private:
87 void* m_handle {nullptr};
88};
89
93class Socket
94{
95public:
96 Socket() = default;
97 ~Socket();
98
99 Socket(const Socket&) = delete;
100 Socket& operator=(const Socket&) = delete;
101
103 bool listen(int port);
105 bool accept(Socket& stream);
106 bool connect(const std::string& host, int port);
107
109 bool sendAll(const void* data, size_t len);
111 bool recvAll(void* data, size_t len);
114 bool isPeerClosed();
115
116 bool setNonBlocking();
117
118 bool getLocalAddress(std::string& host, int& port) const;
119 bool getRemoteAddress(std::string& host, int& port) const;
120
121 void close();
122
123private:
124 std::intptr_t m_fd {-1};
125};
126
127} // namespace shmem
128
129#endif // YARP_SHMEM_SHMEMPLATFORM_H
A named inter-process semaphore.
NamedSemaphore(const NamedSemaphore &)=delete
bool waitFor(double timeout)
Wait at most timeout seconds. Returns true if the semaphore was acquired.
bool wait()
Wait until the semaphore is available.
static void unlink(const std::string &name)
Remove the name of the semaphore (no-op on Windows)
bool create(const std::string &name, unsigned int initial, unsigned int max)
Create a new semaphore (removing any stale semaphore with the same name)
bool open(const std::string &name)
Open an existing semaphore.
NamedSemaphore & operator=(const NamedSemaphore &)=delete
A named shared memory segment.
void * data() const
static void unlink(const std::string &name)
Remove the name of the segment (no-op on Windows)
void close()
Unmap the segment.
SharedMemory & operator=(const SharedMemory &)=delete
bool create(const std::string &name, size_t size)
Create a new segment (removing any stale segment with the same name)
SharedMemory(const SharedMemory &)=delete
SharedMemory()=default
bool open(const std::string &name)
Open an existing segment.
A minimal IPv4 TCP socket.
Socket & operator=(const Socket &)=delete
bool recvAll(void *data, size_t len)
Receive exactly len bytes (blocking socket)
bool accept(Socket &stream)
Accept a connection on a listening socket.
Socket()=default
bool connect(const std::string &host, int port)
bool listen(int port)
Listen on all the interfaces (port 0 means any free port)
bool isPeerClosed()
Check, on a non-blocking socket, whether the connection was closed or reset by the peer (e....
bool sendAll(const void *data, size_t len)
Send exactly len bytes (blocking socket)
Socket(const Socket &)=delete
bool getRemoteAddress(std::string &host, int &port) const
bool getLocalAddress(std::string &host, int &port) const
std::string objectName(const char *kind, int port, int index=-1)
Build the name of a named inter-process object.