E-MailRelay
gserverpeer.h
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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 gserverpeer.h
19///
20
21#ifndef G_NET_SERVER_PEER_H
22#define G_NET_SERVER_PEER_H
23
24#include "gdef.h"
25#include "gsocket.h"
26#include "gsocketprotocol.h"
27#include "gexception.h"
28#include "gaddress.h"
29#include "glinebuffer.h"
30#include "gtimer.h"
31#include "gconnection.h"
32#include "gexceptionsource.h"
33#include "gevent.h"
34#include "gstringview.h"
35#include <utility>
36#include <memory>
37#include <string>
38
39namespace GNet
40{
41 class Server ;
42 class ServerPeer ;
43 class ServerPeerConfig ;
44 class ServerPeerInfo ;
45}
46
47//| \class GNet::ServerPeerConfig
48/// A structure that GNet::Server uses to configure its ServerPeer objects.
49///
51{
52public:
53 unsigned int idle_timeout{0U} ;
55 explicit ServerPeerConfig( unsigned int idle_timeout ) ;
56 ServerPeerConfig & set_idle_timeout( unsigned int ) ;
57} ;
58
59//| \class GNet::ServerPeer
60/// An abstract base class for the GNet::Server's connection to a remote
61/// client. Instances are created on the heap by the Server::newPeer()
62/// override. Exceptions are delivered to the owning Server and result
63/// in a call to the relevant ServerPeer's onDelete() method followed
64/// by its deletion.
65/// \see GNet::Server, GNet::EventHandler
66///
68{
69public:
70 G_EXCEPTION( IdleTimeout , "idle timeout" ) ;
71
73 ///< Constructor. This constructor is only used from within the
74 ///< override of GNet::Server::newPeer(). The ExceptionSink refers
75 ///< to the owning Server.
76
77 ~ServerPeer() override ;
78 ///< Destructor.
79
80 bool send( const std::string & data , std::size_t offset = 0U ) ;
81 ///< Sends data down the socket to the peer. Returns true if completely
82 ///< sent; returns false if flow control asserted (see onSendComplete()).
83 ///< If flow control is asserted then there should be no new calls to
84 ///< send() until onSendComplete() is triggered.
85 ///< Throws on error.
86
87 bool send( const std::vector<G::string_view> & data ) ;
88 ///< Overload to send data using scatter-gather segments. If false is
89 ///< returned then segment data pointers must stay valid until
90 ///< onSendComplete() is triggered.
91
92 std::pair<bool,Address> localAddress() const override ;
93 ///< Returns the local address. Pair.first is false on error.
94 ///< Override from GNet::Connection.
95
96 std::pair<bool,Address> peerAddress() const override ;
97 ///< Returns the peer address.
98 ///< Override from GNet::Connection.
99
100 std::string connectionState() const override ;
101 ///< Returns the connection state display string.
102 ///< Override from GNet::Connection.
103
104 std::string peerCertificate() const override ;
105 ///< Returns the peer's TLS certificate.
106 ///< Override from GNet::Connection.
107
108 void doOnDelete( const std::string & reason , bool done ) ;
109 ///< Used by the Server class to call onDelete().
110
112 ///< Returns information about the state of the internal
113 ///< line-buffer.
114
115protected:
116 virtual void onSendComplete() = 0 ;
117 ///< Called after flow-control has been released and all
118 ///< residual data sent.
119
120 virtual bool onReceive( const char * data , std::size_t size , std::size_t eolsize , std::size_t linesize , char c0 ) = 0 ;
121 ///< Called on receipt of data. See GNet::LineBuffer.
122
123 virtual void onDelete( const std::string & reason ) = 0 ;
124 ///< Called just before the Server deletes this ServerPeer as
125 ///< the result of an exception (but not as a result of Server
126 ///< destruction). The reason is the empty string if caused
127 ///< by a GNet::Done exception. Consider making the implementation
128 ///< non-throwing, in the spirit of a destructor, since the
129 ///< ServerPeer object is about to be deleted.
130
131 void secureAccept() ;
132 ///< Waits for the peer to start a secure session. Uses a
133 ///< profile called "server"; see GSsl::Library::addProfile().
134 ///< The callback GNet::SocketProtocolSink::onSecure() is
135 ///< triggered when the secure session is established.
136
137 StreamSocket & socket() ;
138 ///< Returns a reference to the client-server connection
139 ///< socket.
140
141 void expect( std::size_t ) ;
142 ///< Modifies the line buffer state so that it delivers
143 ///< a chunk of non-line-delimited data.
144
145private: // overrides
146 void readEvent() override ; // Override from GNet::EventHandler.
147 void writeEvent() override ; // Override from GNet::EventHandler.
148 void otherEvent( EventHandler::Reason ) override ; // Override from GNet::EventHandler.
149 std::string exceptionSourceId() const override ; // Override from GNet::ExceptionSource.
150
151protected:
152 void onData( const char * , std::size_t ) override ;
153 ///< Override from GNet::SocketProtocolSink.
154 ///< Protected to allow derived classes to ignore
155 ///< incoming data for DoS prevention.
156
157public:
158 ServerPeer( const ServerPeer & ) = delete ;
159 ServerPeer( ServerPeer && ) = delete ;
160 void operator=( const ServerPeer & ) = delete ;
161 void operator=( ServerPeer && ) = delete ;
162
163private:
164 void onIdleTimeout() ;
165 bool onDataImp( const char * , std::size_t , std::size_t , std::size_t , char ) ;
166
167private:
168 Address m_address ;
169 std::shared_ptr<StreamSocket> m_socket ; // order dependency -- first
170 SocketProtocol m_sp ; // order dependency -- second
171 LineBuffer m_line_buffer ;
172 ServerPeerConfig m_config ;
173 Timer<ServerPeer> m_idle_timer ;
174 mutable std::string m_exception_source_id ;
175} ;
176
177#endif
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:53
An abstract interface which provides address information for a network connection.
Definition: gconnection.h:38
A base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:48
A tuple containing an ExceptionHandler interface pointer and a bound 'exception source' pointer.
A mixin base class that identifies the source of an exception when delivered to GNet::ExceptionHandle...
A configuration structure for GNet::LineBuffer.
Definition: glinebuffer.h:325
Provides information abount the state of a line buffer.
Definition: glinebuffer.h:383
A class that does line buffering, supporting auto-detection of line endings and fixed-size block extr...
Definition: glinebuffer.h:83
A structure that GNet::Server uses to configure its ServerPeer objects.
Definition: gserverpeer.h:51
A structure used in GNet::Server::newPeer().
Definition: gserver.h:142
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserverpeer.h:68
void expect(std::size_t)
Modifies the line buffer state so that it delivers a chunk of non-line-delimited data.
Definition: gserverpeer.cpp:61
~ServerPeer() override
Destructor.
Definition: gserverpeer.cpp:50
void secureAccept()
Waits for the peer to start a secure session.
Definition: gserverpeer.cpp:56
virtual bool onReceive(const char *data, std::size_t size, std::size_t eolsize, std::size_t linesize, char c0)=0
Called on receipt of data. See GNet::LineBuffer.
std::string connectionState() const override
Returns the connection state display string.
Definition: gserverpeer.cpp:93
bool send(const std::string &data, std::size_t offset=0U)
Sends data down the socket to the peer.
void doOnDelete(const std::string &reason, bool done)
Used by the Server class to call onDelete().
LineBufferState lineBuffer() const
Returns information about the state of the internal line-buffer.
std::pair< bool, Address > peerAddress() const override
Returns the peer address.
Definition: gserverpeer.cpp:88
virtual void onDelete(const std::string &reason)=0
Called just before the Server deletes this ServerPeer as the result of an exception (but not as a res...
std::string peerCertificate() const override
Returns the peer's TLS certificate.
Definition: gserverpeer.cpp:98
std::pair< bool, Address > localAddress() const override
Returns the local address.
Definition: gserverpeer.cpp:82
StreamSocket & socket()
Returns a reference to the client-server connection socket.
Definition: gserverpeer.cpp:66
virtual void onSendComplete()=0
Called after flow-control has been released and all residual data sent.
void onData(const char *, std::size_t) override
Override from GNet::SocketProtocolSink.
ServerPeer(ExceptionSink, const ServerPeerInfo &, const LineBufferConfig &)
Constructor.
Definition: gserverpeer.cpp:28
An interface used by GNet::SocketProtocol to deliver data from a socket.
An interface for implementing a low-level TLS/SSL protocol layer on top of a connected non-blocking s...
A derivation of GNet::Socket for a stream socket.
Definition: gsocket.h:311
A timer class template in which the timeout is delivered to the specified method.
Definition: gtimer.h:129
Network classes.
Definition: gdef.h:1115