TrinityCore
SslSocket.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef SslSocket_h__
19#define SslSocket_h__
20
21#include <boost/asio/ip/tcp.hpp>
22#include <boost/asio/ssl/stream.hpp>
23#include <boost/system/error_code.hpp>
24
25namespace boostssl = boost::asio::ssl;
26
27template<class Stream = boostssl::stream<boost::asio::ip::tcp::socket>>
29{
30public:
31 explicit SslSocket(boost::asio::ip::tcp::socket&& socket, boost::asio::ssl::context& sslContext) : _sslSocket(std::move(socket), sslContext)
32 {
33 _sslSocket.set_verify_mode(boostssl::verify_none);
34 }
35
36 // adapting tcp::socket api
37 void close(boost::system::error_code& error)
38 {
39 _sslSocket.lowest_layer().close(error);
40 }
41
42 void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
43 {
44 _sslSocket.shutdown(shutdownError);
45 _sslSocket.lowest_layer().shutdown(what, shutdownError);
46 }
47
48 template<typename MutableBufferSequence, typename ReadHandlerType>
49 void async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler)
50 {
51 _sslSocket.async_read_some(buffers, std::move(handler));
52 }
53
54 template<typename ConstBufferSequence, typename WriteHandlerType>
55 void async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler)
56 {
57 _sslSocket.async_write_some(buffers, std::move(handler));
58 }
59
60 template<typename ConstBufferSequence>
61 std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error)
62 {
63 return _sslSocket.write_some(buffers, error);
64 }
65
66 template<typename SettableSocketOption>
67 void set_option(SettableSocketOption const& option, boost::system::error_code& error)
68 {
69 _sslSocket.lowest_layer().set_option(option, error);
70 }
71
72 boost::asio::ip::tcp::socket::endpoint_type remote_endpoint() const
73 {
74 return _sslSocket.lowest_layer().remote_endpoint();
75 }
76
77 // ssl api
78 template<typename HandshakeHandlerType>
79 void async_handshake(boostssl::stream_base::handshake_type type, HandshakeHandlerType&& handler)
80 {
81 _sslSocket.async_handshake(type, std::move(handler));
82 }
83
84protected:
85 Stream _sslSocket;
86};
87
88#endif // SslSocket_h__
boost::asio::ip::tcp::socket::endpoint_type remote_endpoint() const
Definition: SslSocket.h:72
Stream _sslSocket
Definition: SslSocket.h:85
SslSocket(boost::asio::ip::tcp::socket &&socket, boost::asio::ssl::context &sslContext)
Definition: SslSocket.h:31
void close(boost::system::error_code &error)
Definition: SslSocket.h:37
void async_handshake(boostssl::stream_base::handshake_type type, HandshakeHandlerType &&handler)
Definition: SslSocket.h:79
void async_write_some(ConstBufferSequence const &buffers, WriteHandlerType &&handler)
Definition: SslSocket.h:55
void set_option(SettableSocketOption const &option, boost::system::error_code &error)
Definition: SslSocket.h:67
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code &shutdownError)
Definition: SslSocket.h:42
std::size_t write_some(ConstBufferSequence const &buffers, boost::system::error_code &error)
Definition: SslSocket.h:61
void async_read_some(MutableBufferSequence const &buffers, ReadHandlerType &&handler)
Definition: SslSocket.h:49
STL namespace.