44** Author Francois Michaut
55**
66** Started on Sat Jan 15 01:27:40 2022 Francois Michaut
7- ** Last update Wed Sep 14 22:19:18 2022 Francois Michaut
7+ ** Last update Sun Feb 19 11:01:09 2023 Francois Michaut
88**
99** Socket.cpp : Protable C++ socket class implementation
1010*/
@@ -27,43 +27,26 @@ static constexpr int SOCKET_ERROR = -1;
2727
2828static constexpr int BUFF_SIZE = 4096 ;
2929
30- // TODO add exceptions on error retunrs
31- // TODO throw custom exceptions on invalid status (eg: socket already connected)
30+ # include < array >
31+
3232#include " CppSockets/IPv4.hpp"
3333#include " CppSockets/Socket.hpp"
3434
35+ // TODO add exceptions on error retunrs
36+ // TODO throw custom exceptions on invalid status (eg: socket already connected)
3537namespace CppSockets {
36- void Socket::init () {
37- #ifdef _WIN32
38- static bool init_done = false ;
39- WSADATA wsa_data;
40-
41- if (!init_done) {
42- // TODO need to call WSACleanup too
43- if (WSAStartup (MAKEWORD (2 , 2 ), &wsa_data) != 0 ) {
44- throw std::runtime_error (std::string (" WASStartup Failed : " ) + std::strerror (errno));
45- }
46- init_done = true ;
47- }
48- #endif
49- }
50-
5138 Socket::Socket (int domain, int type, int protocol, int sockfd) :
5239 domain (domain), type(type), protocol(protocol), sockfd(sockfd)
53- {
54- init ();
55- }
40+ {}
5641
5742 Socket::Socket (int domain, int type, int protocol) :
58- domain (domain), type(type), protocol(protocol)
43+ domain (domain), type(type), protocol(protocol), sockfd(::socket(domain, type, protocol))
5944 {
60- init ();
61- sockfd = ::socket (domain, type, protocol);
6245 if (sockfd == INVALID_SOCKET )
6346 throw std::runtime_error (std::string (" Failed to create socket : " ) + std::strerror (errno));
6447 }
6548
66- Socket::Socket (Socket &&other) noexcept { // NOLINT
49+ Socket::Socket (Socket &&other) noexcept { // NOLINT(cppcoreguidelines-pro-type-member-init, hicpp-member-init)
6750 *this = std::move (other);
6851 }
6952
@@ -111,7 +94,7 @@ namespace CppSockets {
11194
11295 std::string Socket::read (std::size_t len) {
11396 std::array<char , BUFF_SIZE > buff = {0 };
114- std::string res;
97+ std::string res; // TODO use a stringstream
11598 std::size_t total = 0 ;
11699 std::size_t nb = 1 ;
117100
@@ -165,6 +148,11 @@ namespace CppSockets {
165148 return this ->bind (inet_addr (addr.c_str ()), port);
166149 }
167150
151+ int Socket::bind (const IEndpoint &endpoint) {
152+ // TODO: this only works for IPv4. Need to switch getFamily() to handle IPv6 / AF_UNIX ...
153+ return this ->bind (endpoint.getAddr ().getAddress (), endpoint.getPort ());
154+ }
155+
168156 int Socket::bind (std::uint32_t s_addr, int port) {
169157 struct sockaddr_in addr = {};
170158 int ret = 0 ;
@@ -219,7 +207,7 @@ namespace CppSockets {
219207 socklen_t len = sizeof (int );
220208
221209 if (addr_out != nullptr ) {
222- // TODO
210+ // TODO figure it out
223211 }
224212 if (fd == INVALID_SOCKET ) {
225213 return nullptr ;
@@ -236,9 +224,9 @@ namespace CppSockets {
236224
237225 if (ret >= 0 ) {
238226 if (val) {
239- ret = fcntl (sockfd, F_SETFL , flags | O_NONBLOCK ); // NOLINT
227+ ret = fcntl (sockfd, F_SETFL , flags | O_NONBLOCK ); // NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg, hicpp-signed-bitwise)
240228 } else {
241- ret = fcntl (sockfd, F_SETFL , (flags | O_NONBLOCK ) ^ O_NONBLOCK ); // NOLINT
229+ ret = fcntl (sockfd, F_SETFL , (flags | O_NONBLOCK ) ^ O_NONBLOCK ); // NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg, hicpp-signed-bitwise)
242230 }
243231 }
244232 if (ret < 0 ) {
0 commit comments