Skip to content

Commit bff2e88

Browse files
committed
fix ip4 bitwise AND and ipv6 from_str parsing, add raw socket support, non-blocking connect with timeout, split tcp open() by role, update state_e enum names, improve examples and docs
1 parent d22070a commit bff2e88

File tree

13 files changed

+900
-521
lines changed

13 files changed

+900
-521
lines changed

CMakeLists.txt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ option(IP_SOCKETS_CPP_LITE_BUILD_EXAMPLES "Build with examples" OFF)
1515
# for initialise variables: CMAKE_INSTALL_INCLUDEDIR, CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_BINDIR
1616
include(GNUInstallDirs)
1717

18-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
19-
20-
if(WIN32)
21-
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
22-
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
23-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/bin)
24-
endforeach()
18+
# when this project is the root - set output to own bin/, otherwise inherit from parent
19+
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
20+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
21+
if(WIN32)
22+
foreach(OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES})
23+
string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG)
24+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_SOURCE_DIR}/bin)
25+
endforeach()
26+
endif()
2527
endif()
2628

2729
# =============================================================================
@@ -46,6 +48,12 @@ add_library (${PROJECT_NAME} INTERFACE)
4648
target_sources (${PROJECT_NAME} INTERFACE ${IP_SOCKETS_CPP_LITE_HEADERS})
4749
target_include_directories(${PROJECT_NAME} INTERFACE $<BUILD_INTERFACE: ${CMAKE_CURRENT_SOURCE_DIR}/include>
4850
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> )
51+
52+
# link pthreads on Linux (sockets use threads for examples and may be used in threaded apps)
53+
if(NOT WIN32)
54+
find_package(Threads)
55+
target_link_libraries(${PROJECT_NAME} INTERFACE Threads::Threads)
56+
endif()
4957
# it was need to show header files of INTERFACE target in IDEs like Visual Studio 2015 with CMake < 3.0
5058
#add_custom_target (${PROJECT_NAME}-ide SOURCES ${IP_SOCKETS_CPP_LITE_HEADERS})
5159

examples/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11

2-
if(NOT WIN32)
3-
find_package(Threads)
4-
target_link_libraries(${PROJECT_NAME} INTERFACE ${CMAKE_THREAD_LIBS_INIT})
5-
endif()
6-
72
function(add_example NAME_EXAMPLE REQUIRED_TARGETS)
83
string(CONCAT SUBPROJECT_NAME "ipsockets_" ${NAME_EXAMPLE})
94
message(STATUS " Adding ${PROJECT_NAME}::${NAME_EXAMPLE} example..")
105

116
add_executable (${SUBPROJECT_NAME} ${CMAKE_CURRENT_LIST_DIR}/${NAME_EXAMPLE}.cpp)
127
target_link_libraries (${SUBPROJECT_NAME} ${REQUIRED_TARGETS})
13-
set_target_properties (${SUBPROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/examples)
8+
set_target_properties (${SUBPROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
149
endfunction()
1510

1611
add_example(ip_address ip-sockets-cpp-lite)
1712
add_example(ip_prefix ip-sockets-cpp-lite)
1813
add_example(udp_socket ip-sockets-cpp-lite)
1914
add_example(tcp_socket ip-sockets-cpp-lite)
2015
add_example(resolve_host ip-sockets-cpp-lite)
16+
add_example(raw_socket ip-sockets-cpp-lite)
2117
add_example(http_server ip-sockets-cpp-lite)
2218
add_example(tcp_stream ip-sockets-cpp-lite)

examples/http_server.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
#include "tcp_socket.h"
23

34
#include <chrono>
@@ -18,11 +19,11 @@ using namespace ipsockets;
1819
using namespace std::chrono_literals;
1920

2021
#if true
21-
static const ip_type_e cfg_ip_type = v4;
22-
static const addr4_t cfg_server = "127.0.0.1:8080";
22+
static const ip_type_e ip_type = v4;
23+
static const addr4_t ip_server = "127.0.0.1:8080";
2324
#else
24-
static const ip_type_e cfg_ip_type = v6;
25-
static const addr6_t cfg_server = "[::1]:8080";
25+
static const ip_type_e ip_type = v6;
26+
static const addr6_t ip_server = "[::1]:8080";
2627
#endif
2728

2829
// ============================================================
@@ -45,8 +46,8 @@ struct mini_http_server_t {
4546
std::map<std::string, std::function<std::string()> > routes;
4647

4748
std::chrono::steady_clock::time_point start_time;
48-
uint64_t total_requests;
49-
std::map<std::string, uint64_t> route_count;
49+
uint64_t total_requests;
50+
std::map<std::string, uint64_t> route_count;
5051

5152
// ===== constructor / destructor =====
5253

@@ -86,10 +87,10 @@ struct mini_http_server_t {
8687
address_t client_addr;
8788

8889
// Main accept loop
89-
while (!must_die && server_socket.state == state_e::state_opened) {
90+
while (!must_die && server_socket.state == state_e::opened) {
9091

9192
tcp_client_t client = server_socket.accept(client_addr);
92-
if (client.state != state_e::state_opened) continue;
93+
if (client.state != state_e::opened) continue;
9394

9495
// Handle client in separate thread
9596
tasks.push_back(std::async(std::launch::async,&mini_http_server_t::handle_client,this,std::move(client),client_addr));
@@ -348,11 +349,11 @@ struct mini_http_server_t {
348349

349350
int main() {
350351

351-
mini_http_server_t<cfg_ip_type> server(cfg_server);
352+
mini_http_server_t<ip_type> server(ip_server);
352353

353354
std::cout << "\nMini HTTP Server is running!\n";
354355
std::cout << "Open your browser and visit:\n";
355-
std::cout << " http://" << cfg_server << "/\n";
356+
std::cout << " http://" << ip_server << "/\n";
356357
std::cout << "\nAvailable routes:\n";
357358
std::cout << " / - Home page\n";
358359
std::cout << " /about - About page\n";

0 commit comments

Comments
 (0)