-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IPC via abstract unix domain sockets (#4)
- Added new "SDK" library target; added `ocvsmd-cli` executable target to consume the SDK. - Posix calls now are wrapped with `platform::posixSyscallError` (which does retries on `EINTR`). For now only IPC stuff correctly wrapped; the same for other places (like executors, udp sockets, etc.) to be continued... Also: - Now compiles on macOS by adding kqueue-based implementation of executor for Darwin. Thank you @thirtytwobits for sharing your original implementation (I did some fixes related to `kevent` timeout calculations - now it's **nano**seconds based instead of seconds).
- Loading branch information
Showing
26 changed files
with
1,007 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED | ||
#define OCVSMD_SDK_DAEMON_HPP_INCLUDED | ||
|
||
#include <memory> | ||
|
||
namespace ocvsmd | ||
{ | ||
namespace sdk | ||
{ | ||
|
||
/// An abstract factory for the specialized interfaces. | ||
/// | ||
class Daemon | ||
{ | ||
public: | ||
static std::unique_ptr<Daemon> make(); | ||
|
||
Daemon(Daemon&&) = delete; | ||
Daemon(const Daemon&) = delete; | ||
Daemon& operator=(Daemon&&) = delete; | ||
Daemon& operator=(const Daemon&) = delete; | ||
|
||
virtual ~Daemon() = default; | ||
|
||
virtual void send_messages() const = 0; | ||
|
||
protected: | ||
Daemon() = default; | ||
|
||
}; // Daemon | ||
|
||
} // namespace sdk | ||
} // namespace ocvsmd | ||
|
||
#endif // OCVSMD_SDK_DAEMON_HPP_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#include <ocvsmd/sdk/daemon.hpp> | ||
|
||
int main(const int argc, const char** const argv) | ||
{ | ||
(void) argc; | ||
(void) argv; | ||
|
||
if (auto daemon = ocvsmd::sdk::Daemon::make()) | ||
{ | ||
daemon->send_messages(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#include "unix_socket_client.hpp" | ||
|
||
#include "platform/posix_utils.hpp" | ||
|
||
#include <cetl/cetl.hpp> | ||
|
||
#include <algorithm> | ||
#include <array> | ||
#include <cstddef> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <string> | ||
#include <sys/socket.h> | ||
#include <sys/types.h> | ||
#include <sys/un.h> | ||
#include <unistd.h> | ||
#include <utility> | ||
|
||
namespace ocvsmd | ||
{ | ||
namespace common | ||
{ | ||
namespace ipc | ||
{ | ||
|
||
UnixSocketClient::UnixSocketClient(std::string socket_path) | ||
: socket_path_{std::move(socket_path)} | ||
, client_fd_{-1} | ||
{ | ||
} | ||
|
||
UnixSocketClient::~UnixSocketClient() | ||
{ | ||
if (client_fd_ != -1) | ||
{ | ||
platform::posixSyscallError([this] { | ||
// | ||
return ::close(client_fd_); | ||
}); | ||
} | ||
} | ||
|
||
bool UnixSocketClient::connect_to_server() | ||
{ | ||
CETL_DEBUG_ASSERT(client_fd_ == -1, ""); | ||
|
||
if (const auto err = platform::posixSyscallError([this] { | ||
// | ||
return client_fd_ = ::socket(AF_UNIX, SOCK_STREAM, 0); | ||
})) | ||
{ | ||
std::cerr << "Failed to create socket: " << ::strerror(err) << "\n"; | ||
return false; | ||
} | ||
|
||
sockaddr_un addr{}; | ||
addr.sun_family = AF_UNIX; | ||
const std::string abstract_socket_path = '\0' + socket_path_; | ||
CETL_DEBUG_ASSERT(abstract_socket_path.size() <= sizeof(addr.sun_path), ""); | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay) | ||
std::memcpy(addr.sun_path, | ||
abstract_socket_path.c_str(), | ||
std::min(sizeof(addr.sun_path), abstract_socket_path.size())); | ||
|
||
if (const auto err = platform::posixSyscallError([this, &addr, &abstract_socket_path] { | ||
// | ||
return ::connect(client_fd_, | ||
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) | ||
reinterpret_cast<const sockaddr*>(&addr), | ||
offsetof(struct sockaddr_un, sun_path) + abstract_socket_path.size()); | ||
})) | ||
{ | ||
std::cerr << "Failed to connect to server: " << ::strerror(err) << "\n"; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void UnixSocketClient::send_message(const std::string& message) const | ||
{ | ||
if (const auto err = platform::posixSyscallError([this, &message] { | ||
// | ||
return ::write(client_fd_, message.c_str(), message.size()); | ||
})) | ||
{ | ||
std::cerr << "Failed to write: " << ::strerror(err) << "\n"; | ||
return; | ||
} | ||
|
||
constexpr std::size_t buf_size = 256; | ||
std::array<char, buf_size> buffer{}; | ||
ssize_t bytes_read = 0; | ||
if (const auto err = platform::posixSyscallError([this, &bytes_read, &buffer] { | ||
// | ||
return bytes_read = ::read(client_fd_, buffer.data(), buffer.size() - 1); | ||
})) | ||
{ | ||
std::cerr << "Failed to read: " << ::strerror(err) << "\n"; | ||
return; | ||
} | ||
if (bytes_read > 0) | ||
{ | ||
buffer[bytes_read] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) | ||
std::cout << "Received: " << buffer.data() << "\n"; | ||
} | ||
} | ||
|
||
} // namespace ipc | ||
} // namespace common | ||
} // namespace ocvsmd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
#ifndef OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED | ||
#define OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED | ||
|
||
#include <string> | ||
|
||
namespace ocvsmd | ||
{ | ||
namespace common | ||
{ | ||
namespace ipc | ||
{ | ||
|
||
class UnixSocketClient final | ||
{ | ||
public: | ||
explicit UnixSocketClient(std::string socket_path); | ||
|
||
UnixSocketClient(UnixSocketClient&&) = delete; | ||
UnixSocketClient(const UnixSocketClient&) = delete; | ||
UnixSocketClient& operator=(UnixSocketClient&&) = delete; | ||
UnixSocketClient& operator=(const UnixSocketClient&) = delete; | ||
|
||
~UnixSocketClient(); | ||
|
||
bool connect_to_server(); | ||
void send_message(const std::string& message) const; | ||
|
||
private: | ||
std::string socket_path_; | ||
int client_fd_; | ||
|
||
}; // UnixSocketClient | ||
|
||
} // namespace ipc | ||
} // namespace common | ||
} // namespace ocvsmd | ||
|
||
#endif // OCVSMD_COMMON_IPC_UNIX_SOCKET_CLIENT_HPP_INCLUDED |
Oops, something went wrong.