NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
abstract_socket.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 ABSTRACT_SOCKET_HPP_
30 #define ABSTRACT_SOCKET_HPP_
31 
32 #include <unistd.h>
33 #include <mutex>
34 #include <stdexcept>
35 #include <array>
36 #include <thread>
37 
39 #include "logger.hpp"
40 #include "platform.hpp"
41 
42 namespace net {
43 
44 /** @brief Structure to avoid issues when giving buffers to AbstractSocket.
45  *
46  * This structure allows to avoid issues about templates when giving a buffer
47  * to AbstractSocket. It is not meant to be directly used by the user.
48  * The user must only use buffer(...).
49  */
50 struct __buffer {
51  /// Pointer to data in memory
52  void* ptr_;
53 
54  /// Size (in bytes) of data
55  std::size_t size_;
56 };
57 
58 /**
59  * @brief Function to pass a std::array to AbstractSocket
60  *
61  * @param reference to a std::array<char>
62  * @return a __buffer structure
63  */
64 template<std::size_t N>
65 inline __buffer buffer(std::array<char, N>& buf)
66 {
67  return __buffer {reinterpret_cast<void*>(buf.data()), buf.size()};
68 }
69 
70 /**
71  * @brief Base abstract class for protocols.
72  *
73  * This is the base abstract class for protocols. Any supported protocol
74  * must inherit from this class.
75  */
77 public:
78 
79  /**
80  * @brief Close the socket
81  *
82  * Method to close the socket. Currently, there is no mechanism
83  * to re-open a closed socket.
84  *
85  * @return true in case of success; false otherwise
86  */
87  inline bool close()
88  {
89  return socket_->close();
90  }
91 
92  /**
93  * @brief get the socket
94  *
95  * This method is used to let derived classes of AbstractSocket invoke
96  * specific protocol-dependent functions (e.g., accept()).
97  *
98  * @return the socket used by this instance of the class
99  */
101  {
102  return socket_.get();
103  }
104 
105  int receive (__buffer buf, std::size_t size);
106  int send (__buffer buf, std::size_t size);
107 
108 protected:
109  /**
110  * @brief Constructor
111  *
112  * Constructor is protected because only derived classes
113  * can construct this class.
114  * The object is constructed through net::createSocket(),
115  * which creates a platform-dependent socket.
116  * @param the protocol type
117  */
118  AbstractSocket(const protocol& prot):
119  socket_{createSocket(prot)} {}
120 
121 
122  /**
123  * @brief Pointer to the platform-dependent socket
124  *
125  * The platform-dependent socket
126  */
127  std::unique_ptr<AbstractSystemSocket> socket_;
128 
129 private:
130 
131  int __receive (void* buffer, size_t size);
132  int __send (const void* buffer, size_t size);
133 
134  /**
135  * @brief Thread for asynchronous receive operations
136  */
137  std::unique_ptr<std::thread> receive_worker_;
138 
139  /**
140  * @brief Thread for asynchronous send operations
141  */
142  std::unique_ptr<std::thread> send_worker_;
143 
144  /**
145  * @brief Mutex for synchronization with asynchronous receive operations.
146  */
147  std::mutex receive_lock_;
148 
149  /**
150  * @brief Mutex for synchronization with asynchronous send operations.
151  */
152  std::mutex send_lock_;
153 };
154 
155 } // net
156 
157 #endif // ABSTRACT_SOCKET_HPP_