libsocket
unixserverstream.cpp
Go to the documentation of this file.
1 #include <string>
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 
39 #include <libunixsocket.h>
40 #include <exception.hpp>
41 #include <unixserverstream.hpp>
42 
43 namespace libsocket {
44 using std::string;
45 
50 
57 unix_stream_server::unix_stream_server(const char* path, int flags) {
58  setup(path, flags);
59 }
60 
67 unix_stream_server::unix_stream_server(const string& path, int flags) {
68  setup(path, flags);
69 }
70 
77 void unix_stream_server::setup(const char* path, int flags) {
78  if (sfd != -1)
79  throw socket_exception(
80  __FILE__, __LINE__,
81  "unix_stream_server::setup: Socket already set up!", false);
82  if (path == NULL)
83  throw socket_exception(__FILE__, __LINE__,
84  "unix_stream_server::setup: Path is NULL!",
85  false);
86 
87  sfd = create_unix_server_socket(path, LIBSOCKET_STREAM, flags);
88 
89  if (sfd < 0)
90  throw socket_exception(__FILE__, __LINE__,
91  "unix_stream_server::setup: Error at creating "
92  "UNIX stream server socket!");
93 
94  _path.assign(path);
95 }
96 
103 void unix_stream_server::setup(const string& path, int flags) {
104  setup(path.c_str(), flags);
105 }
106 
113  return accept2(flags).release();
114 }
115 
124 unique_ptr<unix_stream_client> unix_stream_server::accept2(int flags) {
125  int cfd;
126 
127  if (sfd == -1)
128  throw socket_exception(
129  __FILE__, __LINE__,
130  "unix_stream_server::accept2: Socket not has not yet been set up!", false);
131 
132  unique_ptr<unix_stream_client> client(new unix_stream_client);
133 
134  cfd = accept_unix_stream_socket(sfd, flags);
135 
136  if (cfd < 0) {
137  if (is_nonblocking && errno == EWOULDBLOCK)
138  return unique_ptr<unix_stream_client>(nullptr);
139  else
140  throw socket_exception(__FILE__, __LINE__,
141  "unix_stream_server::accept2: Error while "
142  "accepting new connection!");
143  }
144 
145  client->sfd = cfd;
146 
147  return client;
148 }
149 } // namespace libsocket
Contains libsocket elements.
Definition: dgramclient.hpp:41
int accept_unix_stream_socket(int sfd, int flags)
Accept connections on a passive UNIX socket.
Provides an interface for working with UNIX STREAM sockets.
This class is instantiated and thrown when an error occurs. If there's an error somewhere in libsocke...
Definition: exception.hpp:52
unix_stream_client * accept(int flags=0)
Accepts incoming connections on a UNIX domain stream server socket.
unique_ptr< unix_stream_client > accept2(int flags=0)
Accepts an incoming connection on a UNIX domain stream server socket and returns an owned pointer.
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_stream_server(void)
Void constructor.
void setup(const char *path, int flags=0)
Sets a server socket up.