Skip to content

Commit f10671a

Browse files
committed
Extract OS Detection logic to a header file
1 parent ceec18b commit f10671a

File tree

9 files changed

+82
-45
lines changed

9 files changed

+82
-45
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
## Author Francois Michaut
55
##
66
## Started on Sun Aug 28 19:26:51 2022 Francois Michaut
7-
## Last update Tue May 9 11:51:16 2023 Francois Michaut
7+
## Last update Wed May 10 08:24:03 2023 Francois Michaut
88
##
99
## CMakeLists.txt : CMake to build the CppSockets library
1010
##
@@ -40,5 +40,5 @@ add_custom_target(test
4040
COMMAND ${CMAKE_COMMAND}
4141
--build "${CMAKE_CURRENT_BINARY_DIR}/tests" -- --quiet
4242
COMMAND ${CMAKE_MAKE_PROGRAM}
43-
-C "${CMAKE_CURRENT_BINARY_DIR}/tests" test
43+
-C "${CMAKE_CURRENT_BINARY_DIR}/tests" test ARGS=--output-on-failure
4444
)

include/CppSockets/Address.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
** Author Francois Michaut
55
**
66
** Started on Sun Feb 13 17:09:05 2022 Francois Michaut
7-
** Last update Thu Feb 9 08:55:11 2023 Francois Michaut
7+
** Last update Tue Jul 18 13:34:19 2023 Francois Michaut
88
**
99
** Address.hpp : Interface to represent network addresses
1010
*/
1111

1212
#pragma once
1313

14+
#include <cstdint>
1415
#include <string>
1516
#include <type_traits>
1617

include/CppSockets/OSDetection.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
** Project CppSockets, 2023
3+
**
4+
** Author Francois Michaut
5+
**
6+
** Started on Tue May 9 23:20:20 2023 Francois Michaut
7+
** Last update Sun May 14 14:03:20 2023 Francois Michaut
8+
**
9+
** OSDetection.hpp : OS Detection macros
10+
*/
11+
12+
#pragma once
13+
14+
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
15+
#define OS_WINDOWS
16+
#elif defined (__APPLE__)
17+
#define OS_UNIX
18+
#define OS_APPLE
19+
#elif defined(__linux__)
20+
#define OS_UNIX
21+
#define OS_LINUX
22+
#elif defined(__unix__)
23+
#define OS_UNIX
24+
#else
25+
#error "Unknown compiler, please open a issue or pull request to support your compiler"
26+
#endif

include/CppSockets/Socket.hpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
** Author Francois Michaut
55
**
66
** Started on Sat Jan 15 01:17:42 2022 Francois Michaut
7-
** Last update Tue Feb 7 22:31:12 2023 Francois Michaut
7+
** Last update Tue May 9 23:31:24 2023 Francois Michaut
88
**
99
** Socket.hpp : Portable C++ socket class
1010
*/
1111

1212
#pragma once
1313

14-
#ifdef _WIN32
15-
#define NOMINMAX
16-
#include <winsock2.h>
17-
using RawSocketType=SOCKET;
18-
using socklen_t=int;
14+
#include "CppSockets/OSDetection.hpp"
15+
16+
#ifdef OS_WINDOWS
17+
#define NOMINMAX
18+
#include <winsock2.h>
19+
using RawSocketType=SOCKET;
20+
using socklen_t=int;
1921
#else
20-
#include <sys/socket.h>
21-
using RawSocketType=int;
22+
#include <sys/socket.h>
23+
using RawSocketType=int;
2224
#endif
2325

2426
#include <memory>

include/CppSockets/TlsSocket.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Sep 14 20:51:23 2022 Francois Michaut
7-
** Last update Mon May 8 19:57:05 2023 Francois Michaut
7+
** Last update Tue May 9 23:29:53 2023 Francois Michaut
88
**
99
** SecureSocket.hpp : TLS socket wrapper using openssl
1010
*/
1111

1212
#pragma once
1313

14-
#ifdef _WIN32
14+
#include "CppSockets/OSDetection.hpp"
15+
16+
#ifdef OS_WINDOWS
1517
// TODO Currently disabling TlsSocket for windows
1618
#else
1719

1820
#include "CppSockets/IPv4.hpp"
19-
#include <CppSockets/Socket.hpp>
21+
#include "CppSockets/Socket.hpp"
2022

2123
#include <functional>
2224

source/Socket.cpp

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@
44
** Author Francois Michaut
55
**
66
** Started on Sat Jan 15 01:27:40 2022 Francois Michaut
7-
** Last update Sun Feb 19 11:01:09 2023 Francois Michaut
7+
** Last update Tue May 9 23:33:46 2023 Francois Michaut
88
**
99
** Socket.cpp : Protable C++ socket class implementation
1010
*/
1111

12-
#include <arpa/inet.h>
13-
#include <cerrno>
14-
#include <cstring>
15-
#include <stdexcept>
16-
#include <unistd.h>
12+
#include "CppSockets/IPv4.hpp"
13+
#include "CppSockets/OSDetection.hpp"
14+
#include "CppSockets/Socket.hpp"
1715

18-
#ifdef _WIN32
19-
#include <ws2tcpip.h>
16+
#ifdef OS_WINDOWS
17+
#include <ws2tcpip.h>
2018
#else
21-
#include <fcntl.h>
22-
#include <netinet/in.h>
19+
#include <fcntl.h>
20+
#include <netinet/in.h>
2321
// To match windows's constants
24-
static constexpr int INVALID_SOCKET = -1;
25-
static constexpr int SOCKET_ERROR = -1;
22+
static constexpr int INVALID_SOCKET = -1;
23+
static constexpr int SOCKET_ERROR = -1;
2624
#endif
2725

2826
static constexpr int BUFF_SIZE = 4096;
2927

3028
#include <array>
29+
#include <cerrno>
30+
#include <cstring>
31+
#include <stdexcept>
3132

32-
#include "CppSockets/IPv4.hpp"
33-
#include "CppSockets/Socket.hpp"
33+
#include <arpa/inet.h>
34+
#include <unistd.h>
3435

3536
// TODO add exceptions on error retunrs
3637
// TODO throw custom exceptions on invalid status (eg: socket already connected)
@@ -58,7 +59,7 @@ namespace CppSockets {
5859
}
5960

6061
Socket::~Socket() {
61-
#ifdef _WIN32
62+
#ifdef OS_WINDOWS
6263
closesocket(raw_socket);
6364
#else
6465
close(sockfd);
@@ -79,14 +80,14 @@ namespace CppSockets {
7980
}
8081

8182
char *Socket::strerror(int err) {
82-
#ifdef _WIN32
83+
#ifdef OS_WINDOWS
8384
#else
8485
return ::strerror(err);
8586
#endif
8687
}
8788

8889
int Socket::get_errno() {
89-
#ifdef _WIN32
90+
#ifdef OS_WINDOWS
9091
#else
9192
return errno;
9293
#endif

source/TlsSocket.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
** Author Francois Michaut
55
**
66
** Started on Wed Sep 14 21:04:42 2022 Francois Michaut
7-
** Last update Wed Oct 12 20:10:27 2022 Francois Michaut
7+
** Last update Tue May 9 23:34:24 2023 Francois Michaut
88
**
99
** SecureSocket.cpp : TLS socket wrapper implementation
1010
*/
1111

12-
#ifdef _WIN32
12+
#include "CppSockets/OSDetection.hpp"
13+
#include "CppSockets/TlsSocket.hpp"
14+
15+
#ifdef OS_WINDOWS
1316
// TODO Currently disabling TlsSocket for windows
1417
#else
1518

16-
#include "CppSockets/TlsSocket.hpp"
17-
1819
#include <openssl/err.h>
1920
#include <openssl/ssl.h>
2021

source/init.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@
44
** Author Francois Michaut
55
**
66
** Started on Thu Sep 15 14:24:25 2022 Francois Michaut
7-
** Last update Thu Sep 15 21:06:20 2022 Francois Michaut
7+
** Last update Tue May 9 23:34:46 2023 Francois Michaut
88
**
99
** init.cpp : Startup/Cleanup functions implementation
1010
*/
1111

12-
#ifdef _WIN32
13-
#include <winsock2.h>
12+
#include "CppSockets/OSDetection.hpp"
13+
14+
#ifdef OS_WINDOWS
15+
#include <winsock2.h>
1416
#else
15-
#include <openssl/err.h>
16-
#include <openssl/ssl.h>
17+
#include <openssl/err.h>
18+
#include <openssl/ssl.h>
1719
#endif
1820

1921
namespace CppSockets {
2022
void init(bool init_ssl, bool init_wsa) {
21-
#ifdef _WIN32
23+
#ifdef OS_WINDOWS
2224
if (init_wsa) {
2325
WSADATA wsa_data;
2426

@@ -41,7 +43,7 @@ namespace CppSockets {
4143
}
4244

4345
void deinit(bool deinit_ssl, bool deinit_wsa) {
44-
#ifdef _WIN32
46+
#ifdef OS_WINDOWS
4547
if (deinit_wsa) {
4648
if (WSACleanup() == SOCKET_ERROR) {
4749
// TODO use FormatMessage to get the error string

tests/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
## Author Francois Michaut
55
##
66
## Started on Mon Feb 14 19:35:41 2022 Francois Michaut
7-
## Last update Sun Aug 28 19:55:12 2022 Francois Michaut
7+
## Last update Wed May 10 09:47:41 2023 Francois Michaut
88
##
99
## CMakeLists.txt : CMake building and running tests for CppSockets
1010
##
@@ -19,14 +19,16 @@ include(CTest)
1919

2020
create_test_sourcelist(TestFiles test_driver.cpp
2121
TestSockets.cpp
22-
)
22+
)
2323

2424
add_executable(unit_tests
2525
../source/Address.cpp
2626
../source/Socket.cpp
2727
../source/IPv4.cpp
2828
${TestFiles}
29-
)
29+
)
30+
31+
target_compile_definitions(unit_tests PRIVATE DEBUG)
3032

3133
foreach (test ${TestFiles})
3234
if (NOT ${test} STREQUAL test_driver.cpp)

0 commit comments

Comments
 (0)