libsocket
socket.cpp
Go to the documentation of this file.
1 #include <unistd.h>
2 /*
3  The committers of the libsocket project, all rights reserved
4  (c) 2012, dermesser <lbo@spheniscida.de>
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8 
9  1. Redistributions of source code must retain the above copyright notice,
10  this list of conditions and the following disclaimer.
11  2. Redistributions in binary form must reproduce the above copyright notice,
12  this list of conditions and the following disclaimer in the documentation
13  and/or other materials provided with the distribution.
14 
15  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
16  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
19  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 */
27 
38 #include <socket.hpp>
39 
40 namespace libsocket {
46  : sfd(-1), is_nonblocking(false), close_on_destructor(true) {}
47 
52  : sfd(other.sfd), is_nonblocking(false), close_on_destructor(true) {
53  other.sfd = -1;
54 }
55 
60  /*
61  * This is possible because socket::~socket(const socket&) is deleted.
62  */
64 }
65 
72 int socket::destroy(void) {
73  if (0 > sfd) return 0;
74 
75  if (0 > close(sfd)) return -1;
76 
77  sfd = -1;
78 
79  return 0;
80 }
81 
91 int socket::getfd(void) const { return sfd; }
92 
101 int socket::set_sock_opt(int level, int optname, const char* optval,
102  socklen_t optlen) const {
103  return setsockopt(sfd, level, optname, optval, optlen);
104 }
105 } // namespace libsocket
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
virtual ~socket()
Destructor: closes socket.
Definition: socket.cpp:59
virtual int destroy(void)
Destroys a socket.
Definition: socket.cpp:72