libsocket
unixclientstream.cpp
Go to the documentation of this file.
1 #include <string.h>
2 #include <string>
3 
4 #include <sys/socket.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7 
8 /*
9  The committers of the libsocket project, all rights reserved
10  (c) 2012, dermesser <lbo@spheniscida.de>
11 
12  Redistribution and use in source and binary forms, with or without
13  modification, are permitted provided that the following conditions are met:
14 
15  1. Redistributions of source code must retain the above copyright notice,
16  this list of conditions and the following disclaimer.
17  2. Redistributions in binary form must reproduce the above copyright notice,
18  this list of conditions and the following disclaimer in the documentation
19  and/or other materials provided with the distribution.
20 
21  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS “AS IS” AND ANY
22  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 */
33 
45 #include <libunixsocket.h>
46 #include <exception.hpp>
47 #include <unixclientstream.hpp>
48 
49 namespace libsocket {
50 using std::string;
51 
56 
65 unix_stream_client::unix_stream_client(const char* path, int socket_flags) {
66  connect(path, socket_flags);
67 }
68 
77 unix_stream_client::unix_stream_client(const string& path, int socket_flags) {
78  connect(path.c_str(), socket_flags);
79 }
80 
89 void unix_stream_client::connect(const char* path, int socket_flags) {
90  if (sfd != -1)
91  throw socket_exception(
92  __FILE__, __LINE__,
93  "unix_stream_client::connect: Already connected!", false);
94 
95  sfd = create_unix_stream_socket(path, socket_flags);
96 
97  _path.assign(path);
98 
99  if (sfd < 0)
100  throw socket_exception(__FILE__, __LINE__,
101  "unix_stream_client::unix_stream_client: Could "
102  "not create and connect UNIX socket!");
103 
104  // New file descriptor, therefore reset shutdown flags
105  shut_rd = false;
106  shut_wr = false;
107 }
108 
117 void unix_stream_client::connect(const string& path, int socket_flags) {
118  connect(path.c_str(), socket_flags);
119 }
120 } // 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
int create_unix_stream_socket(const char *path, int flags)
Create and connect a new UNIX STREAM socket.
unix_stream_client(void)
Constructor.
int sfd
sfd is the sockets API file descriptor
Definition: socket.hpp:74
void connect(const char *path, int socket_flags=0)
Connect socket.