NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
stream_local.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 STREAM_LOCAL_HPP_
30 #define STREAM_LOCAL_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 local {
40 namespace stream {
41 
42 /// Address for local stream communications.
43 class address: public Address {
44 public:
45  /**
46  * @brief Constructor
47  *
48  * @param addr std::string containing the path of the local socket
49  */
50  address(const std::string& addr):
51  address_{addr}{};
52 
53  /**
54  * @brief Get the path of the local socket
55  *
56  * @return a std::string containing the path of the local socket
57  */
58  inline std::string getAddress() const {
59  return address_;
60  }
61 private:
62  /// Path of the local socket:
63  std::string address_;
64 };
65 
66 /**
67  * @brief Server for local stream communications.
68  */
69 class server: public AbstractSocket {
70 public:
71  /**
72  * @brief Constructor
73  *
74  * This constructor allocates a concrete class derived from net::AbstractSystemSocket.
75  * @param max_pending_connections Number of maximum allowed pending connections
76  */
77  server(int max_pending_connections = 100):
79  max_pending_connections_{max_pending_connections}{}
80 
81  /**
82  * @brief Constructor to accept() a local stream connection on an existing socket.
83  *
84  * This constructor allocates a concrete class derived from net::AbstractSystemSocket
85  * and accept a local stream connection on the given (existing) local stream socket.
86  * @param srv Existing local stream socket on which the new connection must be accepted.
87  * @param max_pending_connections Number of maximum allowed pending connections
88  */
89  server(AbstractSocket* srv, int max_pending_connections = 100):
91  max_pending_connections_{max_pending_connections}
92  {
93  socket_->accept((srv->getSocket()));
94  }
95 
96  /**
97  * @brief Method to bind the server to a local socket
98  *
99  * This method invokes the platform-specific bind() operation.
100  * @param addr Address (i.e., net::local::stream::address)
101  * containing the local path of the socket which the server must be bound to
102  */
103  inline void bind (const Address& addr)
104  {
105  AbstractSocket::socket_->bind(addr);
106  }
107 
108  /**
109  * @brief Listen operation
110  *
111  * This method invokes the platform-specific listen() operation.
112  * This method sets the number of maximum allowed pending connections.
113  */
114  inline void listen()
115  {
116  AbstractSocket::socket_->listen(max_pending_connections_);
117  }
118 
119  /**
120  * @brief Method to open the server on a certain socket
121  *
122  * This method calls net::local::stream::server::bind() and
123  * net::local::stream::server::listen(). They, in turn, call
124  * the platform-specific operations.
125  * @param addr Address (i.e., net::local::stream::address)
126  * containing the local path of the socket that must be open
127  */
128  inline void open (const Address& addr)
129  {
130  bind (addr);
131  listen();
132  }
133 
134 private:
135 
136  /// Number of maximum allowed pending connections
137  int max_pending_connections_;
138 };
139 
140 
141 
142 /**
143  * @brief Client for local stream communications.
144  */
145 class client: public AbstractSocket {
146 public:
147  /**
148  * @brief Constructor
149  *
150  * This constructor allocates a concrete class
151  * derived from net::AbstractSystemSocket.
152  */
155 
156  /**
157  * @brief Method to connect the client to a certain local socket
158  *
159  * This method invokes the platform-specific connect() operation.
160  * @param addr Address (i.e., net::local::stream::address)
161  * containing the path of the local socket which the client must
162  * be connected to
163  */
164  inline void connect (const Address& addr)
165  {
166  AbstractSocket::socket_->connect(addr);
167  }
168 
169  /**
170  * @brief Method to open the client towards a certain address
171  *
172  * This method calls net::local::stream::connect(),
173  * which in turn invokes the platform-specific connect() operation.
174  * @param addr Address (i.e., net::local::stream::address)
175  * containing the path of the local socket which the client must
176  * open
177  */
178  inline void open (const Address& addr)
179  {
180  connect (addr);
181  }
182 
183 };
184 
185 }}} // net::local::stream
186 
187 #endif // STREAM_LOCAL_HPP_