libsocket
unixclientdgram.cpp
Go to the documentation of this file.
1 #include <string>
2 
3 using std::string;
4 
5 /*
6  The committers of the libsocket project, all rights reserved
7  (c) 2012, dermesser <lbo@spheniscida.de>
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11 
12  1. Redistributions of source code must retain the above copyright notice,
13  this list of conditions and the following disclaimer.
14  2. Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17 
18  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
19  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 */
30 
42 #include <libunixsocket.h>
43 #include <exception.hpp>
44 #include <unixclientdgram.hpp>
45 
46 #include <fcntl.h>
47 #ifndef SOCK_NONBLOCK
48 #define SOCK_NONBLOCK O_NONBLOCK
49 #endif
50 
51 namespace libsocket {
58 void unix_dgram_client::setup(const char* path, int flags) {
59  if (sfd != -1)
60  throw socket_exception(__FILE__, __LINE__,
61  "unix_dgram_client::unix_dgram_client: Socket "
62  "has already been set up!",
63  false);
64 
65  sfd = create_unix_dgram_socket(path, flags);
66 
67  if (sfd < 0)
68  throw socket_exception(__FILE__, __LINE__,
69  "unix_dgram_client::unix_dgram_client: Could "
70  "not create unix dgram client socket!");
71 
72  if (path) _path.assign(path);
73 
74  is_nonblocking = flags & SOCK_NONBLOCK;
75 }
76 
83  setup(NULL, flags); // bind to nowhere
84 }
85 
94 unix_dgram_client::unix_dgram_client(const char* path, int flags) {
95  setup(path, flags); // bind to path
96 }
97 
106 unix_dgram_client::unix_dgram_client(const string& path, int flags) {
107  setup(path.c_str(), flags);
108 }
109 
122 void unix_dgram_client::connect(const char* path) {
123  if (sfd == -1)
124  throw socket_exception(
125  __FILE__, __LINE__,
126  "unix_dgram_client::connect() - Socket has already been closed!",
127  false);
128  if (connect_unix_dgram_socket(sfd, path) < 0)
129  throw socket_exception(
130  __FILE__, __LINE__,
131  "unix_dgram_client::connect: Could not connect dgram socket!");
132 
133  _path.assign(path);
134 
135  connected = true;
136 }
137 
150 void unix_dgram_client::connect(const string& path) { connect(path.c_str()); }
151 
159  if (connect_unix_dgram_socket(sfd, 0) < 0)
160  throw socket_exception(
161  __FILE__, __LINE__,
162  "unix_dgram_client::deconnect: Could not disconnect dgram socket!");
163 
164  _path.clear();
165 
166  connected = false;
167 }
168 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
void setup(const char *path, int flags=0)
Set a UNIX domain datagram socket up.
int create_unix_dgram_socket(const char *bind_path, int flags)
Create a UNIX DGRAM socket.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
int connect_unix_dgram_socket(int sfd, const char *path)
Connect a datagram socket.
void connect(const char *path)
Connect a UNIX datagram socket.
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
void deconnect(void)
Disconnect a UNIX datagram socket.
unix_dgram_client(int flags=0)
Constructor with only socket() flags.