VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gsocketprotocol.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 gsocketprotocol.h
19 ///
20 
21 #ifndef G_SOCKET_PROTOCOL_H
22 #define G_SOCKET_PROTOCOL_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gsocket.h"
27 #include "gexception.h"
28 #include <string>
29 #include <vector>
30 #include <utility>
31 
32 namespace GNet
33 {
34  class SocketProtocol ;
35  class SocketProtocolImp ;
36  class SocketProtocolSink ;
37 }
38 
39 /// \class GNet::SocketProtocol
40 /// An interface for implementing a low-level TLS/SSL protocol layer on top
41 /// of a connected non-blocking socket.
42 ///
43 /// Provides send() to send data, and onData() in a callback interface to
44 /// receive data. The TLS/SSL socket protocol session is negotiated with the
45 /// peer by calling sslConnect() or sslAccept(), and thereafter the interface
46 /// is half-duplex. If no TLS/SSL session is in effect ('raw') then the protocol
47 /// layer is transparent down to the socket.
48 ///
49 /// The interface has read-event and write-event handlers that should be
50 /// called when events are detected on the socket file descriptor. The read
51 /// handler delivers data via the onData() callback interface; the write
52 /// handler is used to flush the output pipeline.
53 ///
55 {
56 public:
57  typedef SocketProtocolSink Sink ;
58  G_EXCEPTION_CLASS( ReadError , "peer disconnected" ) ;
59  G_EXCEPTION( SendError , "peer disconnected" ) ;
60  G_EXCEPTION( SecureConnectionTimeout , "secure connection timeout" ) ;
61 
62  SocketProtocol( EventHandler & , Sink & , StreamSocket & , unsigned int secure_connection_timeout ) ;
63  ///< Constructor. The references are kept.
64 
65  ~SocketProtocol() ;
66  ///< Destructor.
67 
68  void readEvent() ;
69  ///< Called on receipt of a read event. Delivers data via the sink
70  ///< interface. Throws ReadError on error.
71 
72  bool writeEvent() ;
73  ///< Called on receipt of a write event. Sends more pending data
74  ///< down the connection. Returns true if all pending data was
75  ///< sent. Throws SendError on error.
76 
77  bool send( const std::string & data , size_t offset = 0U ) ;
78  ///< Sends data. Returns false if flow control asserted before
79  ///< all the data is sent. Returns true if all the data was sent,
80  ///< or if the data passed in (taking the offset into account)
81  ///< is empty. Throws SendError on error.
82  ///<
83  ///< If flow control is asserted then the socket write-event
84  ///< handler is installed; when the subsequent write-event is
85  ///< triggered the user should call writeEvent(). There should
86  ///< be no new calls to send() until writeEvent() returns true.
87 
88  bool send( const std::vector<std::pair<const char *,size_t> > & data ) ;
89  ///< Overload to send data using scatter-gather segments.
90  ///< If false is returned then segment data pointers must
91  ///< stay valid until writeEvent() returns true.
92 
93  static bool sslCapable() ;
94  ///< Returns true if the implementation supports TLS/SSL.
95 
96  void sslConnect() ;
97  ///< Initiates the TLS/SSL protocol.
98 
99  void sslAccept() ;
100  ///< Accepts the TLS/SSL protocol.
101 
102  bool sslEnabled() const ;
103  ///< Returns true if TLS/SSL is active.
104 
105  std::string peerCertificate() const ;
106  ///< Returns the peer's TLS/SSL certificate
107  ///< or the empty string.
108 
109 private:
110  SocketProtocol( const SocketProtocol & ) ;
111  void operator=( const SocketProtocol & ) ;
112 
113 private:
114  SocketProtocolImp * m_imp ;
115 } ;
116 
117 /// \class GNet::SocketProtocolSink
118 /// to deliver data from a socket.
119 ///
121 {
122 public:
123  virtual ~SocketProtocolSink() ;
124  ///< Destructor.
125 
126 protected:
127  friend class SocketProtocolImp ;
128 
129  virtual void onData( const char * , size_t ) = 0 ;
130  ///< Called when data is read from the socket.
131 
132  virtual void onSecure( const std::string & peer_certificate ) = 0 ;
133  ///< Called once the secure socket protocol has
134  ///< been successfully negotiated.
135 } ;
136 
137 #endif
void readEvent()
Called on receipt of a read event.
virtual void onData(const char *, size_t)=0
Called when data is read from the socket.
virtual ~SocketProtocolSink()
Destructor.
virtual void onSecure(const std::string &peer_certificate)=0
Called once the secure socket protocol has been successfully negotiated.
void sslAccept()
Accepts the TLS/SSL protocol.
A pimple-pattern implementation class used by GNet::SocketProtocol.
std::string peerCertificate() const
Returns the peer's TLS/SSL certificate or the empty string.
A derivation of GNet::Socket for a stream socket.
Definition: gsocket.h:245
~SocketProtocol()
Destructor.
bool send(const std::string &data, size_t offset=0U)
Sends data.
SocketProtocol(EventHandler &, Sink &, StreamSocket &, unsigned int secure_connection_timeout)
Constructor. The references are kept.
A base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:78
static bool sslCapable()
Returns true if the implementation supports TLS/SSL.
void sslConnect()
Initiates the TLS/SSL protocol.
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...
bool sslEnabled() const
Returns true if TLS/SSL is active.
bool writeEvent()
Called on receipt of a write event.