sockpp
Modern C++ socket library wrapper
acceptor.h
Go to the documentation of this file.
1 
11 // --------------------------------------------------------------------------
12 // This file is part of the "sockpp" C++ socket library.
13 //
14 // Copyright (c) 2014-2019 Frank Pagliughi
15 // All rights reserved.
16 //
17 // Redistribution and use in source and binary forms, with or without
18 // modification, are permitted provided that the following conditions are
19 // met:
20 //
21 // 1. Redistributions of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
23 //
24 // 2. Redistributions in binary form must reproduce the above copyright
25 // notice, this list of conditions and the following disclaimer in the
26 // documentation and/or other materials provided with the distribution.
27 //
28 // 3. Neither the name of the copyright holder nor the names of its
29 // contributors may be used to endorse or promote products derived from this
30 // software without specific prior written permission.
31 //
32 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
36 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 // --------------------------------------------------------------------------
44 
45 #ifndef __sockpp_acceptor_h
46 #define __sockpp_acceptor_h
47 
48 #include "sockpp/inet_address.h"
49 #include "sockpp/stream_socket.h"
50 
51 namespace sockpp {
52 
54 
63 class acceptor : public socket
64 {
66  using base = socket;
67 
68  // Non-copyable
69  acceptor(const acceptor&) =delete;
70  acceptor& operator=(const acceptor&) =delete;
71 
72 protected:
74  static const int DFLT_QUE_SIZE = 4;
75 
84  static socket_t create_handle(int domain) {
85  return stream_socket::create_handle(domain);
86  }
87 
88 public:
92  acceptor() {}
98  explicit acceptor(socket_t handle) : base(handle) {}
105  acceptor(const sock_address& addr, int queSize=DFLT_QUE_SIZE) {
106  open(addr, queSize);
107  }
113  acceptor(acceptor&& acc) : base(std::move(acc)) {}
121  static acceptor create(int domain);
128  base::operator=(std::move(rhs));
129  return *this;
130  }
136  bool listen(int queSize=DFLT_QUE_SIZE) {
137  return check_ret_bool(::listen(handle(), queSize));
138  };
149  bool open(const sock_address& addr, int queSize=DFLT_QUE_SIZE, bool reuseSock=true);
156  stream_socket accept(sock_address* clientAddr=nullptr);
157 };
158 
159 
161 
170 template <typename STREAM_SOCK, typename ADDR=typename STREAM_SOCK::addr_t>
171 class acceptor_tmpl : public acceptor
172 {
174  using base = acceptor;
175 
176  // Non-copyable
177  acceptor_tmpl(const acceptor_tmpl&) =delete;
178  acceptor_tmpl& operator=(const acceptor_tmpl&) =delete;
179 
180 public:
182  using stream_sock_t = STREAM_SOCK;
184  using addr_t = ADDR;
185 
195  acceptor_tmpl(const addr_t& addr, int queSize=DFLT_QUE_SIZE) {
196  open(addr, queSize);
197  }
205  acceptor_tmpl(in_port_t port, int queSize=DFLT_QUE_SIZE) {
206  open(port, queSize);
207  }
213  acceptor_tmpl(acceptor_tmpl&& acc) : base(std::move(acc)) {}
221  return base::create(addr_t::ADDRESS_FAMILY);
222  }
229  base::operator=(std::move(rhs));
230  return *this;
231  }
236  addr_t address() const { return addr_t(base::address()); }
242  bool bind(const addr_t& addr) { return base::bind(addr); }
250  bool open(const addr_t& addr, int queSize=DFLT_QUE_SIZE) {
251  return base::open(addr, queSize);
252  }
260  bool open(in_port_t port, int queSize=DFLT_QUE_SIZE) {
261  return open(addr_t(port), queSize);
262  }
269  stream_sock_t accept(addr_t* clientAddr=nullptr) {
270  return stream_sock_t(base::accept(clientAddr));
271  }
272 };
273 
275 // end namespace sockpp
276 }
277 
278 #endif // __sockpp_acceptor_h
279 
acceptor_tmpl(acceptor_tmpl &&acc)
Move constructor.
Definition: acceptor.h:213
static acceptor create(int domain)
Creates an unbound acceptor socket with an open OS socket handle.
Generic socket address.
Definition: sock_address.h:64
sock_address_any address() const
Gets the local address to which the socket is bound.
stream_socket accept(sock_address *clientAddr=nullptr)
Accepts an incoming TCP connection and gets the address of the client.
static socket_t create_handle(int domain)
Creates an underlying acceptor socket.
Definition: acceptor.h:84
static const int DFLT_QUE_SIZE
The default listener queue size.
Definition: acceptor.h:74
Base class for streaming sockets, such as TCP and Unix Domain.
Definition: stream_socket.h:62
acceptor & operator=(acceptor &&rhs)
Move assignment.
Definition: acceptor.h:127
addr_t address() const
Gets the local address to which we are bound.
Definition: acceptor.h:236
static socket_t create_handle(int domain)
Creates a streaming socket.
Definition: stream_socket.h:75
bool open(in_port_t port, int queSize=DFLT_QUE_SIZE)
Opens the acceptor socket, binds the socket to all adapters and starts it listening.
Definition: acceptor.h:260
bool bind(const addr_t &addr)
Binds the socket to the specified address.
Definition: acceptor.h:242
bool bind(const sock_address &addr)
Binds the socket to the specified address.
acceptor(acceptor &&acc)
Move constructor.
Definition: acceptor.h:113
bool check_ret_bool(T ret) const
Checks the value and if less than zero, sets last error.
Definition: socket.h:149
ADDR addr_t
The type of address for the acceptor and streams.
Definition: acceptor.h:184
bool open(const addr_t &addr, int queSize=DFLT_QUE_SIZE)
Opens the acceptor socket, binds it to the specified address, and starts listening.
Definition: acceptor.h:250
acceptor(socket_t handle)
Creates an acceptor from an existing OS socket handle and claims ownership of the handle...
Definition: acceptor.h:98
acceptor(const sock_address &addr, int queSize=DFLT_QUE_SIZE)
Creates an acceptor socket and starts it listening to the specified address.
Definition: acceptor.h:105
acceptor_tmpl & operator=(acceptor_tmpl &&rhs)
Move assignment.
Definition: acceptor.h:228
Class for creating a streaming server.
Definition: acceptor.h:63
socket()
Creates an unconnected (invalid) socket.
Definition: socket.h:183
bool open(const sock_address &addr, int queSize=DFLT_QUE_SIZE, bool reuseSock=true)
Opens the acceptor socket, binds it to the specified address, and starts listening.
acceptor()
Creates an unconnected acceptor.
Definition: acceptor.h:92
Definition: acceptor.h:51
Base template class for streaming servers of specific address families.
Definition: acceptor.h:171
static socket create(int domain, int type, int protocol=0)
Creates a socket with the specified communications characterics.
acceptor_tmpl(in_port_t port, int queSize=DFLT_QUE_SIZE)
Creates a acceptor and starts it listening on the specified port.
Definition: acceptor.h:205
stream_sock_t accept(addr_t *clientAddr=nullptr)
Accepts an incoming connection and gets the address of the client.
Definition: acceptor.h:269
Base class for socket objects.
Definition: socket.h:84
int socket_t
The OS socket handle.
Definition: socket.h:60
Class for a TCP/IP socket address.
acceptor_tmpl()
Creates an unconnected acceptor.
Definition: acceptor.h:189
acceptor_tmpl(const addr_t &addr, int queSize=DFLT_QUE_SIZE)
Creates a acceptor and starts it listening on the specified address.
Definition: acceptor.h:195
bool listen(int queSize=DFLT_QUE_SIZE)
Sets the socket listening on the address to which it is bound.
Definition: acceptor.h:136
socket_t handle() const
Get the underlying OS socket handle.
Definition: socket.h:259
STREAM_SOCK stream_sock_t
The type of streaming socket from the acceptor.
Definition: acceptor.h:182
Classes for stream sockets.
static acceptor_tmpl create()
Creates an unbound acceptor socket with an open OS socket handle.
Definition: acceptor.h:220