Skip to content

Commit f381097

Browse files
committed
Add client -> server hello message
fixes #36
1 parent 9cafdde commit f381097

File tree

13 files changed

+385
-222
lines changed

13 files changed

+385
-222
lines changed

OTDIPC-TestClient/DarwinTransport.cpp

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,79 +7,86 @@
77
#include <print>
88
#include <string>
99

10+
#include <cerrno>
1011
#include <pwd.h>
1112
#include <sys/un.h>
12-
#include <cerrno>
1313
#include <sysdir.h>
1414
#include <wordexp.h>
1515

1616
#include "PosixSocket.hpp"
1717

18-
namespace
19-
{
20-
std::expected<std::filesystem::path, std::string> GetApplicationSupportPath() noexcept
21-
{
22-
char appSupport[PATH_MAX];
23-
{
24-
auto state = sysdir_start_search_path_enumeration(SYSDIR_DIRECTORY_APPLICATION_SUPPORT, SYSDIR_DOMAIN_MASK_USER);
25-
state = sysdir_get_next_search_path_enumeration(state, appSupport);
26-
}
18+
namespace {
19+
std::expected<std::filesystem::path, std::string>
20+
GetApplicationSupportPath() noexcept {
21+
char appSupport[PATH_MAX];
22+
{
23+
auto state = sysdir_start_search_path_enumeration(
24+
SYSDIR_DIRECTORY_APPLICATION_SUPPORT, SYSDIR_DOMAIN_MASK_USER);
25+
state = sysdir_get_next_search_path_enumeration(state, appSupport);
26+
}
2727

28-
if (!appSupport[0]) {
29-
return std::unexpected{"Failed to find Application Support directory"};
30-
}
28+
if (!appSupport[0]) {
29+
return std::unexpected {"Failed to find Application Support directory"};
30+
}
3131

32-
// Expand `~/Library/...` to `/Users/..../Library/...`
33-
wordexp_t p {};
34-
if (const auto ret = wordexp(appSupport, &p, WRDE_NOCMD); ret != 0) {
35-
return std::unexpected{std::format("wordexp failed: {} - {}", errno, strerror(errno))};
36-
}
37-
std::string expandedAppSupport;
38-
for (auto i = 0; i < p.we_wordc; ++i) {
39-
if (i == 0) {
40-
expandedAppSupport = p.we_wordv[i];
41-
} else {
42-
expandedAppSupport = std::format("{} {}", expandedAppSupport, p.we_wordv[i]);
43-
}
44-
}
45-
wordfree(&p);
46-
return std::filesystem::path{expandedAppSupport};
32+
// Expand `~/Library/...` to `/Users/..../Library/...`
33+
wordexp_t p {};
34+
if (const auto ret = wordexp(appSupport, &p, WRDE_NOCMD); ret != 0) {
35+
return std::unexpected {
36+
std::format("wordexp failed: {} - {}", errno, strerror(errno))};
37+
}
38+
std::string expandedAppSupport;
39+
for (auto i = 0; i < p.we_wordc; ++i) {
40+
if (i == 0) {
41+
expandedAppSupport = p.we_wordv[i];
42+
} else {
43+
expandedAppSupport
44+
= std::format("{} {}", expandedAppSupport, p.we_wordv[i]);
4745
}
46+
}
47+
wordfree(&p);
48+
return std::filesystem::path {expandedAppSupport};
4849
}
50+
}// namespace
4951

50-
std::expected<std::unique_ptr<Transport>, std::string> Transport::Open() noexcept
51-
{
52-
return DarwinTransport::Open();
52+
std::expected<std::unique_ptr<Transport>, std::string>
53+
Transport::Open() noexcept {
54+
return DarwinTransport::Open();
5355
}
5456

55-
std::expected<std::unique_ptr<Transport>, std::string> DarwinTransport::Open() noexcept
56-
{
57-
const auto appSupport = GetApplicationSupportPath();
58-
if (!appSupport) {
59-
return std::unexpected{appSupport.error()};
60-
}
61-
const auto path = GetSocketPath(*appSupport);
62-
if (!path)
63-
{
64-
return std::unexpected{path.error()};
65-
}
57+
std::expected<std::unique_ptr<Transport>, std::string>
58+
DarwinTransport::Open() noexcept {
59+
const auto appSupport = GetApplicationSupportPath();
60+
if (!appSupport) {
61+
return std::unexpected {appSupport.error()};
62+
}
63+
const auto path = GetSocketPath(*appSupport);
64+
if (!path) {
65+
return std::unexpected {path.error()};
66+
}
6667

67-
auto fd = PosixSocket::ConnectToUnixSocket(*path);
68-
if (!fd) {
69-
return std::unexpected{fd.error()};
70-
}
68+
auto fd = PosixSocket::ConnectToUnixSocket(*path);
69+
if (!fd) {
70+
return std::unexpected {fd.error()};
71+
}
72+
73+
std::println("Connected to unix domain socket: {}", path->string());
74+
return std::unique_ptr<DarwinTransport> {new DarwinTransport(*std::move(fd))};
75+
}
7176

72-
std::println("Connected to unix domain socket: {}", path->string());
73-
return std::unique_ptr<DarwinTransport>{new DarwinTransport(*std::move(fd))};
77+
std::expected<size_t, std::string> DarwinTransport::Read(
78+
void* buffer,
79+
const size_t bufferSize) noexcept {
80+
return PosixSocket::Read(*mFD, buffer, bufferSize);
7481
}
7582

76-
std::expected<size_t, std::string> DarwinTransport::Read(void* buffer, const size_t bufferSize) noexcept
77-
{
78-
return PosixSocket::Read(*mFD, buffer, bufferSize);
83+
std::expected<void, std::string> DarwinTransport::Write(
84+
const void* buffer,
85+
const size_t bufferSize) noexcept {
86+
return PosixSocket::Write(*mFD, buffer, bufferSize);
7987
}
8088

81-
DarwinTransport::DarwinTransport(unique_fd fd) noexcept : mFD(std::move(fd))
82-
{
89+
DarwinTransport::DarwinTransport(unique_fd fd) noexcept : mFD(std::move(fd)) {
8390
}
8491

8592
DarwinTransport::~DarwinTransport() = default;

OTDIPC-TestClient/DarwinTransport.hpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,20 @@
1010
#include <memory>
1111
#include <string>
1212

13-
class DarwinTransport final : public Transport
14-
{
15-
public:
16-
DarwinTransport() = delete;
17-
~DarwinTransport() override;
13+
class DarwinTransport final : public Transport {
14+
public:
15+
DarwinTransport() = delete;
16+
~DarwinTransport() override;
1817

19-
static std::expected<std::unique_ptr<Transport>, std::string> Open() noexcept;
20-
std::expected<size_t, std::string> Read(void* buffer, size_t bufferSize) noexcept override;
18+
static std::expected<std::unique_ptr<Transport>, std::string> Open() noexcept;
19+
std::expected<size_t, std::string> Read(
20+
void* buffer,
21+
size_t bufferSize) noexcept override;
22+
std::expected<void, std::string> Write(
23+
void const* buffer,
24+
size_t bufferSize) noexcept override;
2125

22-
private:
23-
24-
explicit DarwinTransport(unique_fd fd) noexcept;
25-
unique_fd mFD{};
26+
private:
27+
explicit DarwinTransport(unique_fd fd) noexcept;
28+
unique_fd mFD {};
2629
};

OTDIPC-TestClient/LinuxTransport.cpp

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,66 @@
77
#include <print>
88
#include <string>
99

10+
#include <cerrno>
1011
#include <pwd.h>
1112
#include <sys/un.h>
12-
#include <cerrno>
1313
#include "PosixSocket.hpp"
1414

15+
namespace {
16+
std::expected<std::filesystem::path, std::string>
17+
GetApplicationSupportPath() noexcept {
18+
if (const auto dataDir = getenv("XDG_DATA_HOME")) {
19+
return dataDir;
20+
}
1521

16-
namespace
17-
{
18-
std::expected<std::filesystem::path, std::string> GetApplicationSupportPath() noexcept
19-
{
20-
if (const auto dataDir = getenv("XDG_DATA_HOME"))
21-
{
22-
return dataDir;
23-
}
22+
auto home = getenv("HOME");
23+
if (!home) {
24+
home = getpwuid(getuid())->pw_dir;
25+
}
2426

25-
auto home = getenv("HOME");
26-
if (!home) {
27-
home = getpwuid(getuid())->pw_dir;
28-
}
29-
30-
return std::filesystem::path{home} / ".local" / "share";
31-
}
27+
return std::filesystem::path {home} / ".local" / "share";
3228
}
29+
}// namespace
3330

34-
std::expected<std::unique_ptr<Transport>, std::string> Transport::Open() noexcept
35-
{
36-
return LinuxTransport::Open();
31+
std::expected<std::unique_ptr<Transport>, std::string>
32+
Transport::Open() noexcept {
33+
return LinuxTransport::Open();
3734
}
3835

39-
std::expected<std::unique_ptr<Transport>, std::string> LinuxTransport::Open() noexcept
40-
{
41-
const auto appSupport = GetApplicationSupportPath();
42-
if (!appSupport)
43-
{
44-
return std::unexpected{appSupport.error()};
45-
}
46-
const auto path = GetSocketPath(*appSupport);
47-
if (!path)
48-
{
49-
return std::unexpected{path.error()};
50-
}
51-
52-
auto fd = PosixSocket::ConnectToUnixSocket(*path);
53-
if (!fd) {
54-
return std::unexpected{fd.error()};
55-
}
56-
57-
std::println("Connected to unix socket at `{}`", path->string());
36+
std::expected<std::unique_ptr<Transport>, std::string>
37+
LinuxTransport::Open() noexcept {
38+
const auto appSupport = GetApplicationSupportPath();
39+
if (!appSupport) {
40+
return std::unexpected {appSupport.error()};
41+
}
42+
const auto path = GetSocketPath(*appSupport);
43+
if (!path) {
44+
return std::unexpected {path.error()};
45+
}
46+
47+
auto fd = PosixSocket::ConnectToUnixSocket(*path);
48+
if (!fd) {
49+
return std::unexpected {fd.error()};
50+
}
51+
52+
std::println("Connected to unix socket at `{}`", path->string());
53+
54+
return std::unique_ptr<LinuxTransport> {new LinuxTransport(*std::move(fd))};
55+
}
5856

59-
return std::unique_ptr<LinuxTransport>{new LinuxTransport(*std::move(fd))};
57+
std::expected<size_t, std::string> LinuxTransport::Read(
58+
void* buffer,
59+
const size_t bufferSize) noexcept {
60+
return PosixSocket::Read(*mFD, buffer, bufferSize);
6061
}
6162

62-
std::expected<size_t, std::string> LinuxTransport::Read(void *buffer, const size_t bufferSize) noexcept
63-
{
64-
return PosixSocket::Read(*mFD, buffer, bufferSize);
63+
std::expected<void, std::string> LinuxTransport::Write(
64+
const void* buffer,
65+
const size_t bufferSize) noexcept {
66+
return PosixSocket::Write(*mFD, buffer, bufferSize);
6567
}
6668

67-
LinuxTransport::LinuxTransport(unique_fd fd) noexcept : mFD(std::move(fd))
68-
{
69+
LinuxTransport::LinuxTransport(unique_fd fd) noexcept : mFD(std::move(fd)) {
6970
}
7071

7172
LinuxTransport::~LinuxTransport() = default;
72-

OTDIPC-TestClient/LinuxTransport.hpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,26 @@
55
#include "Transport.hpp"
66

77
#include <expected>
8-
#include <utility>
98
#include <memory>
109
#include <string>
10+
#include <utility>
1111

1212
#include "unique_fd.hpp"
1313

14-
class LinuxTransport final : public Transport
15-
{
16-
public:
17-
LinuxTransport() = delete;
18-
~LinuxTransport() final;
14+
class LinuxTransport final : public Transport {
15+
public:
16+
LinuxTransport() = delete;
17+
~LinuxTransport() final;
1918

20-
static std::expected<std::unique_ptr<Transport>, std::string> Open() noexcept;
21-
std::expected<size_t, std::string> Read(void *buffer, size_t bufferSize) noexcept override;
19+
static std::expected<std::unique_ptr<Transport>, std::string> Open() noexcept;
20+
std::expected<size_t, std::string> Read(
21+
void* buffer,
22+
size_t bufferSize) noexcept override;
23+
std::expected<void, std::string> Write(
24+
void const* buffer,
25+
size_t bufferSize) noexcept override;
2226

23-
private:
24-
explicit LinuxTransport(unique_fd fd) noexcept;
25-
unique_fd mFD{};
27+
private:
28+
explicit LinuxTransport(unique_fd fd) noexcept;
29+
unique_fd mFD {};
2630
};

OTDIPC-TestClient/OTDIPC-TestClient.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,18 @@ int main() {
237237
static_assert(sizeof(buffer) >= sizeof(State));
238238
const auto header = reinterpret_cast<const Header* const>(buffer);
239239

240+
{
241+
const auto dbgMessage = reinterpret_cast<DebugMessage*>(buffer);
242+
*dbgMessage = {};
243+
constexpr std::string_view kMessage = "Hello from OTDIPC-TestClient";
244+
dbgMessage->header.size = sizeof(Header) + kMessage.size();
245+
memcpy(dbgMessage->data, kMessage.data(), kMessage.size());
246+
if (const auto written = (*connection)->Write(buffer, dbgMessage->header.size); !written) {
247+
std::cerr << "Failed to write client hello: " << written.error() << std::endl;
248+
return EXIT_FAILURE;
249+
}
250+
}
251+
240252
const ScopedAlternateBuffer useAlternateBuffer;
241253

242254
while (true) {

0 commit comments

Comments
 (0)