libsocket
socket.hpp
Go to the documentation of this file.
1 #ifndef LIBSOCKET_SOCKET_H_CCC0882809A540AAAD842D9AE3DB47BB
2 #define LIBSOCKET_SOCKET_H_CCC0882809A540AAAD842D9AE3DB47BB
3 
4 #include <sys/socket.h>
5 #include <sys/types.h>
6 
7 #include <errno.h>
8 
9 #include "exception.hpp"
10 
16 /*
17  The committers of the libsocket project, all rights reserved
18  (c) 2012, dermesser <lbo@spheniscida.de>
19 
20  Redistribution and use in source and binary forms, with or without
21  modification, are permitted provided that the following conditions are met:
22 
23  1. Redistributions of source code must retain the above copyright notice,
24  this list of conditions and the following disclaimer.
25  2. Redistributions in binary form must reproduce the above copyright notice,
26  this list of conditions and the following disclaimer in the documentation
27  and/or other materials provided with the distribution.
28 
29  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
30  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
33  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 */
41 
42 #define LIBSOCKET_TCP 1
43 #define LIBSOCKET_UDP 2
44 
45 #define LIBSOCKET_IPv4 3
46 #define LIBSOCKET_IPv6 4
47 
48 #define LIBSOCKET_BOTH 5 // what fits best (TCP/UDP or IPv4/6)
49 
50 #define LIBSOCKET_READ 1
51 #define LIBSOCKET_WRITE 2
52 
53 #define LIBSOCKET_NUMERIC 1
54 
60 namespace libsocket {
71 class socket {
72  protected:
74  int sfd;
75  bool is_nonblocking;
79 
80  public:
81  socket(void);
82  socket(const socket&) = delete;
83  socket(socket&&);
84 
85  virtual ~socket();
86 
87  virtual int destroy(void);
88 
89  int getfd(void) const;
90 
91  int set_sock_opt(int level, int optname, const char* optval,
92  socklen_t optlen) const;
95  void set_close_on_destructor(bool cod) { close_on_destructor = cod; }
96 };
100 } // namespace libsocket
101 #endif
Contains libsocket elements.
Definition: dgramclient.hpp:41
int set_sock_opt(int level, int optname, const char *optval, socklen_t optlen) const
Set socket options on the underlying socket.
Definition: socket.cpp:101
int getfd(void) const
Returns the socket file descriptor.
Definition: socket.cpp:91
socket is the base class of every other libsocket++ object.
Definition: socket.hpp:71
socket(void)
Constructor. Sets sfd to -1.
Definition: socket.cpp:45
bool close_on_destructor
Definition: socket.hpp:78
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
void set_close_on_destructor(bool cod)
Definition: socket.hpp:95
virtual ~socket()
Destructor: closes socket.
Definition: socket.cpp:59
virtual int destroy(void)
Destroys a socket.
Definition: socket.cpp:72