Skip to content

Commit 1c0da40

Browse files
authored
Merge pull request #1 from lpcvoid/testing_windows
Testing other platforms and testing CI
2 parents cb9ec3b + 8f93b9d commit 1c0da40

File tree

15 files changed

+243
-161
lines changed

15 files changed

+243
-161
lines changed

.github/workflows/cmake.yml

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

.github/workflows/test_matrix.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: CMake Build Matrix
2+
3+
on: [push]
4+
5+
env:
6+
BUILD_TYPE: Release
7+
8+
jobs:
9+
build:
10+
name: ${{ matrix.config.name }}
11+
runs-on: ${{ matrix.config.os }}
12+
strategy:
13+
fail-fast: false
14+
matrix:
15+
config:
16+
# - {
17+
# name: "Windows MSVC",
18+
# os: windows-latest,
19+
# build_type: "Release", cc: "cl", cxx: "cl",
20+
# environment_script: "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat"
21+
# }
22+
- {
23+
name: "Ubuntu gcc",
24+
os: ubuntu-latest,
25+
build_type: "Release", cc: "gcc", cxx: "g++"
26+
}
27+
- {
28+
name: "Ubuntu clang",
29+
os: ubuntu-latest,
30+
build_type: "Release", cc: "clang", cxx: "clang++"
31+
}
32+
- {
33+
name: "macOS clang",
34+
os: macos-latest,
35+
build_type: "Release", cc: "clang", cxx: "clang++"
36+
}
37+
steps:
38+
- uses: actions/checkout@v2
39+
with:
40+
submodules: 'true'
41+
42+
- name: Configure CMake
43+
run: cmake -B ${{github.workspace}}/build -DBUILD_TESTS=ON
44+
45+
- name: Build
46+
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
47+
48+
- name: Test
49+
working-directory: ${{github.workspace}}/build
50+
run: tests/test_libnetcpp

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@ cmake_minimum_required(VERSION 3.15)
22
project(netlib)
33
set(CMAKE_CXX_STANDARD 20)
44

5+
# we want the maximum amount of compiler bickering
6+
if(MSVC)
7+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
8+
else()
9+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
10+
endif()
11+
12+
find_package(Threads)
13+
514
set(NETLIB_SRC
615
src/netlib.hpp
716
src/socket.hpp
8-
src/error.hpp
917
src/client.hpp
1018
src/server.hpp
1119
src/service_resolver.hpp

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# cpp-net-lib
22

3-
***
4-
53
This lib is under heavy development and not really ready for use yet.
64

75
![test-status](https://github.com/lpcvoid/cpp-net-lib/actions/workflows/cmake.yml/badge.svg)
86

9-
Modern, header-only, compact and cross plattform C++ network/sockets library.
7+
Modern, header-only, compact and cross-platform C++ network/sockets library.
108
Don't mind the crappy name, I suck at naming things.
119

1210
### Why?

examples/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ set(EXAMPLE_SOURCES echo_server.cpp daytime_client.cpp threadpool.cpp time_clien
33
foreach (examplesource ${EXAMPLE_SOURCES})
44
string(REPLACE ".cpp" "" examplename ${examplesource})
55
add_executable(${examplename} ${examplesource})
6-
target_link_libraries(${examplename} pthread)
6+
target_link_libraries(${examplename} ${CMAKE_THREAD_LIBS_INIT})
77
endforeach (examplesource ${EXAMPLE_SOURCES})

examples/daytime_client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "../src/netlib.hpp"
22
#include <csignal>
33
#include <iostream>
4+
#include <chrono>
45

56
using namespace std::chrono_literals;
67

src/client.hpp

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "socket.hpp"
55
#include "thread_pool.hpp"
66
#include <future>
7+
#include <iostream>
78
#include <optional>
89
#include <variant>
910
#include <vector>
@@ -18,11 +19,12 @@ namespace netlib {
1819
addrinfo* _endpoint_addr = nullptr;
1920
netlib::thread_pool _thread_pool = netlib::thread_pool::create<1,1>();
2021

21-
inline std::pair<std::error_condition, std::chrono::milliseconds> wait_for_operation(socket_t sock, OperationClass op_class, std::chrono::milliseconds timeout) {
22+
static inline std::pair<std::error_condition, std::chrono::milliseconds> wait_for_operation(socket_t sock, OperationClass op_class, std::chrono::milliseconds timeout) {
2223
fd_set fdset;
2324
FD_ZERO(&fdset);
2425
FD_SET(sock, &fdset);
25-
timeval tv{.tv_sec = timeout.count() / 1000, .tv_usec=(timeout.count() % 1000) * 1000 };
26+
timeval tv{.tv_sec = timeout.count() / 1000, .tv_usec=static_cast<int32_t>((timeout.count() % 1000) * 1000)};
27+
std::cout << "tv.tv_sec = " << tv.tv_sec << ", usec=" << tv.tv_usec << std::endl;
2628
fd_set* fdset_ptr_read = ((op_class == OperationClass::read) || (op_class == OperationClass::both)) ? &fdset : nullptr;
2729
fd_set* fdset_ptr_write = ((op_class == OperationClass::write) || (op_class == OperationClass::both)) ? &fdset : nullptr;
2830
std::chrono::time_point<std::chrono::steady_clock> start = std::chrono::steady_clock::now();
@@ -31,19 +33,29 @@ namespace netlib {
3133
std::chrono::milliseconds ms_taken = std::chrono::duration_cast<std::chrono::milliseconds>((end - start));
3234
if (select_res == 1)
3335
{
34-
int32_t so_error = 0;
35-
socklen_t len = sizeof(so_error);
36-
getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
37-
if (so_error == 0) {
38-
//success
39-
return {{}, ms_taken};
40-
}
41-
return {static_cast<std::errc>(so_error), ms_taken};
36+
return {{}, ms_taken};
37+
38+
39+
40+
41+
// int32_t so_error = 0;
42+
// socklen_t len = sizeof(so_error);
43+
// getsockopt(sock, SOL_SOCKET, SO_ERROR, &so_error, &len);
44+
// if (so_error == 0) {
45+
// //success
46+
// std::cout << "client select no error" << std::endl;
47+
// return {{}, ms_taken};
48+
// }
49+
// std::error_condition error_condition = static_cast<std::errc>(so_error);
50+
// std::cout << "client select ret error" << error_condition.message() << std::endl;
51+
// return {static_cast<std::errc>(so_error), ms_taken};
4252
} else if (select_res == 0) {
4353
//timeout
54+
std::cout << "select ret 0 timeout" << std::endl;
4455
return {std::errc::timed_out, ms_taken};
4556
} else {
4657
//error
58+
std::cout << "select ret -1" << socket_get_last_error().message() << std::endl;
4759
return {socket_get_last_error(), ms_taken};
4860
}
4961
}
@@ -127,6 +139,8 @@ namespace netlib {
127139
}, data, timeout);
128140
}
129141
inline std::pair<std::size_t, std::error_condition> send(const std::vector<uint8_t> &data, std::optional<std::chrono::milliseconds> timeout) {
142+
143+
std::cout << "client send: " << data.size() << std::endl;
130144
if (!is_connected()) {
131145
return {0, std::errc::not_connected};
132146
}
@@ -161,22 +175,29 @@ namespace netlib {
161175
}
162176

163177
if (timeout.has_value() && timeout->count() < 0) {
178+
std::cout << "client recv timeout returned" << std::endl;
164179
return {{}, std::errc::timed_out};
165180
}
166181

167182
auto wait_res = wait_for_operation(_socket.value().get_raw().value(), OperationClass::read, timeout.value());
168183
if (wait_res.first) {
184+
std::cout << "client recv select timeout returned" << std::endl;
169185
return {{}, wait_res.first};
170186
}
171187

188+
std::cout << "client recv after wait" << std::endl;
189+
172190
std::vector<uint8_t> data(byte_count, 0);
173191
ssize_t recv_res = ::recv(_socket.value().get_raw().value(), data.data(), byte_count, 0);
174192
if (recv_res > 0) {
193+
std::cout << "client recv " << recv_res << std::endl;
175194
data.resize(recv_res);
176195
return {data, {}};
177196
} else if (recv_res == 0){
197+
std::cout << "client recv 0" << std::endl;
178198
return {{}, std::errc::connection_aborted};
179199
}
200+
std::cout << "client recv error: " << recv_res << " : " << socket_get_last_error().message() << std::endl;
180201
return {{}, socket_get_last_error()};
181202
}
182203

@@ -190,10 +211,12 @@ namespace netlib {
190211
if (_endpoint_addr) {
191212
freeaddrinfo(_endpoint_addr);
192213
}
214+
215+
std::cout << "client disconnected" << std::endl;
193216
return {};
194217
}
195218
inline bool is_connected() {
196-
return _socket.has_value();
219+
return _socket.has_value() && _socket->is_valid();
197220
}
198221
[[nodiscard]] inline std::optional<netlib::socket> get_socket() const {
199222
return _socket;

src/error.hpp

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

0 commit comments

Comments
 (0)