Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
socket.h
Go to the documentation of this file.
1
13// --------------------------------------------------------------------------
14// This file is part of the "sockpp" C++ socket library.
15//
16// Copyright (c) 2014-2019 Frank Pagliughi
17// All rights reserved.
18//
19// Redistribution and use in source and binary forms, with or without
20// modification, are permitted provided that the following conditions are
21// met:
22//
23// 1. Redistributions of source code must retain the above copyright notice,
24// this list of conditions and the following disclaimer.
25//
26// 2. Redistributions in binary form must reproduce the above copyright
27// notice, this list of conditions and the following disclaimer in the
28// documentation and/or other materials provided with the distribution.
29//
30// 3. Neither the name of the copyright holder nor the names of its
31// contributors may be used to endorse or promote products derived from this
32// software without specific prior written permission.
33//
34// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
38// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45// --------------------------------------------------------------------------
46
47#ifndef __sockpp_socket_h
48#define __sockpp_socket_h
49
50#include "sockpp/sock_address.h"
51#include <chrono>
52#include <string>
53#include <tuple>
54
55namespace sockpp {
56
58
59#if !defined(SOCKPP_SOCKET_T_DEFINED)
60 typedef int socket_t;
62 #define SOCKPP_SOCKET_T_DEFINED
63#endif
64
65
66timeval to_timeval(const std::chrono::microseconds& dur);
67
68template<class Rep, class Period>
69timeval to_timeval(const std::chrono::duration<Rep,Period>& dur) {
70 return to_timeval(std::chrono::duration_cast<std::chrono::microseconds>(dur));
71}
72
74
84class socket
85{
87 socket_t handle_;
89 mutable int lastErr_;
95 bool close(socket_t h);
96
97 // Non-copyable.
98 socket(const socket&) =delete;
99 socket& operator=(const socket&) =delete;
100
101protected:
112 close(release());
113 return false;
114 }
121 static int get_last_error();
128 lastErr_ = get_last_error();
129 }
136 template <typename T>
137 T check_ret(T ret) const{
138 lastErr_ = (ret < 0) ? get_last_error() : 0;
139 return ret;
140 }
148 template <typename T>
149 bool check_ret_bool(T ret) const{
150 lastErr_ = (ret < 0) ? get_last_error() : 0;
151 return ret >= 0;
152 }
162 lastErr_ = (ret == INVALID_SOCKET) ? get_last_error() : 0;
163 return ret;
164 }
175 lastErr_ = (ret == INVALID_SOCKET) ? get_last_error() : 0;
176 return ret != INVALID_SOCKET;
177 }
178
179public:
183 socket() : handle_(INVALID_SOCKET), lastErr_(0) {}
190 explicit socket(socket_t h) : handle_(h), lastErr_(0) {}
196 socket(socket&& sock) noexcept
197 : handle_(sock.handle_), lastErr_(sock.lastErr_) {
198 sock.handle_ = INVALID_SOCKET;
199 }
203 virtual ~socket() { close(); }
209 static void initialize();
215 static void destroy();
233 static socket create(int domain, int type, int protocol=0);
238 bool is_open() const { return handle_ != INVALID_SOCKET; }
244 bool operator!() const {
245 return handle_ == INVALID_SOCKET || lastErr_ != 0;
246 }
252 explicit operator bool() const {
253 return handle_ != INVALID_SOCKET && lastErr_ == 0;
254 }
259 socket_t handle() const { return handle_; }
266 virtual sa_family_t family() const {
267 return address().family();
268 }
280 socket clone() const;
301 static std::tuple<socket, socket> pair(int domain, int type, int protocol=0);
306 void clear(int val=0) { lastErr_ = val; }
312 socket_t h = handle_;
313 handle_ = INVALID_SOCKET;
314 return h;
315 }
320 void reset(socket_t h=INVALID_SOCKET);
327 socket& operator=(socket&& sock) noexcept {
328 // Give our handle to the other to close.
329 std::swap(handle_, sock.handle_);
330 lastErr_ = sock.lastErr_;
331 return *this;
332 }
338 bool bind(const sock_address& addr);
364 bool get_option(int level, int optname, void* optval, socklen_t* optlen) const;
375 template <typename T>
376 bool get_option(int level, int optname, T* val) const {
377 socklen_t len = sizeof(T);
378 return get_option(level, optname, (void*) val, &len);
379 }
394 bool set_option(int level, int optname, const void* optval, socklen_t optlen);
406 template <typename T>
407 bool set_option(int level, int optname, const T& val) {
408 return set_option(level, optname, (void*) &val, sizeof(T));
409 }
418 bool set_non_blocking(bool on=true);
425 static std::string error_str(int errNum);
431 int last_error() const { return lastErr_; }
437 std::string last_error_str() const {
438 return error_str(lastErr_);
439 }
448 bool shutdown(int how=SHUT_RDWR);
455 bool close();
456};
457
459
476
478// end namespace sockpp
479}
480
481#endif // __sockpp_socket_h
482
Definition sock_address.h:109
Definition sock_address.h:65
virtual sa_family_t family() const
Definition sock_address.h:93
Definition socket.h:471
Definition socket.h:85
socket_t release()
Definition socket.h:311
virtual ~socket()
Definition socket.h:203
bool close_on_err()
Definition socket.h:111
bool operator!() const
Definition socket.h:244
static std::string error_str(int errNum)
Definition socket.cpp:287
socket(socket &&sock) noexcept
Definition socket.h:196
void reset(socket_t h=INVALID_SOCKET)
Definition socket.cpp:175
bool set_option(int level, int optname, const T &val)
Definition socket.h:407
bool check_socket_bool(socket_t ret) const
Definition socket.h:174
void clear(int val=0)
Definition socket.h:306
static void destroy()
Definition socket.cpp:110
socket clone() const
Definition socket.cpp:129
bool shutdown(int how=SHUT_RDWR)
Definition socket.cpp:295
T check_ret(T ret) const
Definition socket.h:137
int last_error() const
Definition socket.h:431
bool is_open() const
Definition socket.h:238
static std::tuple< socket, socket > pair(int domain, int type, int protocol=0)
Definition socket.cpp:146
bool bind(const sock_address &addr)
Definition socket.cpp:187
socket & operator=(socket &&sock) noexcept
Definition socket.h:327
bool get_option(int level, int optname, T *val) const
Definition socket.h:376
socket_t handle() const
Definition socket.h:259
socket()
Definition socket.h:183
static socket create(int domain, int type, int protocol=0)
Definition socket.cpp:119
bool set_non_blocking(bool on=true)
Definition socket.cpp:256
bool check_ret_bool(T ret) const
Definition socket.h:149
std::string last_error_str() const
Definition socket.h:437
virtual sa_family_t family() const
Definition socket.h:266
static void initialize()
Definition socket.cpp:97
sock_address_any address() const
Definition socket.cpp:195
socket(socket_t h)
Definition socket.h:190
sock_address_any peer_address() const
Definition socket.cpp:210
bool set_option(int level, int optname, const void *optval, socklen_t optlen)
Definition socket.cpp:243
static int get_last_error()
Definition socket.cpp:71
socket_t check_socket(socket_t ret) const
Definition socket.h:161
bool close()
Definition socket.cpp:303
void set_last_error()
Definition socket.h:127
bool get_option(int level, int optname, void *optval, socklen_t *optlen) const
Definition socket.cpp:224
int socket_t
The OS socket handle.
Definition socket.h:60
const socket_t INVALID_SOCKET
Invalid socket descriptor.
Definition socket.h:61