Skip to content

Commit 92da76b

Browse files
authored
add code coverage (#38)
* add code coverage * update coverage action * add more testing and move to embedded compiler flags * remove unused workflow * try to fix windows testing * fix window action
1 parent 008a20d commit 92da76b

File tree

5 files changed

+324
-77
lines changed

5 files changed

+324
-77
lines changed

CMakeLists.txt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ SET(LICENSE "MIT")
1111

1212
project(cppsocket VERSION ${project_version} LANGUAGES CXX)
1313

14-
if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
15-
add_definitions(-DLINUX)
16-
else()
17-
add_definitions(-DWINDOWS)
18-
endif()
19-
2014
include_directories(inc)
2115

2216
enable_testing()

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.6
1+
0.0.7

inc/cppsocket.hpp

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
#ifdef LINUX
2+
#ifdef __GNUC__
33

44
#include <arpa/inet.h>
55
#include <netinet/in.h>
@@ -46,7 +46,7 @@ namespace com::github::socket
4646
Socket(Socket&) = delete;
4747

4848
Socket() noexcept(false)
49-
#ifdef LINUX
49+
#ifdef __GNUC__
5050
: m_fd(-1)
5151
#else
5252
: m_fd(INVALID_SOCKET)
@@ -55,7 +55,7 @@ namespace com::github::socket
5555
init();
5656
}
5757

58-
#ifdef LINUX
58+
#ifdef __GNUC__
5959
explicit Socket(const int filedescriptor) noexcept
6060
#else
6161
explicit Socket(SOCKET filedescriptor) noexcept
@@ -72,7 +72,7 @@ namespace com::github::socket
7272

7373
virtual ~Socket()
7474
{
75-
#ifdef LINUX
75+
#ifdef __GNUC__
7676
static_cast<void>(::shutdown(m_fd, SHUT_RDWR));
7777
static_cast<void>(::close(m_fd));
7878
#else
@@ -84,7 +84,7 @@ namespace com::github::socket
8484
void initSocket(const int domain, const int type, const int protocol) noexcept(false)
8585
{
8686
m_fd = ::socket(domain, type, protocol);
87-
#ifdef LINUX
87+
#ifdef __GNUC__
8888
if (m_fd < 0)
8989
#else
9090
if (m_fd == INVALID_SOCKET)
@@ -94,7 +94,7 @@ namespace com::github::socket
9494
}
9595
}
9696

97-
#ifdef LINUX
97+
#ifdef __GNUC__
9898
auto accept(sockaddr* address, socklen_t* addrlen) const noexcept -> int
9999
#else
100100
auto accept(sockaddr* address, socklen_t* addrlen) const noexcept -> SOCKET
@@ -103,7 +103,7 @@ namespace com::github::socket
103103
return ::accept(m_fd, address, addrlen);
104104
}
105105

106-
#ifdef LINUX
106+
#ifdef __GNUC__
107107
[[nodiscard]] auto accept(std::string& ipAddr, uint16_t& port) const noexcept(false) -> int
108108
#else
109109
[[nodiscard]] auto accept(std::string& ipAddr, uint16_t& port) const noexcept(false) -> SOCKET
@@ -112,7 +112,7 @@ namespace com::github::socket
112112
sockaddr_in addr = {};
113113
socklen_t length = sizeof(addr);
114114
auto retval = accept(reinterpret_cast<sockaddr*>(&addr), &length);
115-
#ifdef LINUX
115+
#ifdef __GNUC__
116116
if (retval > 0)
117117
#else
118118
if (retval != INVALID_SOCKET)
@@ -169,7 +169,7 @@ namespace com::github::socket
169169

170170
[[nodiscard]] auto read(void* buffer, size_t length) const noexcept -> ssize_t
171171
{
172-
#ifdef LINUX
172+
#ifdef __GNUC__
173173
return ::read(m_fd, buffer, length);
174174
#else
175175
return ::recv(m_fd, reinterpret_cast<char*>(buffer), length, 0);
@@ -178,7 +178,7 @@ namespace com::github::socket
178178

179179
[[nodiscard]] auto readfrom(void* buffer, size_t length, sockaddr* from, socklen_t* fromlength) const noexcept -> ssize_t
180180
{
181-
#ifdef LINUX
181+
#ifdef __GNUC__
182182
return ::recvfrom(m_fd, buffer, length, 0, from, fromlength);
183183
#else
184184
return ::recvfrom(m_fd, reinterpret_cast<char*>(buffer), length, 0, from, fromlength);
@@ -201,7 +201,7 @@ namespace com::github::socket
201201

202202
auto send(const void* buffer, size_t length) const noexcept -> ssize_t
203203
{
204-
#ifdef LINUX
204+
#ifdef __GNUC__
205205
return ::send(m_fd, buffer, length, 0);
206206
#else
207207
return ::send(m_fd, reinterpret_cast<const char*>(buffer), length, 0);
@@ -210,7 +210,7 @@ namespace com::github::socket
210210

211211
auto sendto(const void* buffer, size_t length, const sockaddr* toaddr, int tolength) const noexcept -> ssize_t
212212
{
213-
#ifdef LINUX
213+
#ifdef __GNUC__
214214
return ::sendto(m_fd, buffer, length, 0, toaddr, tolength);
215215
#else
216216
return ::sendto(m_fd, reinterpret_cast<const char*>(buffer), length, 0, toaddr, tolength);
@@ -224,40 +224,25 @@ namespace com::github::socket
224224
return sendto(buffer, length, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
225225
}
226226

227-
[[nodiscard]] auto select(fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timeval* timeout) const noexcept -> ssize_t
227+
[[nodiscard]] auto selectWrite(timeval& timeout) const noexcept -> bool
228228
{
229-
return ::select(m_fd, readfds, writefds, exceptfds, timeout);
229+
fd_set set;
230+
FD_ZERO(&set);
231+
FD_SET(m_fd, &set);
232+
return (::select(m_fd+1, nullptr, &set, nullptr, &timeout) == 1);
230233
}
231234

232-
[[nodiscard]] auto select(fd_set& readfds, fd_set& writefds, fd_set& exceptfds, timeval& timeout) const noexcept -> ssize_t
235+
[[nodiscard]] auto selectRead(timeval& timeout) const noexcept -> bool
233236
{
234-
return ::select(m_fd, &readfds, &writefds, &exceptfds, &timeout);
235-
}
236-
237-
[[nodiscard]] auto select(fd_set& readfds, fd_set& writefds, fd_set& exceptfds, const int milliseconds) const noexcept -> ssize_t
238-
{
239-
ssize_t retval;
240-
if (milliseconds < 0)
241-
{
242-
retval = ::select(m_fd, &readfds, &writefds, &exceptfds, nullptr);
243-
}
244-
else
245-
{
246-
timeval timeout
247-
{
248-
.tv_sec = static_cast<time_t>(milliseconds * MILLISECONDS_PER_SECOND),
249-
.tv_usec = static_cast<time_t>(milliseconds / MILLISECONDS_PER_SECOND)
250-
};
251-
252-
retval = select(readfds, writefds, exceptfds, timeout);
253-
}
254-
255-
return retval;
237+
fd_set set;
238+
FD_ZERO(&set);
239+
FD_SET(m_fd, &set);
240+
return (::select(m_fd+1, &set, nullptr, nullptr, &timeout) == 1);
256241
}
257242

258243
auto getsockopt(const int level, const int optname, void *optval, socklen_t* optlen) const noexcept -> ssize_t
259244
{
260-
#ifdef LINUX
245+
#ifdef __GNUC__
261246
return ::getsockopt(m_fd, level, optname, optval, optlen);
262247
#else
263248
return ::getsockopt(m_fd, level, optname, reinterpret_cast<char*>(optval), optlen);
@@ -266,13 +251,22 @@ namespace com::github::socket
266251

267252
auto setsockopt(const int level, const int optname, const void *optval, const int optlen) const noexcept -> ssize_t
268253
{
269-
#ifdef LINUX
254+
#ifdef __GNUC__
270255
return ::setsockopt(m_fd, level, optname, optval, optlen);
271256
#else
272257
return ::setsockopt(m_fd, level, optname, reinterpret_cast<const char*>(optval), optlen);
273258
#endif
274259
}
275260

261+
#ifdef __GNUC__
262+
auto getFd() const noexcept -> int
263+
#else
264+
auto getFd() const noexcept -> SOCKET
265+
#endif
266+
{
267+
return m_fd;
268+
}
269+
276270
protected:
277271

278272
[[nodiscard]] static auto getIpAddress(const in_addr inaddr) noexcept(false) -> std::string
@@ -292,7 +286,7 @@ namespace com::github::socket
292286
*/
293287
void init() noexcept(false)
294288
{
295-
#ifdef WINDOWS
289+
#ifdef _WIN32
296290
std::call_once(m_onetime, [this]()
297291
{
298292
if (::WSAStartup(MAKEWORD(2, 2), &m_wsaData) != 0)
@@ -414,7 +408,7 @@ namespace com::github::socket
414408
constexpr static unsigned int FIVE_SECONDS = 5; // NOLINT
415409

416410
static std::array<unsigned char, COOKIE_LEN> m_cookie; // NOLINT
417-
#ifdef LINUX
411+
#ifdef __GNUC__
418412
int m_fd; // NOLINT
419413
#else
420414
SOCKET m_fd; // NOLINT
@@ -456,7 +450,7 @@ namespace com::github::socket
456450
initAddr(ipAddr, port, addr);
457451

458452
auto ret = ::connect(m_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
459-
#ifdef LINUX
453+
#ifdef __GNUC__
460454
if (ret < 0)
461455
#else
462456
if (ret == SOCKET_ERROR)
@@ -471,7 +465,7 @@ namespace com::github::socket
471465
void init() noexcept(false)
472466
{
473467
int flag = 1;
474-
#ifdef LINUX
468+
#ifdef __GNUC__
475469
if (setsockopt(IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) != 0)
476470
#else
477471
if (setsockopt(IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&flag), sizeof(flag)) == SOCKET_ERROR)
@@ -524,7 +518,7 @@ namespace com::github::socket
524518
sockaddr_in client = {};
525519
socklen_t clientLen = sizeof(client);
526520
auto fileD = Socket::accept(reinterpret_cast<sockaddr*>(&client), &clientLen);
527-
#ifdef LINUX
521+
#ifdef __GNUC__
528522
if (fileD < 0)
529523
#else
530524
if (fileD == INVALID_SOCKET)
@@ -541,7 +535,7 @@ namespace com::github::socket
541535
void init() noexcept(false)
542536
{
543537
int opt = 1;
544-
#ifdef LINUX
538+
#ifdef __GNUC__
545539
if (setsockopt(SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) != 0)
546540
#else
547541
if (setsockopt(SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&opt), sizeof(opt)) == SOCKET_ERROR)
@@ -590,7 +584,7 @@ namespace com::github::socket
590584
[[nodiscard]] auto read(void* buffer, size_t length) noexcept -> ssize_t
591585
{
592586
socklen_t fromlength = sizeof(m_serverAddr);
593-
#ifdef LINUX
587+
#ifdef __GNUC__
594588
return ::recvfrom(m_fd, buffer, length, 0, reinterpret_cast<sockaddr*>(&m_serverAddr), &fromlength);
595589
#else
596590
return ::recvfrom(m_fd, reinterpret_cast<char*>(buffer), length, 0, reinterpret_cast<sockaddr*>(&m_serverAddr), &fromlength);
@@ -599,7 +593,7 @@ namespace com::github::socket
599593

600594
auto send(const void* buffer, size_t length) noexcept -> ssize_t
601595
{
602-
#ifdef LINUX
596+
#ifdef __GNUC__
603597
return ::sendto(m_fd, buffer, length, 0, reinterpret_cast<sockaddr*>(&m_serverAddr), sizeof(m_serverAddr));
604598
#else
605599
return ::sendto(m_fd, reinterpret_cast<const char*>(buffer), length, 0, reinterpret_cast<sockaddr*>(&m_serverAddr), sizeof(m_serverAddr));
@@ -637,7 +631,7 @@ namespace com::github::socket
637631
[[nodiscard]] auto read(void* buffer, size_t length) noexcept -> ssize_t
638632
{
639633
socklen_t fromlength = sizeof(m_clientAddr);
640-
#ifdef LINUX
634+
#ifdef __GNUC__
641635
return ::recvfrom(m_fd, buffer, length, 0, reinterpret_cast<sockaddr*>(&m_clientAddr), &fromlength);
642636
#else
643637
return ::recvfrom(m_fd, reinterpret_cast<char*>(buffer), length, 0, reinterpret_cast<sockaddr*>(&m_clientAddr), &fromlength);
@@ -646,7 +640,7 @@ namespace com::github::socket
646640

647641
auto send(const void* buffer, size_t length) noexcept -> ssize_t
648642
{
649-
#ifdef LINUX
643+
#ifdef __GNUC__
650644
return ::sendto(m_fd, buffer, length, 0, reinterpret_cast<sockaddr*>(&m_clientAddr), sizeof(m_clientAddr));
651645
#else
652646
return ::sendto(m_fd, reinterpret_cast<const char*>(buffer), length, 0, reinterpret_cast<sockaddr*>(&m_clientAddr), sizeof(m_clientAddr));
@@ -658,7 +652,7 @@ namespace com::github::socket
658652
void init() noexcept(false)
659653
{
660654
int opt = 1;
661-
#ifdef LINUX
655+
#ifdef __GNUC__
662656
if (setsockopt(SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) != 0)
663657
#else
664658
if (setsockopt(SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&opt), sizeof(opt)) == SOCKET_ERROR)
@@ -679,7 +673,7 @@ namespace com::github::socket
679673
auto operator=(SecureTcpClient&&) -> SecureTcpClient& = delete;
680674
SecureTcpClient(SecureTcpClient&) = delete;
681675

682-
#ifdef LINUX
676+
#ifdef __GNUC__
683677
SecureTcpClient(const int filedescriptor, SSL_CTX* sslctx) noexcept(false)
684678
#else
685679
SecureTcpClient(SOCKET filedescriptor, SSL_CTX *sslctx) noexcept(false)
@@ -717,7 +711,7 @@ namespace com::github::socket
717711
initAddr(ipAddr, port, addr);
718712

719713
auto ret = connect(reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
720-
#ifdef LINUX
714+
#ifdef __GNUC__
721715
if (ret < 0)
722716
#else
723717
if (ret == SOCKET_ERROR)
@@ -765,7 +759,7 @@ namespace com::github::socket
765759
void init() noexcept(false)
766760
{
767761
m_fd = ::socket(AF_INET, SOCK_STREAM, 0);
768-
#ifdef LINUX
762+
#ifdef __GNUC__
769763
if (m_fd < 0)
770764
#else
771765
if (m_fd == INVALID_SOCKET)
@@ -775,7 +769,7 @@ namespace com::github::socket
775769
}
776770

777771
int flag = 1;
778-
#ifdef LINUX
772+
#ifdef __GNUC__
779773
if (setsockopt(IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag)) != 0)
780774
#else
781775
if (setsockopt(IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&flag), sizeof(flag)) == SOCKET_ERROR)
@@ -861,7 +855,7 @@ namespace com::github::socket
861855
[[nodiscard]] auto accept(std::string& ipAddr, uint16_t& port) noexcept(false) -> SecureTcpClient
862856
{
863857
auto fileD = Socket::accept(ipAddr, port);
864-
#ifdef LINUX
858+
#ifdef __GNUC__
865859
if (fileD < 0)
866860
#else
867861
if (fileD == INVALID_SOCKET)
@@ -885,7 +879,7 @@ namespace com::github::socket
885879
sockaddr_in client = {};
886880
socklen_t clientLen = sizeof(client);
887881
auto fileD = Socket::accept(reinterpret_cast<sockaddr*>(&client), &clientLen);
888-
#ifdef LINUX
882+
#ifdef __GNUC__
889883
if (fileD < 0)
890884
#else
891885
if (fileD == INVALID_SOCKET)
@@ -906,7 +900,7 @@ namespace com::github::socket
906900
Socket::init();
907901

908902
m_fd = ::socket(AF_INET, SOCK_STREAM, 0);
909-
#ifdef LINUX
903+
#ifdef __GNUC__
910904
if (m_fd < 0)
911905
#else
912906
if (m_fd == INVALID_SOCKET)
@@ -916,7 +910,7 @@ namespace com::github::socket
916910
}
917911

918912
int opt = 1;
919-
#ifdef LINUX
913+
#ifdef __GNUC__
920914
if (setsockopt(SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) != 0)
921915
#else
922916
if (setsockopt(SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&opt), sizeof(opt)) == SOCKET_ERROR)
@@ -1034,7 +1028,7 @@ namespace com::github::socket
10341028
void init(const std::string& keyFile, const std::string& certFile) noexcept(false)
10351029
{
10361030
m_fd = ::socket(AF_INET, SOCK_DGRAM, 0);
1037-
#ifdef LINUX
1031+
#ifdef __GNUC__
10381032
if (m_fd < 0)
10391033
#else
10401034
if (m_fd == INVALID_SOCKET)
@@ -1185,7 +1179,7 @@ namespace com::github::socket
11851179
SSL_CTX_set_cookie_verify_cb(m_sslctx, &Socket::verifyCookie);
11861180

11871181
m_fd = ::socket(AF_INET, SOCK_DGRAM, 0);
1188-
#ifdef LINUX
1182+
#ifdef __GNUC__
11891183
if (m_fd < 0)
11901184
#else
11911185
if (m_fd == INVALID_SOCKET)
@@ -1195,7 +1189,7 @@ namespace com::github::socket
11951189
}
11961190

11971191
int opt = 1;
1198-
#ifdef LINUX
1192+
#ifdef __GNUC__
11991193
if (setsockopt(SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)) != 0)
12001194
#else
12011195
if (setsockopt(SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char*>(&opt), sizeof(opt)) == SOCKET_ERROR)

0 commit comments

Comments
 (0)