libBsdSockets
C++ Wrapper classes to the BSD Socket API
Socket.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2013, Komodo Does Inc
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 
13 */
14 
15 #ifndef SOCKET_H
16 #define SOCKET_H
17 
18 #ifndef ADDRESS_INFO_H
19 #include "AddressInfo.h"
20 #endif // ADDRESS_INFO_H
21 
22 namespace BsdSockets {
23 
24  /*
25  * Forward Declarations
26  */
27  class Address;
28 
29  /** \class SelectResult
30  * \brief Result of Socket::select() indicating the result status.
31  */
32  class SelectResult {
33  public:
34  const bool checkedRead;
35  const bool checkedWrite;
36  const bool checkedError;
37 
38  const bool readyToRead;
39  const bool readyToWrite;
40  const bool readyWithError;
41 
42  const bool ready;
43 
45  bool theCheckedRead, bool theCheckedWrite, bool theCheckedError,
46  bool theReadyToRead, bool theReadyToWrite, bool theReadyWithError
47  )
48  : checkedRead(theCheckedRead), checkedWrite(theCheckedWrite), checkedError(theCheckedError),
49  readyToRead(theReadyToRead), readyToWrite(theReadyToWrite), readyWithError(theReadyWithError),
51  {
52  }
53  };
54 
55  /** \class Socket
56  *
57  * \brief BSD Socket base class providing the basic, common functionality between
58  * ServerSockets and ClientSockets.
59  *
60  * It provides creation (on creation), closing (on destruct), send/receive,
61  * and socket option set/get.
62  *
63  * \note The socket is created during the constructor and closed in the
64  * destructor. The socket may not be closed prior.
65  */
66  class Socket {
67  public:
68  typedef std::shared_ptr<Socket> Ptr;
69 
70  /** Closes socket. Virtual destructor to support derived classes. */
71  virtual ~Socket();
72 
73  /** Create Socket from an existing low-level socket id.
74  *
75  * @param theAddress to create socket to work for
76  * @param existingLowLevelSocket low-level socket id of an existing socket
77  *
78  * @return the socket
79  */
80  static Socket::Ptr create(std::shared_ptr<Address> theAddress, int existingLowLevelSocket);
81 
82  protected:
83  /** Create Socket to support an address, reading parameters from address.
84  *
85  * @param theAddress to create socket to work for
86  */
87  Socket(std::shared_ptr<Address> theAddress);
88 
89  private:
90  /** Create Socket from an existing low-level socket id.
91  *
92  * @param theAddress to create socket to work for
93  * @param existingLowLevelSocket low-level socket id of an existing socket
94  */
95  Socket(std::shared_ptr<Address> theAddress, int existingLowLevelSocket);
96 
97  Socket(const Socket& rhs) = delete;
98  Socket(Socket&&) = delete;
99  Socket& operator=(const Socket& rhs) = delete;
100  Socket& operator=(Socket&& rhs) = delete;
101 
102  public:
103  /** Check state of socket without timeout_ms
104  *
105  * @param timeout_ms timeout in milliseconds to stop trying to check
106  * @param checkRead true if read should be checked
107  * @param checkWrite true if write should be checked
108  * @param checkError true if error should be checked
109  *
110  * @return SelectResult containing results of select
111  */
112  SelectResult select(int timeout_ms, bool checkRead, bool checkWrite, bool checkError);
113 
114  /** poll the Socket for eventsToLookFor timeout out after timeout_ms
115  *
116  * @param timeout_ms time in ms to try to find eventsToLookFor
117  * @param eventsToLookFor low-level events from %poll() to look for
118  *
119  * @return low-level events from %poll() to look for
120  */
121  short poll(int timeout_ms, short eventsToLookFor);
122 
123  public:
124  /** Send length bytes from buffer with flags to socket.
125  *
126  * @param buffer to read
127  * @param length bytes to read from buffer and write to socket
128  * @param flags to use when sending.
129  *
130  * \see %send() flags parameter for details of flags
131  *
132  * @return bytes sent
133  */
134  ssize_t send(const void* buffer, size_t length, int flags = 0) const;
135 
136  /** Read up to length bytes into buffer with flags from socket.
137  *
138  * @param buffer to write
139  * @param length bytes to read from socket and write to buffer
140  * @param flags to use when receiving.
141  *
142  * @return bytes read
143  *
144  * \see %send() flags parameter for details of flags
145  */
146  ssize_t receive(void *buffer, size_t length, int flags = 0) const;
147 
148  /** Read up to length bytes into buffer with flags from socket, blocking
149  * until ready or error..
150  *
151  * @param buffer to write
152  * @param length bytes to read from socket and write to buffer
153  *
154  * @return bytes read. 0 is a remote closed socket.
155  */
156  ssize_t blockingReceive(void *buffer, size_t length) const;
157 
158  public:
159  /** Get socket option optionName at network level, saving to buffer
160  * optionValue and to length optionLength.
161  *
162  * @param level network level of option
163  * @param optionName name of the option at level
164  * @param optionValue to save value to
165  * @param optionLength to save length of optionValue to
166  */
167  void getSocketOption(int level, int optionName, void* optionValue, socklen_t* optionLength) const;
168 
169  /** Set socket option optionName at network level, reading from buffer
170  * optionValue and from length optionLength.
171  *
172  * @param level network level of option
173  * @param optionName name of the option at level
174  * @param optionValue to read value from
175  * @param optionLength length of optionValue
176  */
177  void setSocketOption(int level, int optionName, const void* optionValue, socklen_t optionLength) const;
178 
179  public:
180  /** @return the low-level socket number */
181  int getLowLevelSocket() const;
182 
183  /** @return the address of this socket */
184  std::shared_ptr<Address> getAddress() const;
185 
186  private:
187  /** The low-level socket number */
188  const int lowLevelSocket;
189 
190  /** The address of this socket */
191  std::shared_ptr<Address> address;
192  };
193 
194 } // namespace BsdSockets
195 
196 #endif // SOCKET_H
Result of Socket::select() indicating the result status.
Definition: Socket.h:32
const bool ready
Definition: Socket.h:42
const bool checkedWrite
Definition: Socket.h:35
SelectResult(bool theCheckedRead, bool theCheckedWrite, bool theCheckedError, bool theReadyToRead, bool theReadyToWrite, bool theReadyWithError)
Definition: Socket.h:44
const bool checkedRead
Definition: Socket.h:34
const bool checkedError
Definition: Socket.h:36
const bool readyToWrite
Definition: Socket.h:39
const bool readyToRead
Definition: Socket.h:38
const bool readyWithError
Definition: Socket.h:40
BSD Socket base class providing the basic, common functionality between ServerSockets and ClientSocke...
Definition: Socket.h:66
static Socket::Ptr create(std::shared_ptr< Address > theAddress, int existingLowLevelSocket)
Definition: Socket.cpp:38
int getLowLevelSocket() const
Definition: Socket.cpp:139
short poll(int timeout_ms, short eventsToLookFor)
Definition: Socket.cpp:83
ssize_t receive(void *buffer, size_t length, int flags=0) const
Definition: Socket.cpp:105
std::shared_ptr< Socket > Ptr
Definition: Socket.h:68
ssize_t send(const void *buffer, size_t length, int flags=0) const
Definition: Socket.cpp:96
void setSocketOption(int level, int optionName, const void *optionValue, socklen_t optionLength) const
Definition: Socket.cpp:129
std::shared_ptr< Address > getAddress() const
Definition: Socket.cpp:135
void getSocketOption(int level, int optionName, void *optionValue, socklen_t *optionLength) const
Definition: Socket.cpp:123
Socket(std::shared_ptr< Address > theAddress)
ssize_t blockingReceive(void *buffer, size_t length) const
Definition: Socket.cpp:114
SelectResult select(int timeout_ms, bool checkRead, bool checkWrite, bool checkError)
Definition: Socket.cpp:61
virtual ~Socket()
Definition: Socket.cpp:29
Namespace of the BsdSockets library.
Definition: Address.cpp:20