Wizard
Software Engineering Project - Wizard
Loading...
Searching...
No Matches
acceptor.h
Go to the documentation of this file.
1
10
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"
50
51namespace sockpp {
52
54
63class acceptor : public socket
64{
66 using base = socket;
67
68 // Non-copyable
69 acceptor(const acceptor&) =delete;
70 acceptor& operator=(const acceptor&) =delete;
71
72protected:
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
88public:
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
170template <typename STREAM_SOCK, typename ADDR=typename STREAM_SOCK::addr_t>
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
180public:
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
Definition acceptor.h:172
acceptor_tmpl(acceptor_tmpl &&acc)
Definition acceptor.h:213
acceptor_tmpl(const addr_t &addr, int queSize=DFLT_QUE_SIZE)
Definition acceptor.h:195
ADDR addr_t
Definition acceptor.h:184
stream_sock_t accept(addr_t *clientAddr=nullptr)
Definition acceptor.h:269
static acceptor_tmpl create()
Definition acceptor.h:220
addr_t address() const
Definition acceptor.h:236
bool bind(const addr_t &addr)
Definition acceptor.h:242
STREAM_SOCK stream_sock_t
Definition acceptor.h:182
acceptor_tmpl & operator=(acceptor_tmpl &&rhs)
Definition acceptor.h:228
acceptor_tmpl(in_port_t port, int queSize=DFLT_QUE_SIZE)
Definition acceptor.h:205
bool open(in_port_t port, int queSize=DFLT_QUE_SIZE)
Definition acceptor.h:260
acceptor_tmpl()
Definition acceptor.h:189
bool open(const addr_t &addr, int queSize=DFLT_QUE_SIZE)
Definition acceptor.h:250
Definition acceptor.h:64
static socket_t create_handle(int domain)
Definition acceptor.h:84
stream_socket accept(sock_address *clientAddr=nullptr)
Definition acceptor.cpp:98
bool open(const sock_address &addr, int queSize=DFLT_QUE_SIZE, bool reuseSock=true)
Definition acceptor.cpp:62
acceptor(acceptor &&acc)
Definition acceptor.h:113
bool listen(int queSize=DFLT_QUE_SIZE)
Definition acceptor.h:136
acceptor & operator=(acceptor &&rhs)
Definition acceptor.h:127
acceptor(socket_t handle)
Definition acceptor.h:98
acceptor()
Definition acceptor.h:92
acceptor(const sock_address &addr, int queSize=DFLT_QUE_SIZE)
Definition acceptor.h:105
static const int DFLT_QUE_SIZE
Definition acceptor.h:74
static acceptor create(int domain)
Definition acceptor.cpp:46
Definition sock_address.h:65
Definition socket.h:85
bool bind(const sock_address &addr)
Definition socket.cpp:187
socket_t handle() const
Definition socket.h:259
socket()
Definition socket.h:183
bool check_ret_bool(T ret) const
Definition socket.h:149
sock_address_any address() const
Definition socket.cpp:195
Definition stream_socket.h:63
static socket_t create_handle(int domain)
Definition stream_socket.h:75
int socket_t
The OS socket handle.
Definition socket.h:60