1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
Copyright © 2015 Gluzskiy Alexandr (sss)
This file is part of Unknown Download Manager (UDM).
UDM is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
UDM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UDM. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOCKET_H_INCLUDED
#define SOCKET_H_INCLUDED
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
class socket_wraper
{
public:
socket_wraper(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>* s)
: is_ssl(true)
{
socket_ssl_ = s;
}
socket_wraper(boost::asio::ip::tcp::socket* s)
: is_ssl(false)
{
socket_ = s;
}
void operator=(boost::asio::ssl::stream<boost::asio::ip::tcp::socket>* s)
{
socket_ssl_ = s;
is_ssl = true;
}
void operator=(boost::asio::ip::tcp::socket* s)
{
socket_ = s;
is_ssl = false;
}
boost::asio::ip::tcp::socket& get_socket()
{
return *socket_;
}
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>& get_ssl_socket()
{
return *socket_ssl_;
}
template <typename MutableBufferSequence> std::size_t read_some(const MutableBufferSequence& buffers)
{
if(is_ssl)
return socket_ssl_->read_some(buffers);
else
return socket_->read_some(buffers);
}
template <typename MutableBufferSequence>
std::size_t read_some(const MutableBufferSequence& buffers, boost::system::error_code& ec)
{
if(is_ssl)
return socket_ssl_->read_some(buffers, ec);
else
return socket_->read_some(buffers, ec);
}
template <typename MutableBufferSequence, typename ReadHandler>
void async_read_some(const MutableBufferSequence& buffers, ReadHandler handler)
{
if(is_ssl)
socket_ssl_->async_read_some(buffers, handler);
else
socket_->async_read_some(buffers, handler);
}
template <typename ConstBufferSequence, typename WriteHandler>
void async_write_some(const ConstBufferSequence& buffers, WriteHandler handler)
{
if(is_ssl)
socket_ssl_->async_write_some(buffers, handler);
else
socket_->async_write_some(buffers, handler);
}
~socket_wraper()
{
if(is_ssl)
delete socket_ssl_;
else
delete socket_;
}
private:
bool is_ssl;
boost::asio::ip::tcp::socket* socket_ = nullptr;
boost::asio::ssl::stream<boost::asio::ip::tcp::socket>* socket_ssl_ = nullptr;
};
#endif
|