gserver.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // ===
20 
21 #ifndef G_SERVER_H
22 #define G_SERVER_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gsocket.h"
27 #include "gsocketprotocol.h"
28 #include "gtimer.h"
29 #include "gconnection.h"
30 #include "gconnectionlookup.h"
31 #include "gevent.h"
32 #include <utility>
33 #include <list>
34 #include <memory>
35 #include <string>
36 
38 namespace GNet
39 {
40  class Server ;
41  class ServerPeer ;
42  class ServerPeerTimer ;
43  class ServerPeerHandle ;
44 }
45 
55 {
56 public:
59 
60  void set( ServerPeer * p ) ;
62 
63  void reset() ;
65 
66  ServerPeer * peer() ;
68 
69  ServerPeer * old() ;
72 
73 private:
74  ServerPeer * m_p ;
75  ServerPeer * m_old ;
76 } ;
77 
84 {
85 public:
86  G_EXCEPTION( CannotBind , "cannot bind the listening port" ) ;
87  G_EXCEPTION( CannotListen , "cannot listen" ) ;
88  G_EXCEPTION( AcceptError , "socket accept() failed" ) ;
89 
91  struct PeerInfo
92  {
93  std::auto_ptr<StreamSocket> m_socket ;
95  std::string m_name ; // for local peers - not always available
97  PeerInfo() ;
98  } ;
99 
100  static bool canBind( const Address & listening_address , bool do_throw ) ;
104 
105  explicit Server( unsigned int listening_port , ConnectionLookup * = NULL ) ;
108 
109  explicit Server( const Address & listening_address , ConnectionLookup * = NULL ) ;
112 
113  Server() ;
115 
116  void init( unsigned int listening_port ) ;
118 
119  void init( const Address & listening_address ) ;
121 
122  virtual ~Server() ;
124 
125  std::pair<bool,Address> address() const ;
128 
129  virtual void readEvent() ;
131 
132  virtual void writeEvent() ;
134 
135  virtual void onException( std::exception & e ) ;
137 
138 protected:
139  virtual ServerPeer * newPeer( PeerInfo ) = 0 ;
160 
161  void serverCleanup() ;
168 
169 private:
170  Server( const Server & ) ; // not implemented
171  void operator=( const Server & ) ; // not implemented
172  void serverCleanupCore() ;
173  void collectGarbage() ;
174  void accept( PeerInfo & ) ;
175 
176 private:
177  typedef std::list<ServerPeerHandle> PeerList ;
178  std::auto_ptr<StreamSocket> m_socket ;
179  ConnectionLookup * m_connection_lookup ;
180  PeerList m_peer_list ;
181  bool m_cleaned_up ;
182 } ;
183 
192 {
193 public:
195 
196  explicit ServerPeer( Server::PeerInfo ) ;
199 
200  bool send( const std::string & data , std::string::size_type offset = 0U ) ;
204 
205  void doDelete( const std::string & = std::string() ) ;
207 
208  std::string logId() const ;
210 
211  virtual std::pair<bool,Address> localAddress() const ;
214 
215  virtual std::pair<bool,Address> peerAddress() const ;
218 
219  virtual std::string peerCertificate() const ;
222 
223  virtual void readEvent() ;
225 
226  virtual void writeEvent() ;
228 
229  virtual void onException( std::exception & ) ;
231 
232  void doDeleteThis( int ) ;
234 
235 protected:
236  virtual ~ServerPeer() ;
240 
241  virtual void onDelete( const std::string & reason ) = 0 ;
244 
245  virtual void onSendComplete() = 0 ;
252 
253  void sslAccept() ;
256 
257  StreamSocket & socket() ;
260 
261  Server * server() ;
264 
265 private:
266  ServerPeer( const ServerPeer & ) ; // not implemented
267  void operator=( const ServerPeer & ) ; // not implemented
268  void onTimeout() ;
269 
270 private:
271  Address m_address ;
272  std::auto_ptr<StreamSocket> m_socket ; // order dependency -- first
273  SocketProtocol m_sp ; // order dependency -- second
274  ServerPeerHandle * m_handle ;
275  Timer<ServerPeer> m_delete_timer ;
276 } ;
277 
278 #endif
virtual void onException(std::exception &)
Final override from GNet::EventHandler.
Definition: gserver.cpp:77
virtual ServerPeer * newPeer(PeerInfo)=0
A factory method which new()s a ServerPeer-derived object.
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:191
std::auto_ptr< StreamSocket > m_socket
Definition: gserver.h:93
static bool canBind(const Address &listening_address, bool do_throw)
Checks that the specified address can be bound.
Definition: gserver.cpp:172
Network classes.
virtual std::pair< bool, Address > localAddress() const
Returns the local address.
Definition: gserver.cpp:89
virtual std::pair< bool, Address > peerAddress() const
Returns the peer address.
Definition: gserver.cpp:95
virtual void writeEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:120
ServerPeer(Server::PeerInfo)
Constructor.
Definition: gserver.cpp:33
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
virtual void onSendComplete()=0
Called after flow-control has been released and all residual data sent.
ServerPeerHandle * m_handle
Definition: gserver.h:96
Server * server()
Returns a pointer to the associated server object.
void set(ServerPeer *p)
Sets the pointer.
Definition: gserver.cpp:349
A class for getting more information about a connection from the operating system.
A network server class which listens on a specific port and spins off ServerPeer objects for each inc...
Definition: gserver.h:83
A structure used in the implementation of GNet::Server.
Definition: gserver.h:54
A derivation of Socket for a stream socket.
Definition: gsocket.h:270
std::string m_name
Definition: gserver.h:95
virtual void readEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:229
bool send(const std::string &data, std::string::size_type offset=0U)
Sends data down the socket to the peer.
Definition: gserver.cpp:115
Server()
Default constructor. Initialise with init().
Definition: gserver.cpp:148
StreamSocket & socket()
Returns a reference to the client-server connection socket.
Definition: gserver.cpp:66
void doDelete(const std::string &=std::string())
Does "onDelete(); delete this".
Definition: gserver.cpp:83
virtual void onDelete(const std::string &reason)=0
Called just before destruction.
virtual ~Server()
Destructor.
Definition: gserver.cpp:182
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
void doDeleteThis(int)
Does delete this. Should only be used by the GNet::Server class.
Definition: gserver.cpp:110
virtual void readEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:72
An interface which provides address information for a network connection.
Definition: gconnection.h:39
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
virtual std::string peerCertificate() const
Returns the peer's TLS certificate.
Definition: gserver.cpp:100
void reset()
Resets the pointer.
Definition: gserver.cpp:334
virtual void writeEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:313
ServerPeer * old()
Returns the pointer value before it was reset().
Definition: gserver.cpp:344
std::string logId() const
Returns an identification string for logging purposes.
Definition: gserver.cpp:56
void init(unsigned int listening_port)
Initilisation after default construction.
Definition: gserver.cpp:154
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:103
void serverCleanup()
May be called from the derived class destructor in order to trigger early deletion of peer objects...
Definition: gserver.cpp:187
void sslAccept()
Waits for the peer to start a secure session.
Definition: gserver.cpp:61
An interface used by GNet::SocketProtocol to deliver data from a socket.
An interface for implementing a low-level protocol layer by means of calling read() and write() on a ...
virtual ~ServerPeer()
Destructor.
Definition: gserver.cpp:48
A structure used in GNet::Server::newPeer().
Definition: gserver.h:91
ServerPeerHandle()
Default constructor.
Definition: gserver.cpp:328
std::string::size_type size_type
Definition: gserver.h:194
std::pair< bool, Address > address() const
Returns the listening address.
Definition: gserver.cpp:221
ServerPeer * peer()
Returns the pointer.
Definition: gserver.cpp:339
virtual void onException(std::exception &e)
Final override from GNet::EventHandler.
Definition: gserver.cpp:215