libsocket
unixserverdgram.cpp
Go to the documentation of this file.
1 
2 #include <sys/socket.h>
3 #include <sys/types.h>
4 
5 #include <string>
6 /*
7  The committers of the libsocket project, all rights reserved
8  (c) 2012, dermesser <lbo@spheniscida.de>
9 
10  Redistribution and use in source and binary forms, with or without
11  modification, are permitted provided that the following conditions are met:
12 
13  1. Redistributions of source code must retain the above copyright notice,
14  this list of conditions and the following disclaimer.
15  2. Redistributions in binary form must reproduce the above copyright notice,
16  this list of conditions and the following disclaimer in the documentation
17  and/or other materials provided with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
20  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
23  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 */
31 
40 #include <libunixsocket.h>
41 #include <exception.hpp>
42 #include <unixserverdgram.hpp>
43 
44 #include <fcntl.h>
45 #ifndef SOCK_NONBLOCK
46 #define SOCK_NONBLOCK O_NONBLOCK
47 #endif
48 
49 namespace libsocket {
50 using std::string;
51 
55 unix_dgram_server::unix_dgram_server(void) : bound(false) {}
56 
65 unix_dgram_server::unix_dgram_server(const char* bindpath, int socket_flags) {
66  setup(bindpath, socket_flags);
67 }
68 
77 unix_dgram_server::unix_dgram_server(const string& bindpath, int socket_flags) {
78  setup(bindpath.c_str(), socket_flags);
79 }
80 
89 void unix_dgram_server::setup(const char* bindpath, int socket_flags) {
90  if (sfd != -1)
91  throw socket_exception(__FILE__, __LINE__,
92  "unix_dgram_server::setup: Already set up!",
93  false);
94 
95  sfd = create_unix_server_socket(bindpath, LIBSOCKET_DGRAM, socket_flags);
96 
97  if (sfd < 0)
98  throw socket_exception(
99  __FILE__, __LINE__,
100  "unix_dgram_server::setup: Could not create server!");
101 
102  _path.assign(bindpath);
103  bound = true;
104  is_nonblocking = socket_flags & SOCK_NONBLOCK;
105 }
106 
115 void unix_dgram_server::setup(const string& bindpath, int socket_flags) {
116  setup(bindpath.c_str(), socket_flags);
117 }
118 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
bool bound
Shows if the socket is already bound (for setup routines)
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
int create_unix_server_socket(const char *path, int socktype, int flags)
Create a passive UNIX socket.
unix_dgram_server(void)
Void constructor.
void setup(const char *bindpath, int socket_flags=0)
Binds a socket.