Skip to content

Commit

Permalink
Fixed receiveDatagram() function and added unit tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
FlorianReimold authored Feb 23, 2024
1 parent 4946dc2 commit dd9f1f6
Show file tree
Hide file tree
Showing 32 changed files with 1,966 additions and 775 deletions.
4 changes: 4 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,25 @@ Checks: "-*,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-use-default-member-init,
misc-*,
-misc-non-private-member-variables-in-classes,
-misc-no-recursion,
-misc-include-cleaner,
modernize-*,
-modernize-pass-by-value,
-modernize-use-trailing-return-type,
-modernize-use-auto,
-modernize-use-default-member-init,
-modernize-concat-nested-namespaces,
-modernize-return-braced-init-list,
-modernize-use-nodiscard,
-modernize-avoid-bind,
performance-*,
-performance-avoid-endl,
readability-*,
-readability-braces-around-statements,
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
}
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
submodules: 'true'
fetch-depth: 0
Expand Down Expand Up @@ -81,7 +81,7 @@ jobs:
echo "CMAKE_PROJECT_VERSION=$cmake_project_version" >> "$Env:GITHUB_ENV"
- name: Upload binaries
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: ${{ env.PROJECT_NAME }}-${{ env.CMAKE_PROJECT_VERSION }}-windows-${{ matrix.build_arch }}-${{ env.VS_NAME }}-${{ matrix.library_type }}
path: ${{github.workspace}}/${{env.INSTALL_PREFIX}}
Expand Down
21 changes: 21 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ option(UDPCAP_BUILD_SAMPLES
"Build project samples"
ON)

option(UDPCAP_BUILD_TESTS
"Build the udpcap GTests. Requires GTest::GTest to be available."
OFF)

option(UDPCAP_THIRDPARTY_ENABLED
"Enable building against the builtin dependencies"
ON)
Expand All @@ -57,6 +61,12 @@ cmake_dependent_option(UDPCAP_THIRDPARTY_USE_BUILTIN_ASIO
"UDPCAP_THIRDPARTY_ENABLED"
OFF)

cmake_dependent_option(UDPCAP_THIRDPARTY_USE_BUILTIN_GTEST
"Fetch and build tests against a predefined version of GTest. If disabled, the targets have to be provided externally."
ON
"UDPCAP_THIRDPARTY_ENABLED AND UDPCAP_BUILD_TESTS"
OFF)

# Module path for finding udpcap
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/udpcap/modules)

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

#--- Fetch GTest -------------------------------
if (UDPCAP_THIRDPARTY_USE_BUILTIN_GTEST)
include(thirdparty/GTest/GTest_make_available.cmake)
endif()


#----------------------------------------------

# Set Debug postfix
Expand All @@ -94,6 +110,11 @@ if (UDPCAP_BUILD_SAMPLES)
add_subdirectory(samples/asio_sender_unicast)
endif()

# Tests
if (UDPCAP_BUILD_TESTS)
enable_testing()
add_subdirectory(tests/udpcap_test)
endif()

# Make this package available for packing with CPack
include("${CMAKE_CURRENT_LIST_DIR}/cpack_config.cmake")
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Udpcap has a very simple API with strong similarities to other well-known socket

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

Expand All @@ -70,13 +70,23 @@ int main()

for (;;)
{
// Allocate a buffer for the received datagram. The size of the buffer
// should be large enough to hold the largest possible datagram.
std::vector<char> datagram(65535);

// Create an error code object to hold the error code if an error occurs.
Udpcap::Error error = Udpcap::Error::OK;

// Receive a datagram from the Socket. This is a blocking
// operation. The operation will return once a datagram has been received,
// the socket was closed by another thread or an error occured.
std::vector<char> received_datagram = socket.receiveDatagram();
size_t num_bytes = socket.receiveDatagram(datagram.data(), datagram.size(), error);

// Resize the buffer to the actual size of the received datagram.
datagram.resize(num_bytes);

std::cout << "Received " << received_datagram.size() << " bytes: "
<< std::string(received_datagram.data(), received_datagram.size())
std::cout << "Received " << datagram.size() << " bytes: "
<< std::string(datagram.data(), datagram.size())
<< std::endl;
}

Expand Down Expand Up @@ -117,10 +127,12 @@ You can set the following CMake Options to control how Udpcap is supposed to bui
**Option** | **Type** | **Default** | **Explanation** |
|----------------------------------------------|----------|-------------|-----------------------------------------------------------------------------------------------------------------|
| `UDPCAP_BUILD_SAMPLES` | `BOOL` | `ON` | Build the Udpcap (and asio) samples for sending and receiving dummy data |
| `UDPCAP_BUILD_TESTS` | `BOOL` | `OFF` | Build the udpcap GTests. Requires GTest::GTest to be available. |
| `UDPCAP_THIRDPARTY_ENABLED` | `BOOL` | `ON` | Activate / Deactivate the usage of integrated dependencies. |
| `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` |
| `UDPCAP_THIRDPARTY_USE_BUILTIN_PCAPPLUSPLUS` | `BOOL` | `ON` | Fetch and build against an integrated Version of Pcap++. <br>_Only available if `UDPCAP_THIRDPARTY_ENABLED=ON`_ |
| `UDPCAP_THIRDPARTY_USE_BUILTIN_ASIO` | `BOOL` | `ON` | Fetch and build against an integrated Version of asio. <br>Only available if `UDPCAP_THIRDPARTY_ENABLED=ON` |
| `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`|
| `BUILD_SHARED_LIBS` | `BOOL` | | Not a udpcap option, but use this to control whether you want to have a static or shared library |
# How to integrate Udpcap in your project
Expand Down
30 changes: 14 additions & 16 deletions samples/asio_sender_multicast/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# =========================== LICENSE =================================
#
# Copyright (C) 2016 - 2022 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
################################################################################
# Copyright (c) 2016 Continental Corporation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# =========================== LICENSE =================================
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
################################################################################

cmake_minimum_required(VERSION 3.13)

Expand Down
39 changes: 18 additions & 21 deletions samples/asio_sender_multicast/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/* =========================== LICENSE =================================
*
* Copyright (C) 2016 - 2022 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
/********************************************************************************
* Copyright (c) 2016 Continental Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =========================== LICENSE =================================
*/
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include <iostream>

Expand All @@ -33,13 +30,13 @@ int main()
asio::io_service io_service;

const asio::ip::udp::endpoint endpoint(asio::ip::make_address("239.0.0.1"), 14000);
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());
asio::ip::udp::socket udp_socket(io_service, endpoint.protocol());

// set multicast packet TTL
{
const asio::ip::multicast::hops ttl(2);
asio::error_code ec;
upd_socket.set_option(ttl, ec);
udp_socket.set_option(ttl, ec);
if (ec)
{
std::cerr << "ERROR: Setting TTL failed: " << ec.message() << std::endl;
Expand All @@ -51,7 +48,7 @@ int main()
{
const asio::ip::multicast::enable_loopback loopback(true);
asio::error_code ec;
upd_socket.set_option(loopback, ec);
udp_socket.set_option(loopback, ec);
if (ec)
{
std::cerr << "ERROR: Error setting loopback option: " << ec.message() << std::endl;
Expand All @@ -65,7 +62,7 @@ int main()
std::string buffer_string = "Hello World " + std::to_string(counter);

std::cout << "Sending data \"" << buffer_string << "\"" << std::endl;
upd_socket.send_to(asio::buffer(buffer_string), endpoint);
udp_socket.send_to(asio::buffer(buffer_string), endpoint);
counter++;

std::this_thread::sleep_for(std::chrono::milliseconds(500));
Expand Down
30 changes: 14 additions & 16 deletions samples/asio_sender_unicast/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# =========================== LICENSE =================================
#
# Copyright (C) 2016 - 2022 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
################################################################################
# Copyright (c) 2016 Continental Corporation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# =========================== LICENSE =================================
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
################################################################################

cmake_minimum_required(VERSION 3.13)

Expand Down
35 changes: 16 additions & 19 deletions samples/asio_sender_unicast/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
/* =========================== LICENSE =================================
*
* Copyright (C) 2016 - 2022 Continental Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
/********************************************************************************
* Copyright (c) 2016 Continental Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* =========================== LICENSE =================================
*/
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include <iostream>

Expand All @@ -33,15 +30,15 @@ int main()
asio::io_service io_service;

const asio::ip::udp::endpoint endpoint(asio::ip::make_address("127.0.0.1"), 14000);
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());
asio::ip::udp::socket udp_socket(io_service, endpoint.protocol());

int counter = 0;
for(;;)
{
std::string buffer_string = "Hello World " + std::to_string(counter);

std::cout << "Sending data \"" << buffer_string << "\"" << std::endl;
upd_socket.send_to(asio::buffer(buffer_string), endpoint);
udp_socket.send_to(asio::buffer(buffer_string), endpoint);
counter++;

std::this_thread::sleep_for(std::chrono::milliseconds(500));
Expand Down
30 changes: 14 additions & 16 deletions samples/udpcap_receiver_multicast/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# =========================== LICENSE =================================
#
# Copyright (C) 2016 - 2022 Continental Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
################################################################################
# Copyright (c) 2016 Continental Corporation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# =========================== LICENSE =================================
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
################################################################################

cmake_minimum_required(VERSION 3.13)

Expand Down
Loading

0 comments on commit dd9f1f6

Please sign in to comment.