E-MailRelay
gmultiserver.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gmultiserver.h
19 ///
20 
21 #ifndef G_NET_MULTI_SERVER__H
22 #define G_NET_MULTI_SERVER__H
23 
24 #include "gdef.h"
25 #include "gserver.h"
26 #include <vector>
27 #include <utility> // std::pair<>
28 
29 namespace GNet
30 {
31  class MultiServer ;
32  class MultiServerImp ;
33  class MultiServerPtr ;
34 }
35 
36 /// \class GNet::MultiServerImp
37 /// A private implementation class used by GNet::MultiServer.
38 ///
40 {
41 public:
43  ///< Constructor.
44 
45  void cleanup() ;
46  ///< Does cleanup.
47 
48 private:
49  virtual ServerPeer * newPeer( PeerInfo ) override ;
50 
51 private:
52  MultiServer & m_ms ;
53 } ;
54 
55 /// \class GNet::MultiServerPtr
56 /// A private implementation class used by GNet::MultiServer.
57 /// The implementation is unusual in that it only has proper value semantics
58 /// if the contained pointer is null.
59 ///
61 {
62 public:
64 
65  explicit MultiServerPtr( ServerImp * = nullptr ) ;
66  ///< Constructor.
67 
68  ~MultiServerPtr() ;
69  ///< Destructor.
70 
71  void swap( MultiServerPtr & ) ;
72  ///< Swaps internals with the other.
73 
74  MultiServerImp * get() ;
75  ///< Returns the raw pointer.
76 
77  const MultiServerImp * get() const ;
78  ///< Returns the raw const pointer.
79 
80  MultiServerPtr( const MultiServerPtr & ) ;
81  ///< Copy constructor.
82 
83  void operator=( const MultiServerPtr & ) ;
84  ///< Assignment operator.
85 
86 private:
87  MultiServerImp * m_p ;
88 } ;
89 
90 /// \class GNet::MultiServer
91 /// A server that listens on more than one interface using a facade
92 /// pattern to multiple GNet::Server instances.
93 ///
95 {
96 public:
97  typedef std::vector<Address> AddressList ;
98  typedef Server::PeerInfo PeerInfo ;
99 
100  struct ServerInfo /// A structure used in GNet::MultiServer::newPeer().
101  {
102  Address m_address ; ///< The server address that the peer connected to.
103  ServerInfo() ;
104  } ;
105 
106  MultiServer( ExceptionHandler & , const AddressList & address_list ) ;
107  ///< Constructor. The server listens on on the
108  ///< specific (local) interfaces.
109  ///<
110  ///< Precondition: ! address_list.empty()
111 
112  explicit MultiServer( ExceptionHandler & ) ;
113  ///< Default constructor. Initialise with init().
114 
115  void init( const AddressList & address_list ) ;
116  ///< Initilisation after default construction.
117  ///<
118  ///< Precondition: ! address_list.empty()
119 
120  virtual ~MultiServer() ;
121  ///< Destructor.
122 
123  std::pair<bool,Address> firstAddress() const ;
124  ///< Returns the first listening address. The boolean
125  ///< value is false if none.
126 
127  virtual ServerPeer * newPeer( PeerInfo , ServerInfo ) = 0 ;
128  ///< A factory method which new()s a ServerPeer-derived
129  ///< object. See Server for the details.
130 
131  static bool canBind( const AddressList & listening_address_list , bool do_throw ) ;
132  ///< Checks that the specified addresses can be
133  ///< bound. Throws CannotBind if an address cannot
134  ///< be bound and 'do_throw' is true.
135 
136  static AddressList addressList( const Address & ) ;
137  ///< A trivial convenience fuction that returns the given
138  ///< address as a single-element list.
139 
140  static AddressList addressList( const AddressList & , unsigned int port ) ;
141  ///< Returns the given list of addresses with the port set
142  ///< correctly. If the given list is empty then a single
143  ///< 'any' address is returned.
144 
145  static AddressList addressList( const G::StringArray & , unsigned int port ) ;
146  ///< A convenience function that returns a list of
147  ///< listening addresses given a list of listening
148  ///< interfaces and a port number. If the list of
149  ///< interfaces is empty then a single 'any' address
150  ///< is returned.
151 
152 protected:
153  void serverCleanup() ;
154  ///< Should be called from all derived classes' destructors
155  ///< so that peer objects can use their Server objects
156  ///< safely during their own destruction.
157 
158  void serverReport( const std::string & server_type ) const ;
159  ///< Writes to the system log a summary of the underlying server objects
160  ///< and their addresses.
161 
162 private:
163  MultiServer( const MultiServer & ) ; // not implemented
164  void operator=( const MultiServer & ) ; // not implemented
165  void init( const Address & ) ;
166 
167 private:
168  typedef std::list<MultiServerPtr> List ;
169  ExceptionHandler & m_eh ;
170  List m_server_list ;
171 } ;
172 
173 #endif
static AddressList addressList(const Address &)
A trivial convenience fuction that returns the given address as a single-element list.
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:173
A private implementation class used by GNet::MultiServer.
Definition: gmultiserver.h:60
Network classes.
Definition: gaddress.h:29
void swap(MultiServerPtr &)
Swaps internals with the other.
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
MultiServerImp(MultiServer &, ExceptionHandler &, const Address &)
Constructor.
A network server class which listens on a specific port and spins off ServerPeer objects for each inc...
Definition: gserver.h:80
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
static bool canBind(const AddressList &listening_address_list, bool do_throw)
Checks that the specified addresses can be bound.
void cleanup()
Does cleanup.
A structure used in GNet::MultiServer::newPeer().
Definition: gmultiserver.h:100
virtual ~MultiServer()
Destructor.
A private implementation class used by GNet::MultiServer.
Definition: gmultiserver.h:39
MultiServerPtr(ServerImp *=nullptr)
Constructor.
void init(const AddressList &address_list)
Initilisation after default construction.
virtual ServerPeer * newPeer(PeerInfo, ServerInfo)=0
A factory method which new()s a ServerPeer-derived object.
void serverCleanup()
Should be called from all derived classes' destructors so that peer objects can use their Server obje...
~MultiServerPtr()
Destructor.
A server that listens on more than one interface using a facade pattern to multiple GNet::Server inst...
Definition: gmultiserver.h:94
Address m_address
The server address that the peer connected to.
Definition: gmultiserver.h:102
std::pair< bool, Address > firstAddress() const
Returns the first listening address.
MultiServer(ExceptionHandler &, const AddressList &address_list)
Constructor.
A structure used in GNet::Server::newPeer().
Definition: gserver.h:85
void serverReport(const std::string &server_type) const
Writes to the system log a summary of the underlying server objects and their addresses.
void operator=(const MultiServerPtr &)
Assignment operator.