- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
54 class GNet::ServerPeerHandle
55 {
56 public:
57 ServerPeerHandle() ;
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
83 class GNet::Server : public GNet::EventHandler
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 ;
94 Address m_address ;
95 std::string m_name ; // for local peers - not always available
96 ServerPeerHandle * m_handle ;
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
191 class GNet::ServerPeer : public GNet::EventHandler , public GNet::Connection , public GNet::SocketProtocolSink
192 {
193 public:
194 typedef std::string::size_type size_type ;
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
GNet::ServerPeer::onException
virtual void onException(std::exception &)
Final override from GNet::EventHandler.
Definition: gserver.cpp:77
GNet::Server::newPeer
virtual ServerPeer * newPeer(PeerInfo)=0
A factory method which new()s a ServerPeer-derived object.
GNet::ServerPeer
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:191
GNet::Server::PeerInfo::m_socket
std::auto_ptr< StreamSocket > m_socket
Definition: gserver.h:93
GNet::Server::canBind
static bool canBind(const Address &listening_address, bool do_throw)
Checks that the specified address can be bound.
Definition: gserver.cpp:172
GNet
Network classes.
GNet::ServerPeer::localAddress
virtual std::pair< bool, Address > localAddress() const
Returns the local address.
Definition: gserver.cpp:89
GNet::ServerPeer::peerAddress
virtual std::pair< bool, Address > peerAddress() const
Returns the peer address.
Definition: gserver.cpp:95
GNet::ServerPeer::writeEvent
virtual void writeEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:120
gsocketprotocol.h
GNet::ServerPeer::ServerPeer
ServerPeer(Server::PeerInfo)
Constructor.
Definition: gserver.cpp:33
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::Server::PeerInfo::m_address
Address m_address
Definition: gserver.h:94
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
GNet::ServerPeer::onSendComplete
virtual void onSendComplete()=0
Called after flow-control has been released and all residual data sent.
GNet::Server::PeerInfo::m_handle
ServerPeerHandle * m_handle
Definition: gserver.h:96
GNet::ServerPeer::server
Server * server()
Returns a pointer to the associated server object.
GNet::ServerPeerHandle::set
void set(ServerPeer *p)
Sets the pointer.
Definition: gserver.cpp:349
GNet::ConnectionLookup
A class for getting more information about a connection from the operating system.
Definition: gconnectionlookup.h:42
GNet::Server
A network server class which listens on a specific port and spins off ServerPeer objects for each inc...
Definition: gserver.h:83
GNet::ServerPeerHandle
A structure used in the implementation of GNet::Server.
Definition: gserver.h:54
GNet::StreamSocket
A derivation of Socket for a stream socket.
Definition: gsocket.h:270
GNet::Server::PeerInfo::m_name
std::string m_name
Definition: gserver.h:95
GNet::Server::readEvent
virtual void readEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:229
GNet::ServerPeer::send
bool send(const std::string &data, std::string::size_type offset=0U)
Sends data down the socket to the peer.
Definition: gserver.cpp:115
GNet::Server::Server
Server()
Default constructor. Initialise with init().
Definition: gserver.cpp:148
GNet::ServerPeer::socket
StreamSocket & socket()
Returns a reference to the client-server connection socket.
Definition: gserver.cpp:66
gtimer.h
GNet::ServerPeer::doDelete
void doDelete(const std::string &=std::string())
Does "onDelete(); delete this".
Definition: gserver.cpp:83
GNet::Server::PeerInfo::PeerInfo
PeerInfo()
Definition: gserver.cpp:320
gconnectionlookup.h
GNet::ServerPeer::onDelete
virtual void onDelete(const std::string &reason)=0
Called just before destruction.
gevent.h
GNet::Server::~Server
virtual ~Server()
Destructor.
Definition: gserver.cpp:182
GNet::EventHandler
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
GNet::ServerPeer::doDeleteThis
void doDeleteThis(int)
Does delete this. Should only be used by the GNet::Server class.
Definition: gserver.cpp:110
GNet::ServerPeer::readEvent
virtual void readEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:72
gsocket.h
GNet::Connection
An interface which provides address information for a network connection.
Definition: gconnection.h:39
G_EXCEPTION
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
GNet::ServerPeer::peerCertificate
virtual std::string peerCertificate() const
Returns the peer's TLS certificate.
Definition: gserver.cpp:100
GNet::ServerPeerHandle::reset
void reset()
Resets the pointer.
Definition: gserver.cpp:334
GNet::Server::writeEvent
virtual void writeEvent()
Final override from GNet::EventHandler.
Definition: gserver.cpp:313
GNet::ServerPeerHandle::old
ServerPeer * old()
Returns the pointer value before it was reset().
Definition: gserver.cpp:344
GNet::ServerPeer::logId
std::string logId() const
Returns an identification string for logging purposes.
Definition: gserver.cpp:56
GNet::Server::init
void init(unsigned int listening_port)
Initilisation after default construction.
Definition: gserver.cpp:154
GNet::Timer
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:103
GNet::Server::serverCleanup
void serverCleanup()
May be called from the derived class destructor in order to trigger early deletion of peer objects...
Definition: gserver.cpp:187
GNet::ServerPeer::sslAccept
void sslAccept()
Waits for the peer to start a secure session.
Definition: gserver.cpp:61
GNet::SocketProtocolSink
An interface used by GNet::SocketProtocol to deliver data from a socket.
Definition: gsocketprotocol.h:112
GNet::SocketProtocol
An interface for implementing a low-level protocol layer by means of calling read() and write() on a ...
Definition: gsocketprotocol.h:54
GNet::ServerPeer::~ServerPeer
virtual ~ServerPeer()
Destructor.
Definition: gserver.cpp:48
GNet::Server::PeerInfo
A structure used in GNet::Server::newPeer().
Definition: gserver.h:91
GNet::ServerPeerHandle::ServerPeerHandle
ServerPeerHandle()
Default constructor.
Definition: gserver.cpp:328
GNet::ServerPeer::size_type
std::string::size_type size_type
Definition: gserver.h:194
GNet::Server::address
std::pair< bool, Address > address() const
Returns the listening address.
Definition: gserver.cpp:221
GNet::ServerPeerHandle::peer
ServerPeer * peer()
Returns the pointer.
Definition: gserver.cpp:339
gconnection.h
GNet::Server::onException
virtual void onException(std::exception &e)
Final override from GNet::EventHandler.
Definition: gserver.cpp:215