Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
markaren committed Sep 24, 2024
1 parent c86b3ea commit 151dfd2
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 94 deletions.
13 changes: 5 additions & 8 deletions include/simple_socket/Pipe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace simple_socket {

class NamedPipe: SimpleConnection {
class NamedPipe: public SimpleConnection {

struct PassKey {
friend class NamedPipe;
Expand All @@ -23,17 +23,14 @@ namespace simple_socket {
NamedPipe(NamedPipe& other) = delete;
NamedPipe& operator=(NamedPipe& other) = delete;

int read(std::vector<unsigned char>& buffer) override;
int read(unsigned char* buffer, size_t size) override;
bool readExact(std::vector<unsigned char>& buffer) override;
bool readExact(unsigned char* buffer, size_t size) override;
bool write(const std::string& message) override;
bool write(const std::vector<unsigned char>& data) override;
void close() override;
bool write(const unsigned char* data, size_t size) override;

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

static std::unique_ptr<NamedPipe> connect(const std::string& name, long timeOut = 5000);
static std::unique_ptr<SimpleConnection> listen(const std::string& name);
static std::unique_ptr<SimpleConnection> connect(const std::string& name, long timeOut = 5000);

~NamedPipe() override;

Expand Down
37 changes: 25 additions & 12 deletions include/simple_socket/SimpleConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@
#ifndef SIMPLE_SOCKET_SOCKET_HPP
#define SIMPLE_SOCKET_SOCKET_HPP

#include <string>
#include <vector>

namespace simple_socket {

class SimpleConnection {
public:
virtual int read(std::vector<unsigned char>& buffer) = 0;

virtual int read(unsigned char* buffer, size_t size) = 0;

virtual bool readExact(std::vector<unsigned char>& buffer) = 0;

virtual bool readExact(unsigned char* buffer, size_t size) = 0;

virtual bool write(const std::string& message) = 0;

virtual bool write(const std::vector<unsigned char>& data) = 0;
virtual bool write(const unsigned char* data, size_t size) = 0;

template<class Container>
int read(Container& buffer) {
return read(buffer.data(), buffer.size());
}

template<class Container>
bool readExact(Container& buffer) {
return readExact(buffer.data(), buffer.size());
}

template<class Container>
bool write(const Container& data) {
return write(data.data(), data.size());
}

bool write(const char* data, size_t size) {
return write(reinterpret_cast<const unsigned char*>(data), size);
}

template<size_t N>
bool write(const char (&data)[N]) {
return write(reinterpret_cast<const unsigned char*>(data), N - 1);// N - 1 to exclude null terminator if it's a C-string
}

virtual void close() = 0;

Expand Down
1 change: 1 addition & 0 deletions include/simple_socket/TCPSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "simple_socket/SimpleConnection.hpp"

#include <memory>
#include <string>

namespace simple_socket {

Expand Down
2 changes: 2 additions & 0 deletions include/simple_socket/UnixDomainSocket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "simple_socket/SimpleConnection.hpp"

#include <memory>
#include <string>

namespace simple_socket {

Expand All @@ -15,6 +16,7 @@ namespace simple_socket {
std::unique_ptr<SimpleConnection> connect(const std::string& domain);

~UnixDomainClientContext();

private:
struct Impl;
std::unique_ptr<Impl> pimpl_;
Expand Down
33 changes: 5 additions & 28 deletions src/simple_socket/Pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ NamedPipe::NamedPipe(NamedPipe&& other) noexcept: pimpl_(std::make_unique<Impl>(
other.pimpl_->hPipe_ = INVALID_HANDLE_VALUE;
}

std::unique_ptr<NamedPipe> NamedPipe::listen(const std::string& name) {
std::unique_ptr<SimpleConnection> NamedPipe::listen(const std::string& name) {
const auto pipeName = R"(\\.\pipe\)" + name;
HANDLE hPipe = CreateNamedPipe(
pipeName.c_str(), // Pipe name
Expand All @@ -50,7 +50,7 @@ std::unique_ptr<NamedPipe> NamedPipe::listen(const std::string& name) {
512, // Output buffer size
512, // Input buffer size
0, // Client time-out
nullptr); // Default security attribute
nullptr); // Default security attribute

if (hPipe == INVALID_HANDLE_VALUE) {
std::cerr << "Failed to create pipe. Error: " << GetLastError() << std::endl;
Expand All @@ -65,7 +65,7 @@ std::unique_ptr<NamedPipe> NamedPipe::listen(const std::string& name) {
return Impl::createPipe(hPipe);
}

std::unique_ptr<NamedPipe> NamedPipe::connect(const std::string& name, long timeOut) {
std::unique_ptr<SimpleConnection> NamedPipe::connect(const std::string& name, long timeOut) {
const auto pipeName = R"(\\.\pipe\)" + name;
const auto start = std::chrono::steady_clock::now();
while (true) {
Expand Down Expand Up @@ -105,16 +105,6 @@ std::unique_ptr<NamedPipe> NamedPipe::connect(const std::string& name, long time
}
}

int NamedPipe::read(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 static_cast<int>(bytesRead);
}

int NamedPipe::read(unsigned char* buffer, size_t size) {
DWORD bytesRead;
if (!ReadFile(pimpl_->hPipe_, buffer, size - 1, &bytesRead, nullptr)) {
Expand All @@ -125,26 +115,13 @@ int NamedPipe::read(unsigned char* buffer, size_t size) {
return static_cast<int>(bytesRead);
}

bool NamedPipe::readExact(std::vector<unsigned char>& buffer) {
throw std::runtime_error("Unsupported operation");
}

bool NamedPipe::readExact(unsigned char* buffer, size_t size) {
throw std::runtime_error("Unsupported operation");
}

bool NamedPipe::write(const std::string& message) {
DWORD bytesWritten;
if (!WriteFile(pimpl_->hPipe_, message.c_str(), message.size(), &bytesWritten, nullptr)) {
std::cerr << "Failed to write to pipe. Error: " << GetLastError() << std::endl;
return false;
}
return true;
}

bool NamedPipe::write(const std::vector<unsigned char>& data) {
bool NamedPipe::write(const unsigned char* data, size_t size) {
DWORD bytesWritten;
if (!WriteFile(pimpl_->hPipe_, data.data(), data.size(), &bytesWritten, nullptr)) {
if (!WriteFile(pimpl_->hPipe_, data, size, &bytesWritten, nullptr)) {
std::cerr << "Failed to write to pipe. Error: " << GetLastError() << std::endl;
return false;
}
Expand Down
25 changes: 3 additions & 22 deletions src/simple_socket/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ namespace simple_socket {
explicit Socket(SOCKET socket)
: sockfd_(socket) {}

int read(std::vector<unsigned char>& buffer) override {

return read(buffer.data(), buffer.size());
}

int read(unsigned char* buffer, size_t size) override {

#ifdef _WIN32
Expand All @@ -41,11 +36,6 @@ namespace simple_socket {
return (read != SOCKET_ERROR) && (read != 0) ? read : -1;
}

bool readExact(std::vector<unsigned char>& buffer) override {

return readExact(buffer.data(), buffer.size());
}

bool readExact(unsigned char* buffer, size_t size) override {

size_t totalBytesReceived = 0;
Expand All @@ -66,21 +56,12 @@ namespace simple_socket {
return totalBytesReceived == size;
}

bool write(const std::string& buffer) override {

#ifdef _WIN32
return send(sockfd_, buffer.data(), static_cast<int>(buffer.size()), 0) != SOCKET_ERROR;
#else
return ::write(sockfd_, buffer.data(), buffer.size()) != SOCKET_ERROR;
#endif
}

bool write(const std::vector<unsigned char>& buffer) override {
bool write(const unsigned char* data, size_t size) override {

#ifdef _WIN32
return send(sockfd_, reinterpret_cast<const char*>(buffer.data()), static_cast<int>(buffer.size()), 0) != SOCKET_ERROR;
return send(sockfd_, reinterpret_cast<const char*>(data), static_cast<int>(size), 0) != SOCKET_ERROR;
#else
return ::write(sockfd_, buffer.data(), buffer.size()) != SOCKET_ERROR;
return ::write(sockfd_, data, size) != SOCKET_ERROR;
#endif
}

Expand Down
18 changes: 6 additions & 12 deletions src/simple_socket/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,20 @@ namespace simple_socket {
explicit UDPConnection(UDPSocket* sock, std::string address, uint16_t port)
: socket(sock), address(std::move(address)), port(port) {}

int read(std::vector<unsigned char>& buffer) override {
return socket->recvFrom(address, port, buffer);
}
int read(unsigned char* buffer, size_t size) override {
return socket->recvFrom(address, port, buffer, size);
}
bool readExact(std::vector<unsigned char>& buffer) override {
throw std::runtime_error("Not supported");
}

bool readExact(unsigned char* buffer, size_t size) override {
throw std::runtime_error("Not supported");
}
bool write(const std::string& message) override {
return socket->sendTo(address, port, message);
}
bool write(const std::vector<unsigned char>& data) override {
return socket->sendTo(address, port, data);

bool write(const unsigned char* data, size_t size) override {
return socket->sendTo(address, port, data, size);
}
void close() override {

void close() override {
// do nothing
}
};

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

#include "simple_socket/Pipe.hpp"

#include <array>
#include <iostream>

using namespace simple_socket;
Expand All @@ -21,7 +22,7 @@ int main() {
if (input == "exit")
break;

std::vector<unsigned char> buffer(512);
std::array<unsigned char, 512> buffer{};
const auto bytesRecevied = conn->read(buffer);
std::string received{buffer.begin(), buffer.begin() + bytesRecevied};
std::cout << "Server: " << received << std::endl;
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 @@ -2,6 +2,7 @@
#include "simple_socket/Pipe.hpp"

#include <iostream>
#include <array>

using namespace simple_socket;

Expand All @@ -12,7 +13,7 @@ int main() {

if (!conn) return 1;

std::vector<unsigned char> buffer(512);
std::array<unsigned char, 512> buffer{};
// Ping-Pong logic
while (true) {
const auto len = conn->write(buffer);
Expand Down
9 changes: 4 additions & 5 deletions tests/integration/run_tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "simple_socket/TCPSocket.hpp"

#include <iostream>
#include <vector>
#include <array>

using namespace simple_socket;

Expand All @@ -11,13 +11,12 @@ int main() {
TCPClientContext client;
if (const auto conn = client.connect("127.0.0.1", 8080)) {

std::string message = "Per";
conn->write(message);
conn->write("Per");

std::vector<unsigned char> buffer(1024);
std::array<unsigned char, 1024> buffer{};
const auto bytesRead = conn->read(buffer);

std::cout << "Response from server: " << std::string(buffer.begin(), buffer.begin() + static_cast<int>(bytesRead)) << std::endl;
std::cout << "Response from server: " << std::string(buffer.begin(), buffer.begin() + bytesRead) << std::endl;
} else {
std::cerr << "Failed to connect to server" << std::endl;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/run_tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
#include <array>

using namespace simple_socket;

void socketHandler(std::unique_ptr<SimpleConnection> conn) {

std::vector<unsigned char> buffer(1024);
std::array<unsigned char, 1024> buffer{};
const auto n = conn->read(buffer);

std::string msg{buffer.begin(), buffer.begin() + n};
Expand Down
7 changes: 4 additions & 3 deletions tests/test_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <thread>
#include <vector>
#include <array>

#include <catch2/catch_test_macros.hpp>

Expand All @@ -27,10 +28,10 @@ namespace {
TEST_CASE("NamedPipe read/write") {

std::thread serverThread([] {
std::unique_ptr<NamedPipe> conn = NamedPipe::listen(domain);
const auto conn = NamedPipe::listen(domain);
REQUIRE(conn);

std::vector<unsigned char> buffer(1024);
std::array<unsigned char, 1024> buffer{};
const auto bytesRead = conn->read(buffer);

std::string expectedMessage = generateMessage();
Expand All @@ -53,7 +54,7 @@ TEST_CASE("NamedPipe read/write") {

conn->write(message);

std::vector<unsigned char> buffer(1024);
std::array<unsigned char, 1024> buffer{};
const auto bytesRead = conn->read(buffer);
REQUIRE(bytesRead == expectedResponse.size());
std::string response(buffer.begin(), buffer.begin() + bytesRead);
Expand Down

0 comments on commit 151dfd2

Please sign in to comment.