sockpp
Modern C++ socket library wrapper
connector.h
Go to the documentation of this file.
1 
13 // --------------------------------------------------------------------------
14 // This file is part of the "sockpp" C++ socket library.
15 //
16 // Copyright (c) 2014-2019 Frank Pagliughi
17 // All rights reserved.
18 //
19 // Redistribution and use in source and binary forms, with or without
20 // modification, are permitted provided that the following conditions are
21 // met:
22 //
23 // 1. Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
25 //
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 //
30 // 3. Neither the name of the copyright holder nor the names of its
31 // contributors may be used to endorse or promote products derived from this
32 // software without specific prior written permission.
33 //
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
35 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
36 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
37 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
38 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
40 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
41 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 // --------------------------------------------------------------------------
46 
47 #ifndef __sockpp_connector_h
48 #define __sockpp_connector_h
49 
50 #include "sockpp/stream_socket.h"
51 #include "sockpp/sock_address.h"
52 
53 namespace sockpp {
54 
56 
63 class connector : public stream_socket
64 {
66  using base = stream_socket;
67 
68  // Non-copyable
69  connector(const connector&) =delete;
70  connector& operator=(const connector&) =delete;
71 
72 public:
76  connector() {}
82  connector(const sock_address& addr) { connect(addr); }
88  connector(connector&& conn) : base(std::move(conn)) {}
95  base::operator=(std::move(rhs));
96  return *this;
97  }
106  bool is_connected() const { return is_open(); }
114  bool connect(const sock_address& addr);
115 };
116 
118 
122 template <typename STREAM_SOCK, typename ADDR=typename STREAM_SOCK::addr_t>
123 class connector_tmpl : public connector
124 {
126  using base = connector;
127 
128  // Non-copyable
129  connector_tmpl(const connector_tmpl&) =delete;
130  connector_tmpl& operator=(const connector_tmpl&) =delete;
131 
132 public:
134  using stream_sock_t = STREAM_SOCK;
136  using addr_t = ADDR;
137 
147  connector_tmpl(const addr_t& addr) : base(addr) {}
154  base::operator=(std::move(rhs));
155  return *this;
156  }
162  addr_t address() const { return addr_t(base::address()); }
176  bool bind(const addr_t& addr) { return base::bind(addr); }
184  bool connect(const addr_t& addr) { return base::connect(addr); }
185 };
186 
188 // end namespace sockpp
189 }
190 
191 #endif // __sockpp_connector_h
192 
Generic socket address.
Definition: sock_address.h:64
STREAM_SOCK stream_sock_t
The type of streaming socket from the acceptor.
Definition: connector.h:134
stream_socket & operator=(stream_socket &&rhs)
Move assignment.
Definition: stream_socket.h:122
connector_tmpl()
Creates an unconnected connector.
Definition: connector.h:141
sock_address_any address() const
Gets the local address to which the socket is bound.
Class to create a client stream connection.
Definition: connector.h:63
Class to create a client TCP connection.
Definition: connector.h:123
Base class for streaming sockets, such as TCP and Unix Domain.
Definition: stream_socket.h:62
bool connect(const addr_t &addr)
Attempts to connects to the specified server.
Definition: connector.h:184
addr_t peer_address() const
Gets the address of the remote peer, if this socket is connected.
Definition: connector.h:168
connector()
Creates an unconnected connector.
Definition: connector.h:76
bool connect(const sock_address &addr)
Attempts to connect to the specified server.
connector_tmpl(const addr_t &addr)
Creates the connector and attempts to connect to the specified address.
Definition: connector.h:147
stream_socket()
Creates an unconnected streaming socket.
Definition: stream_socket.h:85
bool bind(const sock_address &addr)
Binds the socket to the specified address.
connector(connector &&conn)
Move constructor.
Definition: connector.h:88
connector & operator=(connector &&rhs)
Move assignment.
Definition: connector.h:94
bool is_open() const
Determines if the socket is open (valid).
Definition: socket.h:238
connector_tmpl & operator=(connector_tmpl &&rhs)
Move assignment.
Definition: connector.h:153
Definition: acceptor.h:51
bool is_connected() const
Determines if the socket connected to a remote host.
Definition: connector.h:106
connector(const sock_address &addr)
Creates the connector and attempts to connect to the specified address.
Definition: connector.h:82
sock_address_any peer_address() const
Gets the address of the remote peer, if this socket is connected.
addr_t address() const
Gets the local address to which the socket is bound.
Definition: connector.h:162
bool bind(const addr_t &addr)
Binds the socket to the specified address.
Definition: connector.h:176
Generic address class for sockpp.
ADDR addr_t
The type of address for the connector.
Definition: connector.h:136
Classes for stream sockets.