Skip to content

Commit

Permalink
update pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Sep 20, 2024
1 parent 045d812 commit fabe69f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
10 changes: 8 additions & 2 deletions include/simple_socket/Pipe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
#include <vector>

class NamedPipe {

struct PassKey {
friend class NamedPipe;
};

public:
NamedPipe();
explicit NamedPipe(PassKey passKey);

NamedPipe(NamedPipe&& other) noexcept;

Expand All @@ -17,7 +22,7 @@ class NamedPipe {

bool send(const std::string& message);

size_t receive(std::vector<unsigned char>& buffer);
int receive(std::vector<unsigned char>& buffer);

static std::unique_ptr<NamedPipe> listen(const std::string& name);

Expand All @@ -26,6 +31,7 @@ class NamedPipe {
~NamedPipe();

private:

struct Impl;
std::unique_ptr<Impl> pimpl_;
};
Expand Down
15 changes: 7 additions & 8 deletions src/simple_socket/Pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ struct NamedPipe::Impl {
}

static std::unique_ptr<NamedPipe> createPipe(HANDLE hPipe) {
auto pipe = std::make_unique<NamedPipe>();
auto pipe = std::make_unique<NamedPipe>(PassKey());
pipe->pimpl_->hPipe_ = hPipe;
return pipe;
}
};

NamedPipe::NamedPipe(): pimpl_(std::make_unique<Impl>()) {}
NamedPipe::NamedPipe(PassKey)
: pimpl_(std::make_unique<Impl>()) {}

NamedPipe::NamedPipe(NamedPipe&& other) noexcept: pimpl_(std::make_unique<Impl>()) {
other.pimpl_->hPipe_ = INVALID_HANDLE_VALUE;
Expand All @@ -42,15 +43,15 @@ bool NamedPipe::send(const std::string& message) {
return true;
}

size_t NamedPipe::receive(std::vector<unsigned char>& buffer) {
int NamedPipe::receive(std::vector<unsigned char>& buffer) {

DWORD bytesRead;
if (!ReadFile(pimpl_->hPipe_, buffer.data(), buffer.size() - 1, &bytesRead, nullptr)) {
std::cerr << "Failed to read from pipe. Error: " << GetLastError() << std::endl;
return -1;
}
buffer[bytesRead] = '\0';
return bytesRead;
return static_cast<int>(bytesRead);
}

std::unique_ptr<NamedPipe> NamedPipe::listen(const std::string& name) {
Expand All @@ -70,14 +71,11 @@ std::unique_ptr<NamedPipe> NamedPipe::listen(const std::string& name) {
return nullptr;
}

std::cout << "Waiting for client connection..." << std::endl;
if (!ConnectNamedPipe(hPipe, nullptr)) {
std::cerr << "Failed to connect pipe. Error: " << GetLastError() << std::endl;
return nullptr;
}

std::cout << "Pipe created successfully!" << std::endl;

return Impl::createPipe(hPipe);
}

Expand Down Expand Up @@ -120,4 +118,5 @@ std::unique_ptr<NamedPipe> NamedPipe::connect(const std::string& name, long time
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
NamedPipe::~NamedPipe() = default;

NamedPipe::~NamedPipe() = default;
2 changes: 1 addition & 1 deletion tests/integration/run_pipe_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

int main() {

auto conn = NamedPipe::connect("PingPongPipe", 500);
const auto conn = NamedPipe::connect("PingPongPipe", 500);

if (!conn) return 1;

Expand Down
3 changes: 2 additions & 1 deletion tests/integration/run_pipe_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

int main() {

auto conn = NamedPipe::listen("PingPongPipe");
std::cout << "Waiting for client connection..." << std::endl;
const auto conn = NamedPipe::listen("PingPongPipe");

if (!conn) return 1;

Expand Down

0 comments on commit fabe69f

Please sign in to comment.