libBsdSockets
C++ Wrapper classes to the BSD Socket API
ServerSocket.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 SERVER_SOCKET_H
16 #define SERVER_SOCKET_H
17 
18 #ifndef SOCKET_H
19 #include "Socket.h"
20 #endif // SOCKET_H
21 
22 #include <functional>
23 
24 namespace BsdSockets {
25 
26  /*
27  * Forward Declarations
28  */
29  class Address;
30 
31  /** \class ServerSocket
32  *
33  * \brief Server Socket providing ability to bind, listen and accept connections
34  *
35  * Datagram sockets may send/receive with the ServerSocket directly.
36  * Other Sockets use the accept() returned Sockets.
37  */
38  class ServerSocket : public Socket {
39  public:
40  typedef std::shared_ptr<ServerSocket> Ptr;
41 
42  /** Create a ServerSocket to theAddress. This still needs bind() and listen().
43  *
44  * @param theAddress to open, bind and listen the ServerSocket on
45  * @return the socket
46  *
47  * @see %listen() parameter backlog for more details
48  */
49  static ServerSocket::Ptr create(std::shared_ptr<Address> theAddress);
50 
51  /** Create and open a ServerSocket to theAddress.
52  *
53  * @param theAddress to open, bind and listen the ServerSocket on
54  * @param backlog length before events error
55  * @param reuse true to allow reuse of address and port, or false for default for timeout before reuse
56  * @return the socket
57  *
58  * @see %listen() parameter backlog for more details
59  */
60  static ServerSocket::Ptr open(std::shared_ptr<Address> theAddress, int backlog = 128, bool reuse = false);
61 
62  /** Closes socket. Virtual destructor to support derived classes. */
63  virtual ~ServerSocket();
64 
65  protected:
66  /** Create ServerSocket to listen to the given address.
67  *
68  * @param theAddress to bind and listen to
69  */
70  ServerSocket(std::shared_ptr<Address> theAddress);
71 
72  private:
73  ServerSocket(const Socket& rhs) = delete;
74  ServerSocket(Socket&&) = delete;
75  ServerSocket& operator=(const Socket& rhs) = delete;
76  ServerSocket& operator=(Socket&& rhs) = delete;
77 
78  public:
79  /** Bind this Socket to the address */
80  void bind() const;
81 
82  /** Listen to the Socket with the given backlog.
83  *
84  * @param backlog maximum length for the queue of pending connections.
85  *
86  * @see %listen() parameter backlog for more details
87  */
88  void listen(int backlog = 128) const;
89 
90  /** Accept a connection on the Server Socket.
91  *
92  * @return the Socket accepted
93  */
94  Socket::Ptr accept() const;
95 
96  /** Accept a connection on the Server Socket and pass it as the argument to
97  * the function-wrapper toCall.
98  *
99  * @param toCall function to call with new socket
100  *
101  * @return thread created
102  */
103  std::thread acceptInNewThread(std::function<void (Socket::Ptr)> toCall);
104  };
105 
106 } // namespace BsdSockets
107 
108 #endif // SERVER_SOCKET_H
Server Socket providing ability to bind, listen and accept connections.
Definition: ServerSocket.h:38
std::thread acceptInNewThread(std::function< void(Socket::Ptr)> toCall)
static ServerSocket::Ptr open(std::shared_ptr< Address > theAddress, int backlog=128, bool reuse=false)
Socket::Ptr accept() const
static ServerSocket::Ptr create(std::shared_ptr< Address > theAddress)
void listen(int backlog=128) const
std::shared_ptr< ServerSocket > Ptr
Definition: ServerSocket.h:40
ServerSocket(std::shared_ptr< Address > theAddress)
BSD Socket base class providing the basic, common functionality between ServerSockets and ClientSocke...
Definition: Socket.h:66
std::shared_ptr< Socket > Ptr
Definition: Socket.h:68
Namespace of the BsdSockets library.
Definition: Address.cpp:20