Skip to content

Commit dd9f1f6

Browse files
Fixed receiveDatagram() function and added unit tests
Core Changes: - Fixed issue that datagrams would be lost if they received faster than the user polled `receiveDatagram()` - Changed API of `receiveDatagram()` function - The `std::vector<char>` variant does not exist anymore. Use the `char*` method instead and allocate the vector yourself, if you need to. - The function now return error reasons the reason as `udpcap::Error` - Added `isClosed()` function - Removed `hasPendingDatagram()` function - Fixed a memory leak - Fixed crashes when closing the socket with close() - Fixed many compiler and clang-tidy warnings Tests: - Added GTest as submodule - Added Tests
1 parent 4946dc2 commit dd9f1f6

32 files changed

+1966
-775
lines changed

.clang-tidy

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,25 @@ Checks: "-*,
3434
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
3535
-cppcoreguidelines-pro-type-vararg,
3636
-cppcoreguidelines-pro-type-reinterpret-cast,
37+
-cppcoreguidelines-use-default-member-init,
3738
3839
misc-*,
3940
-misc-non-private-member-variables-in-classes,
4041
-misc-no-recursion,
42+
-misc-include-cleaner,
4143
4244
modernize-*,
4345
-modernize-pass-by-value,
4446
-modernize-use-trailing-return-type,
4547
-modernize-use-auto,
48+
-modernize-use-default-member-init,
4649
-modernize-concat-nested-namespaces,
4750
-modernize-return-braced-init-list,
4851
-modernize-use-nodiscard,
4952
-modernize-avoid-bind,
5053
5154
performance-*,
55+
-performance-avoid-endl,
5256
5357
readability-*,
5458
-readability-braces-around-statements,

.github/workflows/build-windows.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
}
4242
4343
- name: Checkout
44-
uses: actions/checkout@v3
44+
uses: actions/checkout@v4
4545
with:
4646
submodules: 'true'
4747
fetch-depth: 0
@@ -81,7 +81,7 @@ jobs:
8181
echo "CMAKE_PROJECT_VERSION=$cmake_project_version" >> "$Env:GITHUB_ENV"
8282
8383
- name: Upload binaries
84-
uses: actions/upload-artifact@v3
84+
uses: actions/upload-artifact@v4
8585
with:
8686
name: ${{ env.PROJECT_NAME }}-${{ env.CMAKE_PROJECT_VERSION }}-windows-${{ matrix.build_arch }}-${{ env.VS_NAME }}-${{ matrix.library_type }}
8787
path: ${{github.workspace}}/${{env.INSTALL_PREFIX}}

CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ option(UDPCAP_BUILD_SAMPLES
3535
"Build project samples"
3636
ON)
3737

38+
option(UDPCAP_BUILD_TESTS
39+
"Build the udpcap GTests. Requires GTest::GTest to be available."
40+
OFF)
41+
3842
option(UDPCAP_THIRDPARTY_ENABLED
3943
"Enable building against the builtin dependencies"
4044
ON)
@@ -57,6 +61,12 @@ cmake_dependent_option(UDPCAP_THIRDPARTY_USE_BUILTIN_ASIO
5761
"UDPCAP_THIRDPARTY_ENABLED"
5862
OFF)
5963

64+
cmake_dependent_option(UDPCAP_THIRDPARTY_USE_BUILTIN_GTEST
65+
"Fetch and build tests against a predefined version of GTest. If disabled, the targets have to be provided externally."
66+
ON
67+
"UDPCAP_THIRDPARTY_ENABLED AND UDPCAP_BUILD_TESTS"
68+
OFF)
69+
6070
# Module path for finding udpcap
6171
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/udpcap/modules)
6272

@@ -75,6 +85,12 @@ if (UDPCAP_THIRDPARTY_USE_BUILTIN_ASIO)
7585
include(thirdparty/asio/asio_make_available.cmake)
7686
endif()
7787

88+
#--- Fetch GTest -------------------------------
89+
if (UDPCAP_THIRDPARTY_USE_BUILTIN_GTEST)
90+
include(thirdparty/GTest/GTest_make_available.cmake)
91+
endif()
92+
93+
7894
#----------------------------------------------
7995

8096
# Set Debug postfix
@@ -94,6 +110,11 @@ if (UDPCAP_BUILD_SAMPLES)
94110
add_subdirectory(samples/asio_sender_unicast)
95111
endif()
96112

113+
# Tests
114+
if (UDPCAP_BUILD_TESTS)
115+
enable_testing()
116+
add_subdirectory(tests/udpcap_test)
117+
endif()
97118

98119
# Make this package available for packing with CPack
99120
include("${CMAKE_CURRENT_LIST_DIR}/cpack_config.cmake")

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Udpcap has a very simple API with strong similarities to other well-known socket
6060

6161
int main()
6262
{
63-
// Create a Udpcap socket and bind it to a port. For this exampel we want to
63+
// Create a Udpcap socket and bind it to a port. For this example we want to
6464
// receive data from any local or remote source and therefore not bind to an
6565
// IP address.
6666

@@ -70,13 +70,23 @@ int main()
7070

7171
for (;;)
7272
{
73+
// Allocate a buffer for the received datagram. The size of the buffer
74+
// should be large enough to hold the largest possible datagram.
75+
std::vector<char> datagram(65535);
76+
77+
// Create an error code object to hold the error code if an error occurs.
78+
Udpcap::Error error = Udpcap::Error::OK;
79+
7380
// Receive a datagram from the Socket. This is a blocking
7481
// operation. The operation will return once a datagram has been received,
7582
// the socket was closed by another thread or an error occured.
76-
std::vector<char> received_datagram = socket.receiveDatagram();
83+
size_t num_bytes = socket.receiveDatagram(datagram.data(), datagram.size(), error);
84+
85+
// Resize the buffer to the actual size of the received datagram.
86+
datagram.resize(num_bytes);
7787

78-
std::cout << "Received " << received_datagram.size() << " bytes: "
79-
<< std::string(received_datagram.data(), received_datagram.size())
88+
std::cout << "Received " << datagram.size() << " bytes: "
89+
<< std::string(datagram.data(), datagram.size())
8090
<< std::endl;
8191
}
8292

@@ -117,10 +127,12 @@ You can set the following CMake Options to control how Udpcap is supposed to bui
117127
**Option** | **Type** | **Default** | **Explanation** |
118128
|----------------------------------------------|----------|-------------|-----------------------------------------------------------------------------------------------------------------|
119129
| `UDPCAP_BUILD_SAMPLES` | `BOOL` | `ON` | Build the Udpcap (and asio) samples for sending and receiving dummy data |
130+
| `UDPCAP_BUILD_TESTS` | `BOOL` | `OFF` | Build the udpcap GTests. Requires GTest::GTest to be available. |
120131
| `UDPCAP_THIRDPARTY_ENABLED` | `BOOL` | `ON` | Activate / Deactivate the usage of integrated dependencies. |
121132
| `UDPCAP_THIRDPARTY_USE_BUILTIN_NPCAP` | `BOOL` | `ON` | Fetch and build against an integrated Version of the npcap SDK. <br>Only available if `UDPCAP_THIRDPARTY_ENABLED=ON` |
122133
| `UDPCAP_THIRDPARTY_USE_BUILTIN_PCAPPLUSPLUS` | `BOOL` | `ON` | Fetch and build against an integrated Version of Pcap++. <br>_Only available if `UDPCAP_THIRDPARTY_ENABLED=ON`_ |
123134
| `UDPCAP_THIRDPARTY_USE_BUILTIN_ASIO` | `BOOL` | `ON` | Fetch and build against an integrated Version of asio. <br>Only available if `UDPCAP_THIRDPARTY_ENABLED=ON` |
135+
| `UDPCAP_THIRDPARTY_USE_BUILTIN_GTEST` | `BOOL` | `ON` | Fetch and build tests against a predefined version of GTest. If disabled, the targets have to be provided externally. <br>Only available if `UDPCAP_THIRDPARTY_ENABLED=ON` and `UDPCAP_BUILD_TESTS=ON`|
124136
| `BUILD_SHARED_LIBS` | `BOOL` | | Not a udpcap option, but use this to control whether you want to have a static or shared library |
125137
# How to integrate Udpcap in your project
126138

samples/asio_sender_multicast/CMakeLists.txt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
# =========================== LICENSE =================================
2-
#
3-
# Copyright (C) 2016 - 2022 Continental Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
1+
################################################################################
2+
# Copyright (c) 2016 Continental Corporation
3+
#
4+
# This program and the accompanying materials are made available under the
5+
# terms of the Apache License, Version 2.0 which is available at
6+
# https://www.apache.org/licenses/LICENSE-2.0.
7+
#
118
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
#
17-
# =========================== LICENSE =================================
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
# SPDX-License-Identifier: Apache-2.0
15+
################################################################################
1816

1917
cmake_minimum_required(VERSION 3.13)
2018

samples/asio_sender_multicast/src/main.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
/* =========================== LICENSE =================================
2-
*
3-
* Copyright (C) 2016 - 2022 Continental Corporation
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
1+
/********************************************************************************
2+
* Copyright (c) 2016 Continental Corporation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0.
7+
*
118
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*
17-
* =========================== LICENSE =================================
18-
*/
9+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
* License for the specific language governing permissions and limitations
12+
* under the License.
13+
*
14+
* SPDX-License-Identifier: Apache-2.0
15+
********************************************************************************/
1916

2017
#include <iostream>
2118

@@ -33,13 +30,13 @@ int main()
3330
asio::io_service io_service;
3431

3532
const asio::ip::udp::endpoint endpoint(asio::ip::make_address("239.0.0.1"), 14000);
36-
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());
33+
asio::ip::udp::socket udp_socket(io_service, endpoint.protocol());
3734

3835
// set multicast packet TTL
3936
{
4037
const asio::ip::multicast::hops ttl(2);
4138
asio::error_code ec;
42-
upd_socket.set_option(ttl, ec);
39+
udp_socket.set_option(ttl, ec);
4340
if (ec)
4441
{
4542
std::cerr << "ERROR: Setting TTL failed: " << ec.message() << std::endl;
@@ -51,7 +48,7 @@ int main()
5148
{
5249
const asio::ip::multicast::enable_loopback loopback(true);
5350
asio::error_code ec;
54-
upd_socket.set_option(loopback, ec);
51+
udp_socket.set_option(loopback, ec);
5552
if (ec)
5653
{
5754
std::cerr << "ERROR: Error setting loopback option: " << ec.message() << std::endl;
@@ -65,7 +62,7 @@ int main()
6562
std::string buffer_string = "Hello World " + std::to_string(counter);
6663

6764
std::cout << "Sending data \"" << buffer_string << "\"" << std::endl;
68-
upd_socket.send_to(asio::buffer(buffer_string), endpoint);
65+
udp_socket.send_to(asio::buffer(buffer_string), endpoint);
6966
counter++;
7067

7168
std::this_thread::sleep_for(std::chrono::milliseconds(500));

samples/asio_sender_unicast/CMakeLists.txt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
# =========================== LICENSE =================================
2-
#
3-
# Copyright (C) 2016 - 2022 Continental Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
1+
################################################################################
2+
# Copyright (c) 2016 Continental Corporation
3+
#
4+
# This program and the accompanying materials are made available under the
5+
# terms of the Apache License, Version 2.0 which is available at
6+
# https://www.apache.org/licenses/LICENSE-2.0.
7+
#
118
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
#
17-
# =========================== LICENSE =================================
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
# SPDX-License-Identifier: Apache-2.0
15+
################################################################################
1816

1917
cmake_minimum_required(VERSION 3.13)
2018

samples/asio_sender_unicast/src/main.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
/* =========================== LICENSE =================================
2-
*
3-
* Copyright (C) 2016 - 2022 Continental Corporation
4-
*
5-
* Licensed under the Apache License, Version 2.0 (the "License");
6-
* you may not use this file except in compliance with the License.
7-
* You may obtain a copy of the License at
8-
*
9-
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
1+
/********************************************************************************
2+
* Copyright (c) 2016 Continental Corporation
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0.
7+
*
118
* Unless required by applicable law or agreed to in writing, software
12-
* distributed under the License is distributed on an "AS IS" BASIS,
13-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
* See the License for the specific language governing permissions and
15-
* limitations under the License.
16-
*
17-
* =========================== LICENSE =================================
18-
*/
9+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
* License for the specific language governing permissions and limitations
12+
* under the License.
13+
*
14+
* SPDX-License-Identifier: Apache-2.0
15+
********************************************************************************/
1916

2017
#include <iostream>
2118

@@ -33,15 +30,15 @@ int main()
3330
asio::io_service io_service;
3431

3532
const asio::ip::udp::endpoint endpoint(asio::ip::make_address("127.0.0.1"), 14000);
36-
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());
33+
asio::ip::udp::socket udp_socket(io_service, endpoint.protocol());
3734

3835
int counter = 0;
3936
for(;;)
4037
{
4138
std::string buffer_string = "Hello World " + std::to_string(counter);
4239

4340
std::cout << "Sending data \"" << buffer_string << "\"" << std::endl;
44-
upd_socket.send_to(asio::buffer(buffer_string), endpoint);
41+
udp_socket.send_to(asio::buffer(buffer_string), endpoint);
4542
counter++;
4643

4744
std::this_thread::sleep_for(std::chrono::milliseconds(500));

samples/udpcap_receiver_multicast/CMakeLists.txt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
# =========================== LICENSE =================================
2-
#
3-
# Copyright (C) 2016 - 2022 Continental Corporation
4-
#
5-
# Licensed under the Apache License, Version 2.0 (the "License");
6-
# you may not use this file except in compliance with the License.
7-
# You may obtain a copy of the License at
8-
#
9-
# http://www.apache.org/licenses/LICENSE-2.0
10-
#
1+
################################################################################
2+
# Copyright (c) 2016 Continental Corporation
3+
#
4+
# This program and the accompanying materials are made available under the
5+
# terms of the Apache License, Version 2.0 which is available at
6+
# https://www.apache.org/licenses/LICENSE-2.0.
7+
#
118
# Unless required by applicable law or agreed to in writing, software
12-
# distributed under the License is distributed on an "AS IS" BASIS,
13-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14-
# See the License for the specific language governing permissions and
15-
# limitations under the License.
16-
#
17-
# =========================== LICENSE =================================
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
#
14+
# SPDX-License-Identifier: Apache-2.0
15+
################################################################################
1816

1917
cmake_minimum_required(VERSION 3.13)
2018

0 commit comments

Comments
 (0)