NetCpp  v0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
abstract_system_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_SYSTEM_SOCKET_HPP_
30 #define ABSTRACT_SYSTEM_SOCKET_HPP_
31 
32 #include <unistd.h>
33 
34 #include "protocol.hpp"
35 #include "address.hpp"
36 
37 namespace net {
38 
39 /**
40  * @brief Virtual class for platform-dependent code.
41  *
42  * This is the virtual class which every platform-dependent implementation must
43  * inherit from.
44  * The class offers a set of primitives which must be implemented by the concrete
45  * derived classes.
46  */
48 
49 public:
50  /**
51  * \brief Method to read data from the socket.
52  *
53  * The buffer is filled with the read data.
54  * @param p buf Pointer to the memory address containing data
55  * @param size Number of bytes that must be read
56  * @return the number of bytes actually read; < 0 in case of error
57  */
58  virtual int read (void* buf, size_t size)=0;
59 
60  /**
61  * \brief Method to write data to the socket.
62  *
63  * @param p buf Pointer to the memory address containing data
64  * @param size Number of bytes that must be written
65  * @return the number of bytes actually written; < 0 in case of error
66  */
67  virtual int write (const void* buf, size_t size)=0;
68 
69  /**
70  * @brief Method to close the socket
71  *
72  * Note: currently there is no mechanism to re-open a closed socket.
73  * @return true in case of success; false otherwise
74  */
75  virtual bool close()=0;
76 
77  /**
78  * @brief Method to accept a connection on a socket
79  *
80  * @param sock Pointer to the socket on which the new connection must be accepted.
81  * @exception runtime_error in case of error
82  */
83  virtual void accept (AbstractSystemSocket* sock)=0;
84 
85  /**
86  * @brief Method to connect the socket to an address
87  *
88  * @param addr Address which the socket must be connected to
89  * @exception runtime_error in case of error
90  */
91  virtual void connect (const Address& addr)=0;
92 
93  /**
94  * @brief Method to bind the socket to an address
95  *
96  * @param addr Address which the socket must be bound to
97  * @exception runtime_error in case of error
98  */
99  virtual void bind (const Address& addr)=0;
100 
101  /**
102  * @brief Method to set the maximum number of pending connections
103  *
104  * This method allows to set the maximum number of pending connections for
105  * stream (e.g., TCP) communications.
106  * @param max_pending_connections Maximum number of pending connections
107  * @exception runtime_error in case of error or non-streamed communication
108  */
109  virtual void listen (int max_pending_connections)=0;
110 
111  /**
112  * @brief Method to get the socket protocol
113  *
114  * This method returns the protocol, which has been set at socket creation
115  * (and cannot be changed).
116  * @return Protocol used by the socket
117  */
119  return protocol_;
120  }
121 
122 protected:
123  /**
124  * @brief Constructor
125  *
126  * The constructor is protected because only derived classes can
127  * use it.
128  * @param prot Protocol used by the socket
129  */
131  protocol_{prot}{};
132 
133  /**
134  * @brief Protocol used by the socket.
135  *
136  * This protocol is set during socket creation and cannot be changed.
137  */
138  protocol protocol_;
139 };
140 
141 } // net
142 
143 #endif // ABSTRACT_SYSTEM_SOCKET_HPP_
144