Skip to content

Commit 2b94ff3

Browse files
committed
WIP Win sockets
1 parent 7d0e9d8 commit 2b94ff3

File tree

11 files changed

+583
-139
lines changed

11 files changed

+583
-139
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
13
Include(FetchContent)
24
FetchContent_Declare(
35
spdlog
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
cmake_minimum_required(VERSION 3.14)
12
project(example_shared_memory VERSION 1.0.0 LANGUAGES CXX)
23

34

5+
if(NOT TARGET CPPUTILS2::cpputils2)
6+
find_package(cpputils2 REQUIRED)
7+
8+
Include(FetchContent)
9+
FetchContent_Declare(
10+
spdlog
11+
GIT_REPOSITORY https://github.com/gabime/spdlog.git
12+
GIT_TAG v1.x
13+
)
14+
15+
FetchContent_MakeAvailable(spdlog)
16+
endif()
17+
18+
419
add_executable(shared_memory src/main.cpp)
520

621
target_link_libraries(shared_memory PRIVATE CPPUTILS2::cpputils2 spdlog::spdlog)

examples/shared_memory/src/main.cpp

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,51 +11,53 @@
1111
const int32_t expected = 42;
1212
const std::string file_name("test_shm");
1313

14-
int main(int argc, char** argv) {
15-
SPDLOG_INFO("Test shared memory");
16-
17-
CppUtils2::Shm shm(file_name);
18-
auto ret = shm.allocate(sizeof(int32_t));
19-
if (ret == CppUtils2::Result::RET_ERROR) {
20-
SPDLOG_ERROR("Error");
21-
return 1;
22-
}
23-
24-
void* ptr = shm.get_raw_ptr();
25-
if (ptr == nullptr) {
26-
SPDLOG_ERROR("Error");
27-
return 1;
28-
}
29-
int32_t* ptr_int = reinterpret_cast<int32_t*>(ptr);
30-
SPDLOG_INFO("ptr_int: {}", *ptr_int);
31-
*ptr_int = expected;
32-
33-
int32_t val = *ptr_int;
34-
35-
36-
37-
CppUtils2::Shm shm2("test_shm");
38-
ret = shm2.open_existing(sizeof(int32_t));
39-
if (ret == CppUtils2::Result::RET_ERROR)
40-
{
41-
SPDLOG_ERROR("Error");
42-
return 1;
43-
}
44-
ptr = shm2.get_raw_ptr();
45-
ptr_int = reinterpret_cast<int32_t*>(ptr);
46-
47-
SPDLOG_INFO("ptr_int: {}", *ptr_int);
48-
49-
val = *ptr_int;
50-
51-
SPDLOG_INFO("val: {} expected: {}", val, expected);
52-
if (val != expected) {
53-
SPDLOG_ERROR("Error");
54-
return 1;
55-
}
56-
shm2.close();
57-
shm.close();
58-
shm.unlink();
59-
60-
return 0;
14+
int main(int argc, char **argv)
15+
{
16+
SPDLOG_INFO("Test shared memory");
17+
18+
CppUtils2::Shm shm(file_name);
19+
auto ret = shm.allocate(sizeof(int32_t));
20+
if (ret == CppUtils2::Result::RET_ERROR)
21+
{
22+
SPDLOG_ERROR("Error");
23+
return 1;
24+
}
25+
26+
void *ptr = shm.get_raw_ptr();
27+
if (ptr == nullptr)
28+
{
29+
SPDLOG_ERROR("Error");
30+
return 1;
31+
}
32+
int32_t *ptr_int = reinterpret_cast<int32_t *>(ptr);
33+
SPDLOG_INFO("ptr_int: {}", *ptr_int);
34+
*ptr_int = expected;
35+
36+
int32_t val = *ptr_int;
37+
38+
CppUtils2::Shm shm2("test_shm");
39+
ret = shm2.open_existing(sizeof(int32_t));
40+
if (ret == CppUtils2::Result::RET_ERROR)
41+
{
42+
SPDLOG_ERROR("Error");
43+
return 1;
44+
}
45+
ptr = shm2.get_raw_ptr();
46+
ptr_int = reinterpret_cast<int32_t *>(ptr);
47+
48+
SPDLOG_INFO("ptr_int: {}", *ptr_int);
49+
50+
val = *ptr_int;
51+
52+
SPDLOG_INFO("val: {} expected: {}", val, expected);
53+
if (val != expected)
54+
{
55+
SPDLOG_ERROR("Error");
56+
return 1;
57+
}
58+
shm2.close();
59+
shm.close();
60+
shm.unlink();
61+
62+
return 0;
6163
}

src/CMakeLists.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ set(SOURCES_AND_HEADERS
2020
include/cpputils2/common/types.hpp
2121
include/cpputils2/trigger/trigger.hpp
2222
include/cpputils2/file/file.hpp
23-
23+
include/cpputils2/net/socket/isocket.hpp
2424
)
2525

2626
if (WIN32)
27-
list(APPEND SOURCES_AND_HEADERS include/cpputils2/win/shm/shm.hpp)
27+
28+
list(APPEND SOURCES_AND_HEADERS
29+
include/cpputils2/win/shm/shm.hpp
30+
include/cpputils2/win/net/socket/socket.hpp
31+
include/cpputils2/win/net/socket/tcpsocketclient.hpp
32+
include/cpputils2/win/net/socket/tcpsocketserver.hpp)
33+
2834
else()
35+
2936
list(APPEND SOURCES_AND_HEADERS
3037
include/cpputils2/linux/net/socket/tcpsocketclient.hpp
3138
include/cpputils2/linux/net/socket/tcpsocketserver.hpp
@@ -39,6 +46,7 @@ else()
3946
include/cpputils2/linux/futex/futex.hpp
4047
include/cpputils2/linux/futex/shared_futex.hpp
4148
)
49+
4250
endif()
4351

4452
add_library(cpputils2 INTERFACE

src/include/cpputils2/linux/net/socket/socket.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include "cpputils2/linux/net/socket/isocket.hpp"
3+
#include "cpputils2/net/socket/isocket.hpp"
44

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

src/include/cpputils2/linux/net/socket/isocket.hpp renamed to src/include/cpputils2/net/socket/isocket.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <iostream>
45
#include <string>
56

6-
#include "spdlog/sinks/stdout_color_sinks.h"
7-
#include "spdlog/spdlog.h"
8-
9-
#include <cstdint>
10-
#include <iostream>
7+
#ifdef _WIN32
8+
using ssize_t = std::int32_t;
9+
#else
10+
#include <arpa/inet.h>
11+
#include <netinet/in.h>
12+
#include <sys/socket.h>
13+
#include <sys/types.h>
14+
#include <unistd.h>
15+
#endif
1116

1217
namespace CppUtils2
1318
{
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#pragma once
2+
3+
#include "cpputils2/net/socket/isocket.hpp"
4+
5+
#include <winsock2.h>
6+
7+
#include <cstdint>
8+
#include <cstdio>
9+
#include <iostream>
10+
#include <string>
11+
12+
namespace CppUtils2
13+
{
14+
namespace net
15+
{
16+
17+
class Socket : public ISocket
18+
{
19+
public:
20+
Socket() : sock(INVALID_SOCKET) {};
21+
22+
Socket(SOCKET s) : sock(s) {};
23+
24+
void disconnect() override
25+
{
26+
if (sock != INVALID_SOCKET)
27+
{
28+
auto iResult = shutdown(sock, SD_SEND);
29+
30+
// printf("shutdown failed with error: %d\n", WSAGetLastError());
31+
closesocket(sock);
32+
WSACleanup();
33+
sock = INVALID_SOCKET;
34+
}
35+
}
36+
37+
ssize_t write_data(const void *data, ssize_t size) override
38+
{
39+
auto iResult = send(sock, reinterpret_cast<const char *>(data), size, 0);
40+
if (iResult == SOCKET_ERROR)
41+
{
42+
return -1;
43+
}
44+
return iResult;
45+
}
46+
47+
bool write_string(const std::string &data) override
48+
{
49+
return write_data(data.c_str(), data.size()) != -1;
50+
}
51+
52+
ssize_t read_data(void *buffer, ssize_t size) const override
53+
{
54+
auto iResult = recv(sock, reinterpret_cast<char *>(buffer), size, 0);
55+
if (iResult == SOCKET_ERROR)
56+
{
57+
return -1;
58+
}
59+
60+
return iResult;
61+
}
62+
63+
bool read_string(std::string &data) const override
64+
{
65+
char buffer[1024];
66+
int iResult;
67+
do
68+
{
69+
iResult = recv(sock, buffer, 1024, 0);
70+
if (iResult > 0)
71+
{
72+
data.append(buffer, iResult);
73+
}
74+
else if (iResult == 0)
75+
{
76+
return false;
77+
}
78+
else
79+
{
80+
return false;
81+
}
82+
} while (iResult != 0);
83+
84+
return true;
85+
}
86+
87+
std::int32_t get_socket_fd() const override
88+
{
89+
return 0;
90+
}
91+
92+
SOCKET get_socket_handle() const
93+
{
94+
return sock;
95+
}
96+
97+
virtual ~Socket()
98+
{
99+
disconnect();
100+
};
101+
102+
protected:
103+
SOCKET sock;
104+
};
105+
} // namespace net
106+
} // namespace CppUtils2

0 commit comments

Comments
 (0)