Skip to content

Commit 453c270

Browse files
authored
Add Unix UDP implementation (#54)
* Add Unix UDP implementation * Fix ChannelUnixSocketTx::sendBuffer * Add ChannelUnixUdpClient * Add UDP server implementation and related functionalities - Introduced `ChannelUnixUdpServer` class inheriting from `ChannelUnixSocketServer` to handle UDP-specific operations. - Implemented methods for creating a UDP server socket, handling incoming datagrams, and sending buffers to clients. - Added packet size handling logic in `getPacketSize` and `sendBuffer` methods. - Enhanced error handling for socket operations and added constants for maximum UDP payload size. - Updated `ChannelUnixSocketServer` to manage client connections and data processing. - Refactored `ChannelUnixTcpServer` to reduce code duplication by reusing common functionalities from `ChannelUnixSocketServer`. * Enhance CMake configuration for CPP17 and CPP23 support in workflows and tasks * Refactor Channel classes and update CMake tasks for C++17/23 compatibility
1 parent 1852405 commit 453c270

30 files changed

Lines changed: 1251 additions & 565 deletions

.github/workflows/cpp_tests.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,39 @@ jobs:
3131
3232
- uses: actions/checkout@v4
3333

34-
- name: Configure CMake
34+
- name: Configure CMake for CPP23
3535
env:
3636
CC: gcc-14
37-
CXX: g++-14
37+
CXX: g++-14
3838
working-directory: ${{github.workspace}}/
3939
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
4040
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
41-
run: cmake -B ${{github.workspace}}/build -S ${{github.workspace}}/cpp -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}
41+
run: cmake -B ${{github.workspace}}/build23 -S ${{github.workspace}}/cpp -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCPP_VERSION=23
4242

43-
- name: Build
43+
- name: Build CPP23
4444
working-directory: ${{github.workspace}}/
4545
# Build your program with the given configuration
46-
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
46+
run: cmake --build ${{github.workspace}}/build23 --config ${{env.BUILD_TYPE}}
4747

4848
- name: Run CTest
49-
working-directory: ${{github.workspace}}/build/
49+
working-directory: ${{github.workspace}}/build23/
50+
run: ctest --rerun-failed --output-on-failure
51+
52+
- name: Configure CMake for CPP17
53+
env:
54+
CC: gcc-14
55+
CXX: g++-14
56+
working-directory: ${{github.workspace}}/
57+
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
58+
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
59+
run: cmake -B ${{github.workspace}}/build17 -S ${{github.workspace}}/cpp -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DCPP_VERSION=17
60+
61+
- name: Build CPP17
62+
working-directory: ${{github.workspace}}/
63+
run: cmake --build ${{github.workspace}}/build17 --config ${{env.BUILD_TYPE}}
64+
65+
- name: Run CTest
66+
working-directory: ${{github.workspace}}/build17/
5067
run: ctest --rerun-failed --output-on-failure
5168

5269
convert_to_draft:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
build
1+
build17
2+
build23
23
dist
34
bin

.vscode/tasks.json

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
22
"tasks": [
33
{
4-
"label": "CMakePrepare",
4+
"label": "CMakePrepare CPP23",
55
"type": "shell",
66
"command": "cmake",
77
"args": [
88
"-B",
9-
"${workspaceFolder}/build",
9+
"${workspaceFolder}/build23",
1010
"-S",
1111
"${workspaceFolder}/cpp",
12-
"-DCMAKE_BUILD_TYPE=Debug"
12+
"-DCMAKE_BUILD_TYPE=Debug",
13+
"-DCPP_VERSION=23"
1314
],
1415
"group": {
1516
"kind": "build",
@@ -19,12 +20,54 @@
1920
"detail": "Generated task by Debugger."
2021
},
2122
{
22-
"label": "CMakeBuild",
23+
"label": "CMakeBuild CPP23",
2324
"type": "shell",
2425
"command": "cmake",
2526
"args": [
2627
"--build",
27-
"${workspaceFolder}/build"
28+
"${workspaceFolder}/build23"
29+
],
30+
"group": {
31+
"kind": "build",
32+
"isDefault": true
33+
},
34+
"problemMatcher": ["$gcc"],
35+
"detail": "Generated task by Debugger.",
36+
"presentation": {
37+
"echo": true,
38+
"reveal": "always",
39+
"focus": false,
40+
"panel": "shared",
41+
"showReuseMessage": true,
42+
"clear": true
43+
}
44+
},
45+
{
46+
"label": "CMakePrepare CPP17",
47+
"type": "shell",
48+
"command": "cmake",
49+
"args": [
50+
"-B",
51+
"${workspaceFolder}/build17",
52+
"-S",
53+
"${workspaceFolder}/cpp",
54+
"-DCMAKE_BUILD_TYPE=Debug",
55+
"-DCPP_VERSION=17"
56+
],
57+
"group": {
58+
"kind": "build",
59+
"isDefault": true
60+
},
61+
"problemMatcher": ["$gcc"],
62+
"detail": "Generated task by Debugger."
63+
},
64+
{
65+
"label": "CMakeBuild CPP17",
66+
"type": "shell",
67+
"command": "cmake",
68+
"args": [
69+
"--build",
70+
"${workspaceFolder}/build17"
2871
],
2972
"group": {
3073
"kind": "build",

cpp/CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ include(CTest)
77

88
# option(BUILD_MODULE_L5 "Build the L5 module" ON)
99

10-
set(CMAKE_CXX_STANDARD 23)
10+
set(CPP_VERSION "23" CACHE STRING "C++ standard version to use (17 or 23)")
11+
set_property(CACHE CPP_VERSION PROPERTY STRINGS "17" "23")
12+
if(NOT CPP_VERSION STREQUAL "17" AND NOT CPP_VERSION STREQUAL "23")
13+
message(FATAL_ERROR "CPP_VERSION must be 17 or 23 (got '${CPP_VERSION}')")
14+
endif()
15+
set(CMAKE_CXX_STANDARD ${CPP_VERSION})
1116
set(CMAKE_CXX_STANDARD_REQUIRED True)
1217
set(CMAKE_C_STANDARD 99)
1318
set(CMAKE_C_STANDARD_REQUIRED True)

cpp/modules/l4/CMakeLists.txt

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
22

33
add_library(module_l4 STATIC)
4+
add_executable(module_l4_tests
5+
test/test_dummy.cpp)
46

57
target_sources(module_l4
68
PRIVATE
7-
src/Channel.cpp
8-
src/impl/ChannelUnixTcp.cpp
9+
src/ChannelRx.cpp
10+
src/ChannelTx.cpp
911
src/impl/ClientsRepoImpl.cpp
10-
src/impl/ChannelUnixPipeImpl.cpp
1112
src/impl/ChannelUnixFile.cpp
1213
)
1314

@@ -16,31 +17,45 @@ target_include_directories(module_l4 PUBLIC
1617
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
1718
)
1819

19-
add_executable(module_l4_tests
20-
test/test_ChannelUnixTcp.cpp
21-
test/test_ChannelUnixPipeImpl.cpp
22-
)
2320

2421
if (USE_STD_MEMORY)
2522
target_compile_definitions(module_l4 PRIVATE USE_STD_MEMORY)
2623
target_compile_definitions(module_l4_tests PRIVATE USE_STD_MEMORY)
2724
endif()
2825

2926
if(CMAKE_CXX_STANDARD GREATER_EQUAL 23)
30-
target_sources(module_l4 PRIVATE src/cxx_23/ChannelTx.cpp)
27+
message("Build for C++23")
28+
target_sources(module_l4 PRIVATE
29+
src/impl/ChannelUnixTcp.cpp
30+
src/impl/ChannelUnixUdp.cpp
31+
src/impl/ChannelUnixPipeImpl.cpp
32+
src/impl/ChannelUnixSocket.cpp
33+
)
34+
35+
36+
target_sources(module_l4_tests PRIVATE
37+
test/test_ChannelUnixTcp.cpp
38+
test/test_ChannelUnixPipeImpl.cpp
39+
)
40+
41+
# This helps CMake provide better error messages if the compiler doesn't fully
42+
# support the C++23 standard.
43+
target_compile_features(module_l4_tests PRIVATE cxx_std_23)
44+
3145
elseif(CMAKE_CXX_STANDARD GREATER_EQUAL 17)
32-
target_sources(module_l4 PRIVATE src/cxx_17/ChannelTx.cpp)
46+
47+
message("Build for C++17")
48+
# target_sources(module_l4 PRIVATE src/cxx_17/ChannelTx.cpp)
49+
3350
else()
51+
3452
message(FATAL_ERROR "module_l4 requires C++17 or higher. Please set CMAKE_CXX_STANDARD to 17 or above.")
53+
3554
endif()
3655

3756
target_include_directories(module_l4_tests PRIVATE ./include/)
3857
target_link_libraries(module_l4_tests PUBLIC Catch2::Catch2WithMain module_l4)
3958

40-
# This helps CMake provide better error messages if the compiler doesn't fully
41-
# support the C++23 standard.
42-
target_compile_features(module_l4_tests PRIVATE cxx_std_23)
43-
4459
catch_discover_tests(module_l4_tests
4560
PROPERTIES
4661
TIMEOUT 10)

cpp/modules/l4/include/Channel.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

cpp/modules/l4/include/ChannelRx.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include "data.h"
4+
#include "SerializerL4.h"
5+
6+
namespace styxlib
7+
{
8+
class ChannelRx
9+
{
10+
protected:
11+
DeserializerL4Ptr deserializer;
12+
13+
public:
14+
ChannelRx();
15+
ErrorCode setDeserializer(DeserializerL4Ptr deserializer);
16+
virtual ~ChannelRx() = default;
17+
};
18+
19+
/**
20+
* Reads the first X bytes of buffer and interprets them as a packet size.
21+
* @param headerSize The size of the packet header (1, 2, or 4 bytes).
22+
* @param buffer The buffer to read from.
23+
* @param bufferSize The number of valid bytes available in the buffer.
24+
* @return The decoded packet payload size, or an ErrorCode if the buffer
25+
* is too small to hold the header.
26+
*/
27+
SizeResult getPacketSize(
28+
const PacketHeaderSize &headerSize,
29+
const uint8_t *buffer,
30+
Size bufferSize);
31+
}

cpp/modules/l4/include/ChannelTx.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ namespace styxlib
1010
virtual ~ChannelTx() = default;
1111
virtual SizeResult sendBuffer(ClientId clientId, const StyxBuffer buffer, Size size) = 0;
1212
};
13+
14+
/**
15+
* Sets the packet size in the provided buffer according to the specified header size.
16+
* @param headerSize The size of the packet header (1, 2, or 4 bytes).
17+
* @param buffer The buffer where the packet size will be set.
18+
* @param bufferSize The size of the buffer.
19+
* @param packetSize The size of the packet to set.
20+
* @return The number of bytes used for the header, or an ErrorCode if an error occurs.
21+
*/
22+
SizeResult setPacketSize(
23+
const PacketHeaderSize &headerSize,
24+
uint8_t *buffer,
25+
Size bufferSize,
26+
Size packetSize);
1327
}
1428

15-
#if __cplusplus >= 202302L
16-
#include "cxx_23/ChannelTx.h"
17-
#elif __cplusplus >= 201703L
18-
#include "cxx_17/ChannelTx.h"
19-
#else
20-
// Handle older standards (C++14, C++11, etc.)
21-
#error "This library requires at least C++17."
22-
#endif

cpp/modules/l4/include/SerializerL4.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#pragma once
22
#include "data.h"
3-
#include "Channel.h"
43

54
namespace styxlib
65
{

cpp/modules/l4/include/cxx_17/ChannelTx.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)