Skip to content

Commit 150c9ca

Browse files
committed
added ServerPipe and ServerRouter interfaces
1 parent 9cd3dd4 commit 150c9ca

20 files changed

+348
-71
lines changed

src/common/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ add_cyphal_library(
2121

2222
add_library(ocvsmd_common
2323
ipc/client_router.cpp
24-
ipc/unix_socket_client.cpp
25-
ipc/unix_socket_server.cpp
24+
ipc/pipe/unix_socket_client.cpp
25+
ipc/pipe/unix_socket_server.cpp
26+
ipc/server_router.cpp
2627
)
2728
target_link_libraries(ocvsmd_common
2829
PUBLIC ${common_transpiled}

src/common/ipc/client_router.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// SPDX-License-Identifier: MIT
44
//
55

6-
#include "client_pipe.hpp"
76
#include "client_router.hpp"
87

8+
#include "pipe/client_pipe.hpp"
9+
910
#include <cetl/cetl.hpp>
1011

1112
#include <memory>
@@ -23,20 +24,20 @@ namespace
2324
class ClientRouterImpl final : public ClientRouter
2425
{
2526
public:
26-
explicit ClientRouterImpl(ClientPipe::Ptr client_pipe)
27+
explicit ClientRouterImpl(pipe::ClientPipe::Ptr client_pipe)
2728
: client_pipe_{std::move(client_pipe)}
2829
{
2930
CETL_DEBUG_ASSERT(client_pipe_, "");
3031
}
3132

3233
private:
33-
ClientPipe::Ptr client_pipe_;
34+
pipe::ClientPipe::Ptr client_pipe_;
3435

3536
}; // ClientRouterImpl
3637

3738
} // namespace
3839

39-
ClientRouter::Ptr ClientRouter::make(ClientPipe::Ptr client_pipe)
40+
ClientRouter::Ptr ClientRouter::make(pipe::ClientPipe::Ptr client_pipe)
4041
{
4142
return std::make_unique<ClientRouterImpl>(std::move(client_pipe));
4243
}

src/common/ipc/client_router.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED
77
#define OCVSMD_COMMON_IPC_CLIENT_ROUTER_HPP_INCLUDED
88

9-
#include "client_pipe.hpp"
9+
#include "pipe/client_pipe.hpp"
1010

1111
#include <memory>
1212

@@ -22,7 +22,7 @@ class ClientRouter
2222
public:
2323
using Ptr = std::unique_ptr<ClientRouter>;
2424

25-
static Ptr make(ClientPipe::Ptr client_pipe);
25+
static Ptr make(pipe::ClientPipe::Ptr client_pipe);
2626

2727
ClientRouter(ClientRouter&&) = delete;
2828
ClientRouter(const ClientRouter&) = delete;
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace common
1919
{
2020
namespace ipc
2121
{
22-
22+
namespace pipe
23+
{
2324
class ClientPipe
2425
{
2526
public:
@@ -60,6 +61,7 @@ class ClientPipe
6061

6162
}; // ClientPipe
6263

64+
} // namespace pipe
6365
} // namespace ipc
6466
} // namespace common
6567
} // namespace ocvsmd
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
// SPDX-License-Identifier: MIT
4+
//
5+
6+
#ifndef OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED
7+
#define OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED
8+
9+
#include <cetl/pf17/cetlpf.hpp>
10+
#include <cetl/pf20/cetlpf.hpp>
11+
12+
#include <cstddef>
13+
#include <cstdint>
14+
#include <functional>
15+
#include <memory>
16+
17+
namespace ocvsmd
18+
{
19+
namespace common
20+
{
21+
namespace ipc
22+
{
23+
namespace pipe
24+
{
25+
26+
class ServerPipe
27+
{
28+
public:
29+
using Ptr = std::unique_ptr<ServerPipe>;
30+
31+
using ClientId = std::size_t;
32+
using Payload = cetl::span<const std::uint8_t>;
33+
34+
struct Event
35+
{
36+
struct Connected
37+
{
38+
ClientId client_id;
39+
};
40+
struct Disconnected
41+
{
42+
ClientId client_id;
43+
};
44+
struct Message
45+
{
46+
ClientId client_id;
47+
Payload payload;
48+
49+
}; // Message
50+
51+
using Var = cetl::variant<Message, Connected, Disconnected>;
52+
53+
}; // Event
54+
55+
using EventHandler = std::function<int(const Event::Var&)>;
56+
57+
ServerPipe(ServerPipe&&) = delete;
58+
ServerPipe(const ServerPipe&) = delete;
59+
ServerPipe& operator=(ServerPipe&&) = delete;
60+
ServerPipe& operator=(const ServerPipe&) = delete;
61+
62+
virtual ~ServerPipe() = default;
63+
64+
virtual int start(EventHandler event_handler) = 0;
65+
virtual int sendMessage(const ClientId client_id, const Payload payload) = 0;
66+
67+
protected:
68+
ServerPipe() = default;
69+
70+
}; // ServerPipe
71+
72+
} // namespace pipe
73+
} // namespace ipc
74+
} // namespace common
75+
} // namespace ocvsmd
76+
77+
#endif // OCVSMD_COMMON_IPC_SERVER_PIPE_HPP_INCLUDED
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,21 @@ namespace common
2626
{
2727
namespace ipc
2828
{
29+
namespace pipe
30+
{
2931

3032
class UnixSocketBase
3133
{
34+
public:
35+
UnixSocketBase(UnixSocketBase&&) = delete;
36+
UnixSocketBase(const UnixSocketBase&) = delete;
37+
UnixSocketBase& operator=(UnixSocketBase&&) = delete;
38+
UnixSocketBase& operator=(const UnixSocketBase&) = delete;
39+
3240
protected:
41+
UnixSocketBase() = default;
42+
~UnixSocketBase() = default;
43+
3344
static int sendMessage(const int output_fd, const cetl::span<const std::uint8_t> payload)
3445
{
3546
// 1. Write the message header.
@@ -127,6 +138,7 @@ class UnixSocketBase
127138

128139
}; // UnixSocketBase
129140

141+
} // namespace pipe
130142
} // namespace ipc
131143
} // namespace common
132144
} // namespace ocvsmd

src/common/ipc/unix_socket_client.cpp renamed to src/common/ipc/pipe/unix_socket_client.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <algorithm>
1616
#include <cstddef>
1717
#include <cstring>
18-
#include <functional>
1918
#include <iostream>
2019
#include <string>
2120
#include <sys/socket.h>
@@ -29,6 +28,8 @@ namespace common
2928
{
3029
namespace ipc
3130
{
31+
namespace pipe
32+
{
3233

3334
UnixSocketClient::UnixSocketClient(libcyphal::IExecutor& executor, std::string socket_path)
3435
: socket_path_{std::move(socket_path)}
@@ -123,6 +124,7 @@ void UnixSocketClient::handle_socket()
123124
}
124125
}
125126

127+
} // namespace pipe
126128
} // namespace ipc
127129
} // namespace common
128130
} // namespace ocvsmd

src/common/ipc/unix_socket_client.hpp renamed to src/common/ipc/pipe/unix_socket_client.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace common
2121
{
2222
namespace ipc
2323
{
24+
namespace pipe
25+
{
2426

2527
class UnixSocketClient final : public UnixSocketBase, public ClientPipe
2628
{
@@ -50,10 +52,11 @@ class UnixSocketClient final : public UnixSocketBase, public ClientPipe
5052
int client_fd_;
5153
platform::IPosixExecutorExtension* const posix_executor_ext_;
5254
libcyphal::IExecutor::Callback::Any socket_callback_;
53-
std::function<int(const Event::Var&)> event_handler_;
55+
EventHandler event_handler_;
5456

5557
}; // UnixSocketClient
5658

59+
} // namespace pipe
5760
} // namespace ipc
5861
} // namespace common
5962
} // namespace ocvsmd

src/common/ipc/unix_socket_server.cpp renamed to src/common/ipc/pipe/unix_socket_server.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <algorithm>
1616
#include <cstddef>
1717
#include <cstring>
18-
#include <functional>
1918
#include <memory>
2019
#include <string>
2120
#include <sys/socket.h>
@@ -30,6 +29,8 @@ namespace common
3029
{
3130
namespace ipc
3231
{
32+
namespace pipe
33+
{
3334
namespace
3435
{
3536

@@ -98,12 +99,12 @@ UnixSocketServer::~UnixSocketServer()
9899
}
99100
}
100101

101-
int UnixSocketServer::start(std::function<int(const ClientEvent::Var&)>&& client_event_handler)
102+
int UnixSocketServer::start(EventHandler event_handler)
102103
{
103104
CETL_DEBUG_ASSERT(server_fd_ == -1, "");
104105
CETL_DEBUG_ASSERT(client_event_handler, "");
105106

106-
client_event_handler_ = std::move(client_event_handler);
107+
event_handler_ = std::move(event_handler);
107108

108109
if (const auto err = platform::posixSyscallError([this] {
109110
//
@@ -188,14 +189,14 @@ void UnixSocketServer::handle_accept()
188189
client_id_to_fd_[new_client_id] = client_fd;
189190
client_fd_to_context_.emplace(client_fd, std::move(client_context));
190191

191-
client_event_handler_(ClientEvent::Connected{new_client_id});
192+
event_handler_(Event::Connected{new_client_id});
192193
}
193194

194195
void UnixSocketServer::handle_client_request(const ClientId client_id, const int client_fd)
195196
{
196197
if (const auto err = receiveMessage(client_fd, [this, client_id](const auto payload) {
197198
//
198-
return client_event_handler_(ClientEvent::Message{client_id, payload});
199+
return event_handler_(Event::Message{client_id, payload});
199200
}))
200201
{
201202
if (err == -1)
@@ -216,10 +217,11 @@ void UnixSocketServer::handle_client_request(const ClientId client_id, const int
216217
client_id_to_fd_.erase(client_id);
217218
client_fd_to_context_.erase(client_fd);
218219

219-
client_event_handler_(ClientEvent::Disconnected{client_id});
220+
event_handler_(Event::Disconnected{client_id});
220221
}
221222
}
222223

224+
} // namespace pipe
223225
} // namespace ipc
224226
} // namespace common
225227
} // namespace ocvsmd

0 commit comments

Comments
 (0)