TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SslSocket.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 
25 using boost::asio::ip::tcp;
26 namespace boostssl = boost::asio::ssl;
27 
28 template<class SslContext>
29 class SslSocket
30 {
31 public:
32  explicit SslSocket(tcp::socket&& socket) : _socket(std::move(socket)), _sslSocket(_socket, SslContext::instance())
33  {
34  _sslSocket.set_verify_mode(boostssl::verify_none);
35  }
36 
37  // adapting tcp::socket api
38  void close(boost::system::error_code& error)
39  {
40  _socket.close(error);
41  }
42 
43  void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code& shutdownError)
44  {
45  _sslSocket.shutdown(shutdownError);
46  _socket.shutdown(what, shutdownError);
47  }
48 
49  template<typename MutableBufferSequence, typename ReadHandlerType>
50  void async_read_some(MutableBufferSequence const& buffers, ReadHandlerType&& handler)
51  {
52  _sslSocket.async_read_some(buffers, std::move(handler));
53  }
54 
55  template<typename ConstBufferSequence, typename WriteHandlerType>
56  void async_write_some(ConstBufferSequence const& buffers, WriteHandlerType&& handler)
57  {
58  _sslSocket.async_write_some(buffers, std::move(handler));
59  }
60 
61  template<typename ConstBufferSequence>
62  std::size_t write_some(ConstBufferSequence const& buffers, boost::system::error_code& error)
63  {
64  return _sslSocket.write_some(buffers, error);
65  }
66 
67  template<typename SettableSocketOption>
68  void set_option(SettableSocketOption const& option, boost::system::error_code& error)
69  {
70  _socket.set_option(option, error);
71  }
72 
73  tcp::socket::endpoint_type remote_endpoint() const
74  {
75  return _socket.remote_endpoint();
76  }
77 
78  // ssl api
79  template<typename HandshakeHandlerType>
80  void async_handshake(boostssl::stream_base::handshake_type type, HandshakeHandlerType&& handler)
81  {
82  _sslSocket.async_handshake(type, std::move(handler));
83  }
84 
85 private:
86  tcp::socket _socket;
87  boostssl::stream<tcp::socket&> _sslSocket;
88 };
89 
90 #endif // SslSocket_h__
void close(boost::system::error_code &error)
Definition: SslSocket.h:38
void async_handshake(boostssl::stream_base::handshake_type type, HandshakeHandlerType &&handler)
Definition: SslSocket.h:80
void set_option(SettableSocketOption const &option, boost::system::error_code &error)
Definition: SslSocket.h:68
Definition: SslSocket.h:29
boostssl::stream< tcp::socket & > _sslSocket
Definition: SslSocket.h:87
SslSocket(tcp::socket &&socket)
Definition: SslSocket.h:32
STL namespace.
void async_write_some(ConstBufferSequence const &buffers, WriteHandlerType &&handler)
Definition: SslSocket.h:56
void async_read_some(MutableBufferSequence const &buffers, ReadHandlerType &&handler)
Definition: SslSocket.h:50
tcp::socket _socket
Definition: SslSocket.h:86
tcp::socket::endpoint_type remote_endpoint() const
Definition: SslSocket.h:73
std::size_t write_some(ConstBufferSequence const &buffers, boost::system::error_code &error)
Definition: SslSocket.h:62
void shutdown(boost::asio::socket_base::shutdown_type what, boost::system::error_code &shutdownError)
Definition: SslSocket.h:43