NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
tcp_ip4.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Evidence Srl - www.evidence.eu.com
3  *
4  * Boost Software License - Version 1.0 - August 17th, 2003
5  *
6  * Permission is hereby granted, free of charge, to any person or organization
7  * obtaining a copy of the software and accompanying documentation covered by
8  * this license (the "Software") to use, reproduce, display, distribute,
9  * execute, and transmit the Software, and to prepare derivative works of the
10  * Software, and to permit third-parties to whom the Software is furnished to
11  * do so, all subject to the following:
12  *
13  * The copyright notices in the Software and this entire statement, including
14  * the above license grant, this restriction and the following disclaimer,
15  * must be included in all copies of the Software, in whole or in part, and
16  * all derivative works of the Software, unless such copies or derivative
17  * works are solely in the form of machine-executable object code generated by
18  * a source language processor.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
23  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
24  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
25  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28 
29 #ifndef TCP_IPV4_HPP_
30 #define TCP_IPV4_HPP_
31 
32 #include <stdexcept>
33 
34 #include "abstract_socket.hpp"
35 #include "posix_socket.hpp"
36 #include "address.hpp"
37 
38 namespace net {
39 namespace ip4 {
40 namespace tcp {
41 
42 /// Address for IPv4 TCP communications.
43 class address: public Address {
44 public:
45  /**
46  * @brief Constructor
47  *
48  * @param addr std::string containing the address (e.g., "127.0.0.1")
49  * @param port Integer containing the port number (e.g., 1234)
50  */
51  address(const std::string& addr, int port):
52  address_{addr}, port_{port}{};
53 
54  /**
55  * @brief Get the address
56  *
57  * @return a std::string containing the IP address
58  * (not the port number)
59  */
60  inline std::string getAddress() const {
61  return address_;
62  }
63 
64  /**
65  * @brief Get the port number
66  *
67  * @return an integer containing the port number
68  */
69  inline int getPort() const {
70  return port_;
71  }
72 
73 private:
74  /// IP address
75  std::string address_;
76 
77  /// Port number
78  int port_;
79 };
80 
81 
82 /**
83  * @brief Server for IPv4 TCP communications.
84  */
85 class server: public AbstractSocket {
86 public:
87  /**
88  * @brief Constructor
89  *
90  * This constructor allocates a concrete class derived from net::AbstractSystemSocket.
91  * @param max_pending_connections Number of maximum allowed pending connections
92  */
93  server(int max_pending_connections = 100):
95  max_pending_connections_{max_pending_connections}{}
96 
97  /**
98  * @brief Constructor to accept() a TCP connection on an existing socket.
99  *
100  * This constructor allocates a concrete class derived from net::AbstractSystemSocket
101  * and accept a TCP connection on the given (existing) TCP socket.
102  * @param srv Existing TCP socket on which the new connection must be accepted.
103  * @param max_pending_connections Number of maximum allowed pending connections
104  */
105  server(AbstractSocket* srv, int max_pending_connections = 100):
107  max_pending_connections_{max_pending_connections}
108  {
109  socket_->accept((srv->getSocket()));
110  }
111 
112  /**
113  * @brief Method to bind the server to an address
114  *
115  * This method invokes the platform-specific bind() operation.
116  * @param addr Address (i.e., net::ip4::tcp::address)
117  * which the server must be bound to
118  */
119  inline void bind (const Address& addr){
120  AbstractSocket::socket_->bind(addr);
121  }
122 
123  /**
124  * @brief Listen operation
125  *
126  * This method sets the number of maximum allowed pending connections.
127  * This method invokes the platform-specific listen() operation.
128  */
129  inline void listen(){
130  AbstractSocket::socket_->listen(max_pending_connections_);
131  }
132 
133  /**
134  * @brief Method to open the server towards a certain address
135  *
136  * This method calls net::ip4::tcp::server::bind() and
137  * net::ip4::tcp::server::listen(). They, in turn, call
138  * the platform-specific operations.
139  * @param addr Address (i.e., net::ip4::tcp::address)
140  * that must be open by the server
141  */
142  inline void open (const Address& addr) {
143  bind (addr);
144  listen();
145  }
146 
147 private:
148  /// Number of maximum allowed pending connections
149  int max_pending_connections_;
150 };
151 
152 
153 
154 /**
155  * @brief Client for IPv4 TCP communications.
156  */
157 class client: public AbstractSocket {
158 public:
159  /**
160  * @brief Constructor
161  *
162  * This constructor allocates a concrete class
163  * derived from net::AbstractSystemSocket.
164  */
167 
168  /**
169  * @brief Method to connect the client to an address
170  *
171  * This method invokes the platform-specific connect() operation.
172  * @param addr Address (i.e., net::ip4::tcp::address)
173  * which the client must be connected to
174  */
175  inline void connect (const Address& addr){
176  AbstractSocket::socket_->connect(addr);
177  }
178 
179  /**
180  * @brief Method to open the client towards a certain address
181  *
182  * This method calls net::ip4::tcp::client::connect(),
183  * which in turn invokes the platform-specific connect() operation.
184  * @param addr Address (i.e., net::ip4::tcp::address)
185  * that must be open by the client
186  */
187  inline void open (const Address& addr) {
188  connect(addr);
189  }
190 
191 };
192 
193 }}} // net::ip4::tcp
194 
195 #endif // TCP_IPV4_HPP_