Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cmake_minimum_required(VERSION 3.14)

Include(FetchContent)
FetchContent_Declare(
spdlog
Expand Down
15 changes: 15 additions & 0 deletions examples/shared_memory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
cmake_minimum_required(VERSION 3.14)
project(example_shared_memory VERSION 1.0.0 LANGUAGES CXX)


if(NOT TARGET CPPUTILS2::cpputils2)
find_package(cpputils2 REQUIRED)

Include(FetchContent)
FetchContent_Declare(
spdlog
GIT_REPOSITORY https://github.com/gabime/spdlog.git
GIT_TAG v1.x
)

FetchContent_MakeAvailable(spdlog)
endif()


add_executable(shared_memory src/main.cpp)

target_link_libraries(shared_memory PRIVATE CPPUTILS2::cpputils2 spdlog::spdlog)
96 changes: 49 additions & 47 deletions examples/shared_memory/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,53 @@
const int32_t expected = 42;
const std::string file_name("test_shm");

int main(int argc, char** argv) {
SPDLOG_INFO("Test shared memory");

CppUtils2::Shm shm(file_name);
auto ret = shm.allocate(sizeof(int32_t));
if (ret == CppUtils2::Result::RET_ERROR) {
SPDLOG_ERROR("Error");
return 1;
}

void* ptr = shm.get_raw_ptr();
if (ptr == nullptr) {
SPDLOG_ERROR("Error");
return 1;
}
int32_t* ptr_int = reinterpret_cast<int32_t*>(ptr);
SPDLOG_INFO("ptr_int: {}", *ptr_int);
*ptr_int = expected;

int32_t val = *ptr_int;



CppUtils2::Shm shm2("test_shm");
ret = shm2.open_existing(sizeof(int32_t));
if (ret == CppUtils2::Result::RET_ERROR)
{
SPDLOG_ERROR("Error");
return 1;
}
ptr = shm2.get_raw_ptr();
ptr_int = reinterpret_cast<int32_t*>(ptr);

SPDLOG_INFO("ptr_int: {}", *ptr_int);

val = *ptr_int;

SPDLOG_INFO("val: {} expected: {}", val, expected);
if (val != expected) {
SPDLOG_ERROR("Error");
return 1;
}
shm2.close();
shm.close();
shm.unlink();

return 0;
int main(int argc, char **argv)
{
SPDLOG_INFO("Test shared memory");

CppUtils2::Shm shm(file_name);
auto ret = shm.allocate(sizeof(int32_t));
if (ret == CppUtils2::Result::RET_ERROR)
{
SPDLOG_ERROR("Error");
return 1;
}

void *ptr = shm.get_raw_ptr();
if (ptr == nullptr)
{
SPDLOG_ERROR("Error");
return 1;
}
int32_t *ptr_int = reinterpret_cast<int32_t *>(ptr);
SPDLOG_INFO("ptr_int: {}", *ptr_int);
*ptr_int = expected;

int32_t val = *ptr_int;

CppUtils2::Shm shm2("test_shm");
ret = shm2.open_existing(sizeof(int32_t));
if (ret == CppUtils2::Result::RET_ERROR)
{
SPDLOG_ERROR("Error");
return 1;
}
ptr = shm2.get_raw_ptr();
ptr_int = reinterpret_cast<int32_t *>(ptr);

SPDLOG_INFO("ptr_int: {}", *ptr_int);

val = *ptr_int;

SPDLOG_INFO("val: {} expected: {}", val, expected);
if (val != expected)
{
SPDLOG_ERROR("Error");
return 1;
}
shm2.close();
shm.close();
shm.unlink();

return 0;
}
13 changes: 10 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,32 @@ set(SOURCES_AND_HEADERS
include/cpputils2/common/types.hpp
include/cpputils2/trigger/trigger.hpp
include/cpputils2/file/file.hpp

include/cpputils2/net/socket/isocket.hpp
)

if (WIN32)
list(APPEND SOURCES_AND_HEADERS include/cpputils2/win/shm/shm.hpp)

list(APPEND SOURCES_AND_HEADERS
include/cpputils2/win/shm/shm.hpp
include/cpputils2/win/net/socket/socket.hpp
include/cpputils2/win/net/socket/tcpsocketclient.hpp
include/cpputils2/win/net/socket/tcpsocketserver.hpp)

else()

list(APPEND SOURCES_AND_HEADERS
include/cpputils2/linux/net/socket/tcpsocketclient.hpp
include/cpputils2/linux/net/socket/tcpsocketserver.hpp
include/cpputils2/linux/net/socket/udpsocketserver.hpp
include/cpputils2/linux/net/socket/udpsocketclient.hpp
include/cpputils2/linux/net/socket/socket.hpp
include/cpputils2/linux/net/socket/isocket.hpp
include/cpputils2/linux/net/socket/udsclient.hpp
include/cpputils2/linux/net/socket/udsserver.hpp
include/cpputils2/linux/shm/shm.hpp
include/cpputils2/linux/futex/futex.hpp
include/cpputils2/linux/futex/shared_futex.hpp
)

endif()

add_library(cpputils2 INTERFACE
Expand Down
2 changes: 1 addition & 1 deletion src/include/cpputils2/linux/net/socket/dgramsocket.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "cpputils2/linux/net/socket/isocket.hpp"
#include "cpputils2/net/socket/isocket.hpp"

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
Expand Down
2 changes: 1 addition & 1 deletion src/include/cpputils2/linux/net/socket/socket.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "cpputils2/linux/net/socket/isocket.hpp"
#include "cpputils2/net/socket/isocket.hpp"

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
Expand Down
2 changes: 1 addition & 1 deletion src/include/cpputils2/linux/net/socket/udpsocketserver.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "cpputils2/common/types.hpp"
#include "cpputils2/linux/net/socket/isocket.hpp"
#include "cpputils2/net/socket/isocket.hpp"

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
Expand Down
2 changes: 1 addition & 1 deletion src/include/cpputils2/linux/net/socket/udsclient.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "cpputils2/linux/net/socket/isocket.hpp"
#include "cpputils2/net/socket/isocket.hpp"

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#pragma once

#include <cstdint>
#include <iostream>
#include <string>

#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"

#include <cstdint>
#include <iostream>
#ifdef _WIN32
using ssize_t = std::int32_t;
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#endif

namespace CppUtils2
{
Expand Down
106 changes: 106 additions & 0 deletions src/include/cpputils2/win/net/socket/socket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#pragma once

#include "cpputils2/net/socket/isocket.hpp"

#include <winsock2.h>

#include <cstdint>
#include <cstdio>
#include <iostream>
#include <string>

namespace CppUtils2
{
namespace net
{

class Socket : public ISocket
{
public:
Socket() : sock(INVALID_SOCKET) {};

Socket(SOCKET s) : sock(s) {};

void disconnect() override
{
if (sock != INVALID_SOCKET)
{
auto iResult = shutdown(sock, SD_SEND);

// printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
sock = INVALID_SOCKET;
}
}

ssize_t write_data(const void *data, ssize_t size) override
{
auto iResult = send(sock, reinterpret_cast<const char *>(data), size, 0);
if (iResult == SOCKET_ERROR)
{
return -1;
}
return iResult;
}

bool write_string(const std::string &data) override
{
return write_data(data.c_str(), data.size()) != -1;
}

ssize_t read_data(void *buffer, ssize_t size) const override
{
auto iResult = recv(sock, reinterpret_cast<char *>(buffer), size, 0);
if (iResult == SOCKET_ERROR)
{
return -1;
}

return iResult;
}

bool read_string(std::string &data) const override
{
char buffer[1024];
int iResult;
do
{
iResult = recv(sock, buffer, 1024, 0);
if (iResult > 0)
{
data.append(buffer, iResult);
}
else if (iResult == 0)
{
return false;
}
else
{
return false;
}
} while (iResult != 0);

return true;
}

std::int32_t get_socket_fd() const override
{
return 0;
}

SOCKET get_socket_handle() const
{
return sock;
}

virtual ~Socket()
{
disconnect();
};

protected:
SOCKET sock;
};
} // namespace net
} // namespace CppUtils2
Loading
Loading