Skip to content

Commit

Permalink
updated to use new WC repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Jul 27, 2024
1 parent 35bd6ec commit e2a4ade
Show file tree
Hide file tree
Showing 21 changed files with 115 additions and 61 deletions.
18 changes: 14 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.16.3)
cmake_minimum_required(VERSION 3.5)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
Expand All @@ -14,12 +14,16 @@ if (CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_options(
-g
-O0
-Wall
-fPIE
-march=native
)
else()
add_compile_options(
-O3
-march=native
-Wall
-fPIE
-g
)
endif()
Expand Down Expand Up @@ -59,14 +63,20 @@ include("${_scripts_src_path}/cmake/fetch_dependencies.cmake")
fetch_dependency("https://github.com/buildingcpp/include.git;main")
fetch_dependency("https://github.com/buildingcpp/system.git;main")

set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
set(WORK_CONTRACT_BUILD_BENCHMARK OFF)
fetch_dependency("https://github.com/buildingcpp/work_contract.git;main")

option(NETWORK_BUILD_DEMO "Build examples" ON)
option(NETWORK_BUILD_TEST "Build tests" ON)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/${PROJECT_NAME})
set(_${PROJECT_NAME}_dir ${CMAKE_CURRENT_SOURCE_DIR} CACHE STRING "")


add_subdirectory(src)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
2 changes: 1 addition & 1 deletion src/executable/3_packets/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace example_2
// a toy allocator
static auto constexpr buffer_size = 2048;
static auto constexpr allocator_capacity = 4;
using buffer = char[buffer_size];

static std::vector<char> pool(allocator_capacity);
static std::vector<std::int32_t> avail;
for (auto i = 0; i < allocator_capacity; ++i)
Expand Down
2 changes: 2 additions & 0 deletions src/library/network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ target_link_libraries(network
PUBLIC
include
system
work_contract
)

if (USE_KQUEUE)
Expand All @@ -32,6 +33,7 @@ target_include_directories(network
${_include_dir}/src
${_system_dir}/src
${_network_dir}/src
${_work_contract_dir}/src
${_fmt_src_path}/include
/usr/include/kqueue/
)
2 changes: 2 additions & 0 deletions src/library/network/ip/ip_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace bcpp::network


//=========================================================================
[[maybe_unused]]
static std::string to_string
(
ip_address ipAddress
Expand All @@ -89,6 +90,7 @@ namespace bcpp::network


//=============================================================================
[[maybe_unused]]
static std::ostream & operator <<
(
std::ostream & stream,
Expand Down
4 changes: 4 additions & 0 deletions src/library/network/ip/port_id.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace bcpp::network


//=========================================================================
[[maybe_unused]]
static port_id byte_swap
(
port_id source
Expand All @@ -55,6 +56,7 @@ namespace bcpp::network


//=========================================================================
[[maybe_unused]]
static std::string to_string
(
port_id portId
Expand All @@ -66,13 +68,15 @@ namespace bcpp::network

namespace literals
{
[[maybe_unused]]
static constexpr port_id operator""_port(unsigned long long int value){return port_id(value);}
}

} // namespace bcpp::network


//=============================================================================
[[maybe_unused]]
static std::ostream & operator <<
(
std::ostream & stream,
Expand Down
1 change: 1 addition & 0 deletions src/library/network/ip/socket_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ constexpr bcpp::network::socket_address::socket_address


//=============================================================================
[[maybe_unused]]
static std::ostream & operator <<
(
std::ostream & stream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace bcpp::network


//=============================================================================
[[maybe_unused]]
static std::ostream & operator <<
(
std::ostream & stream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ bcpp::network::virtual_network_interface::virtual_network_interface
{
networkInterfaceConfiguration_.ipAddress_ = in_addr_any;
poller_ = poller_->create({});
sendWorkContractGroup_ = std::make_unique<system::blocking_work_contract_group>(default_capacity);
receiveWorkContractGroup_ = std::make_unique<system::blocking_work_contract_group>(default_capacity);
sendWorkContractGroup_ = std::make_unique<work_contract_tree_type>(default_capacity);
receiveWorkContractGroup_ = std::make_unique<work_contract_tree_type>(default_capacity);
stopped_ = false;
}

Expand All @@ -29,8 +29,8 @@ bcpp::network::virtual_network_interface::virtual_network_interface
if (networkInterfaceConfiguration_.ipAddress_.is_valid())
{
poller_ = poller_->create(config.poller_);
sendWorkContractGroup_ = std::make_unique<system::blocking_work_contract_group>(config.capacity_);
receiveWorkContractGroup_ = std::make_unique<system::blocking_work_contract_group>(config.capacity_);
sendWorkContractGroup_ = std::make_unique<work_contract_tree_type>(config.capacity_);
receiveWorkContractGroup_ = std::make_unique<work_contract_tree_type>(config.capacity_);
stopped_ = false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ namespace bcpp::network

private:

using work_contract_tree_type = work_contract_tree<synchronization_mode::async>; // really we want blocking here but that implementation needs to be restored
using work_contract_type = work_contract<synchronization_mode::async>; // really we want blocking here but that implementation needs to be restored

template <socket_concept P, typename T>
P open_socket
(
Expand All @@ -128,8 +131,8 @@ namespace bcpp::network

network_interface_configuration networkInterfaceConfiguration_;
std::shared_ptr<poller> poller_;
std::unique_ptr<system::blocking_work_contract_group> sendWorkContractGroup_;
std::unique_ptr<system::blocking_work_contract_group> receiveWorkContractGroup_;
std::unique_ptr<work_contract_tree_type> sendWorkContractGroup_;
std::unique_ptr<work_contract_tree_type> receiveWorkContractGroup_;

std::atomic<bool> stopped_{true};

Expand Down
12 changes: 12 additions & 0 deletions src/library/network/packet/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ namespace bcpp::network

operator std::span<element_type const>() const;

operator bool() const;

private:

void release();
Expand All @@ -95,6 +97,7 @@ namespace bcpp::network

namespace literals
{
[[maybe_unused]]
static packet operator""_packet(char const * addr, std::size_t len){return packet({addr, len});}
}

Expand Down Expand Up @@ -286,6 +289,15 @@ inline void bcpp::network::packet::release
}


//=============================================================================
inline bcpp::network::packet::operator bool
(
) const
{
return (buffer_.size() > 0);
}


//=============================================================================
inline bcpp::network::packet::operator std::span<element_type const>
(
Expand Down
12 changes: 6 additions & 6 deletions src/library/network/socket/active_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ bcpp::network::active_socket<P>::socket
socket_address socketAddress,
configuration const & config,
event_handlers const & eventHandlers,
system::blocking_work_contract_group & sendWorkContractGroup,
system::blocking_work_contract_group & receiveWorkContractGroup,
work_contract_tree_type & sendWorkContractGroup,
work_contract_tree_type & receiveWorkContractGroup,
std::shared_ptr<poller> & p
) requires (udp_concept<P>)
try
Expand Down Expand Up @@ -49,8 +49,8 @@ bcpp::network::active_socket<P>::socket
ip_address ipAddress,
configuration const & config,
event_handlers const & eventHandlers,
system::blocking_work_contract_group & sendWorkContractGroup,
system::blocking_work_contract_group & receiveWorkContractGroup,
work_contract_tree_type & sendWorkContractGroup,
work_contract_tree_type & receiveWorkContractGroup,
std::shared_ptr<poller> & p
) requires (tcp_concept<P>)
try
Expand Down Expand Up @@ -89,8 +89,8 @@ bcpp::network::active_socket<P>::socket
system::file_descriptor fileDescriptor,
configuration const & config,
event_handlers const & eventHandlers,
system::blocking_work_contract_group & sendWorkContractGroup,
system::blocking_work_contract_group & recevieWorkContractGroup,
work_contract_tree_type & sendWorkContractGroup,
work_contract_tree_type & recevieWorkContractGroup,
std::shared_ptr<poller> & p
) requires (tcp_concept<P>)
try
Expand Down
16 changes: 10 additions & 6 deletions src/library/network/socket/active_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "./connect_result.h"

#include <library/system.h>
#include <library/work_contract.h>
#include <library/network/poller/poller.h>
#include <library/network/ip/socket_address.h>
#include <library/network/packet/packet.h>
Expand All @@ -30,6 +31,9 @@ namespace bcpp::network
{
public:

using work_contract_tree_type = work_contract_tree<synchronization_mode::async>; // really we want blocking here but that implementation needs to be restored
using work_contract_type = work_contract<synchronization_mode::async>; // really we want blocking here but that implementation needs to be restored

using traits = active_socket_traits<P>;

struct event_handlers
Expand Down Expand Up @@ -72,8 +76,8 @@ namespace bcpp::network
socket_address,
configuration const &,
event_handlers const &,
system::blocking_work_contract_group &,
system::blocking_work_contract_group &,
work_contract_tree_type &,
work_contract_tree_type &,
std::shared_ptr<poller> &
) requires (udp_concept<P>);

Expand All @@ -82,8 +86,8 @@ namespace bcpp::network
ip_address,
configuration const &,
event_handlers const &,
system::blocking_work_contract_group &,
system::blocking_work_contract_group &,
work_contract_tree_type &,
work_contract_tree_type &,
std::shared_ptr<poller> &
) requires (tcp_concept<P>);

Expand All @@ -92,8 +96,8 @@ namespace bcpp::network
system::file_descriptor,
configuration const &,
event_handlers const &,
system::blocking_work_contract_group &,
system::blocking_work_contract_group &,
work_contract_tree_type &,
work_contract_tree_type &,
std::shared_ptr<poller> &
) requires (tcp_concept<P>);

Expand Down
2 changes: 1 addition & 1 deletion src/library/network/socket/passive_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ bcpp::network::passive_socket::socket
socket_address socketAddress,
configuration const & config,
event_handlers const & eventHandlers,
system::blocking_work_contract_group & workContractGroup,
work_contract_tree_type & workContractGroup,
std::shared_ptr<poller> & p
)
try
Expand Down
8 changes: 7 additions & 1 deletion src/library/network/socket/passive_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

#include "./socket.h"
#include "./traits/traits.h"

#include <include/file_descriptor.h>

#include <library/network/poller/poller.h>
#include <library/network/ip/socket_address.h>

#include <library/work_contract.h>
#include <library/system.h>

#include <functional>
Expand All @@ -22,6 +25,9 @@ namespace bcpp::network
{
public:

using work_contract_tree_type = work_contract_tree<synchronization_mode::async>; // really we want blocking here but that implementation needs to be restored
using work_contract_type = work_contract<synchronization_mode::async>; // really we want blocking here but that implementation needs to be restored

using traits = tcp_listener_socket_traits;

static auto constexpr default_backlog{128};
Expand Down Expand Up @@ -55,7 +61,7 @@ namespace bcpp::network
socket_address,
configuration const &,
event_handlers const &,
system::blocking_work_contract_group &,
work_contract_tree_type &,
std::shared_ptr<poller> &
);

Expand Down
27 changes: 14 additions & 13 deletions src/library/network/socket/private/active_socket_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ bcpp::network::active_socket_impl<P>::socket_impl
socket_address socketAddress,
configuration const & config,
event_handlers const & eventHandlers,
system::blocking_work_contract_group & sendWorkContractGroup,
system::blocking_work_contract_group & receiveWorkContractGroup,
work_contract_tree_type & sendWorkContractGroup,
work_contract_tree_type & receiveWorkContractGroup,
std::shared_ptr<poller> const & p
) :
socket_base_impl(socketAddress, {.ioMode_ = config.ioMode_}, eventHandlers,
Expand Down Expand Up @@ -68,8 +68,8 @@ bcpp::network::active_socket_impl<P>::socket_impl
system::file_descriptor fileDescriptor,
configuration const & config,
event_handlers const & eventHandlers,
system::blocking_work_contract_group & sendWorkContractGroup,
system::blocking_work_contract_group & receiveWorkContractGroup,
work_contract_tree_type & sendWorkContractGroup,
work_contract_tree_type & receiveWorkContractGroup,
std::shared_ptr<poller> const & p
) requires (tcp_concept<P>) :
socket_base_impl({.ioMode_ = config.ioMode_}, eventHandlers, std::move(fileDescriptor),
Expand Down Expand Up @@ -336,11 +336,12 @@ void bcpp::network::active_socket_impl<P>::receive
(
) requires (tcp_concept<P>)
{
packet buffer = packetAllocationHandler_(id_, readBufferSize_);
if (auto bytesReceived = ::recv(fileDescriptor_.get(), buffer.data(), buffer.capacity(), 0); bytesReceived > 0)
if (!pendingReceivePacket_)
pendingReceivePacket_ = std::move(packetAllocationHandler_(id_, readBufferSize_));
if (auto bytesReceived = ::recv(fileDescriptor_.get(), pendingReceivePacket_.data(), pendingReceivePacket_.capacity(), 0); bytesReceived > 0)
{
buffer.resize(bytesReceived);
receiveHandler_(id_, std::move(buffer), peerSocketAddress_);
pendingReceivePacket_.resize(bytesReceived);
receiveHandler_(id_, std::move(pendingReceivePacket_), peerSocketAddress_);
if (get_bytes_available() > 0)
on_polled(); // there is more data so reschedule the work contract
return;
Expand Down Expand Up @@ -371,13 +372,13 @@ void bcpp::network::active_socket_impl<P>::receive
{
::sockaddr_in sockAddrIn;
::socklen_t addressLength = sizeof(sockAddrIn);

packet buffer = packetAllocationHandler_(id_, readBufferSize_);
if (auto bytesReceived = ::recvfrom(fileDescriptor_.get(), buffer.data(), buffer.capacity(), 0,
if (!pendingReceivePacket_)
pendingReceivePacket_ = std::move(packetAllocationHandler_(id_, readBufferSize_));
if (auto bytesReceived = ::recvfrom(fileDescriptor_.get(), pendingReceivePacket_.data(), pendingReceivePacket_.capacity(), 0,
reinterpret_cast<::sockaddr *>(&sockAddrIn), &addressLength); bytesReceived >= 0)
{
buffer.resize(bytesReceived);
receiveHandler_(id_, std::move(buffer), sockAddrIn);
pendingReceivePacket_.resize(bytesReceived);
receiveHandler_(id_, std::move(pendingReceivePacket_), sockAddrIn);
on_polled(); // there could be more ...
}
else
Expand Down
Loading

0 comments on commit e2a4ade

Please sign in to comment.