IOStream.h
1 /*
2  * Copyright (C) 2013 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_SSL_IOSTREAM_H
30 #define PT_SSL_IOSTREAM_H
31 
32 #include <Pt/Ssl/Api.h>
33 #include <Pt/Ssl/StreamBuffer.h>
34 #include <Pt/NonCopyable.h>
35 #include <Pt/IOStream.h>
36 
37 namespace Pt {
38 
39 namespace Ssl {
40 
43 class IOStream : public BasicIOStream<char>
44  , private NonCopyable
45 {
46  public:
49  IOStream(std::size_t bufferSize = 1024)
50  : BasicIOStream<char>(0)
51  , _sb(bufferSize)
52  {
53  setBuffer(&_sb);
54  }
55 
58  IOStream(Context& ctx, std::iostream& ios, OpenMode mode, std::size_t bufferSize = 1024)
59  : BasicIOStream<char>(0)
60  , _sb(ctx, ios, mode, bufferSize)
61  {
62  setBuffer(&_sb);
63  }
64 
67  virtual ~IOStream()
68  {}
69 
72  void open(Context& ctx, std::iostream& ios, OpenMode mode)
73  { _sb.open( ctx, ios, mode); }
74 
77  const char* currentCipher() const
78  { return _sb.currentCipher(); }
79 
82  void close()
83  { _sb.close(); }
84 
87  bool isConnected() const
88  { return _sb.isConnected(); }
89 
95  { return _sb.writeHandshake(); }
96 
103  { return _sb.readHandshake(); }
104 
112  bool shutdown()
113  { return _sb.shutdown(); }
114 
117  bool isShutdown() const
118  { return _sb.isShutdown(); }
119 
122  bool isClosed() const
123  { return _sb.isClosed(); }
124 
130  std::streamsize import(std::streamsize maxImport = 0)
131  { return _sb.import(maxImport); }
132 
136  { return _sb; }
137 
138  private:
139  StreamBuffer _sb;
140 };
141 
142 } // namespace Ssl
143 
144 } // namespace Pt
145 
146 #endif // PT_SSL_IOSTREAM_H
void close()
Closes the stream buffer.
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:175
SSL stream.
Definition: IOStream.h:43
bool readHandshake()
Reads handshake message from the underlying stream.
Definition: IOStream.h:102
Protects derived classes from being copied.
Definition: NonCopyable.h:54
bool isConnected() const
Returns true if connected to peer.
Definition: IOStream.h:87
SSL stream buffer.
Definition: StreamBuffer.h:47
bool isShutdown() const
Returns true if the shutown notify has to be completed.
Definition: IOStream.h:117
bool shutdown()
Shutdown the SSL connection.
Definition: IOStream.h:112
IOStream(std::size_t bufferSize=1024)
Construct an SSL stream.
Definition: IOStream.h:49
void open(Context &ctx, std::ios &ios, OpenMode mode)
Opens the stream buffer.
bool isClosed() const
Returns true if the connection is closed.
Definition: IOStream.h:122
void close()
Closes the stream.
Definition: IOStream.h:82
bool shutdown()
Shutdown the SSL connection.
bool isClosed() const
Returns true if the connection is closed.
Input/Output stream.
Definition: IOStream.h:141
Context for SSL connections.
Definition: Context.h:76
virtual ~IOStream()
Destructor.
Definition: IOStream.h:67
std::streamsize import(std::streamsize maxImport=0)
Reads user message from the underlying stream.
void open(Context &ctx, std::iostream &ios, OpenMode mode)
Opens the SSL stream.
Definition: IOStream.h:72
const char * currentCipher() const
Return the currently used cipher.
bool writeHandshake()
Writes a handshake message to the underlying stream.
bool writeHandshake()
Writes a handshake message to the underlying stream.
Definition: IOStream.h:94
bool isConnected() const
Returns true if connected to peer.
StreamBuffer & sslBuffer()
Returns the ssl buffer.
Definition: IOStream.h:135
bool isShutdown() const
Returns true if the shutown notify has to be completed.
OpenMode
Open mode for ssl I/O.
Definition: Context.h:52
IOStream(Context &ctx, std::iostream &ios, OpenMode mode, std::size_t bufferSize=1024)
Constructs an open SSL stream.
Definition: IOStream.h:58
const char * currentCipher() const
Returns the currently used cipher.
Definition: IOStream.h:77
bool readHandshake(std::streamsize maxRead=0)
Reads handshake message from the underlying stream.