From 704bf55df5fae51bf0e8d86679ee7f34e22553d2 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Sun, 9 Aug 2015 08:16:50 +0300 Subject: client-qt: socket wraper to work with both ssl and plain connection server: socket wraper to work with both ssl and plain connection bit of config related changes ssl support (untested) --- server/include/socket_wraper.h | 105 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 server/include/socket_wraper.h (limited to 'server/include/socket_wraper.h') diff --git a/server/include/socket_wraper.h b/server/include/socket_wraper.h new file mode 100644 index 0000000..ed929ca --- /dev/null +++ b/server/include/socket_wraper.h @@ -0,0 +1,105 @@ +/* + 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 . + +*/ + +#ifndef SOCKET_H_INCLUDED +#define SOCKET_H_INCLUDED + +#include +#include + +class socket_wraper +{ +public: + socket_wraper(boost::asio::ssl::stream *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 *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& get_ssl_socket() + { + return *socket_ssl_; + } + template std::size_t read_some(const MutableBufferSequence & buffers) + { + if(is_ssl) + return socket_ssl_->read_some(buffers); + else + return socket_->read_some(buffers); + } + template 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 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 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_; + boost::asio::ssl::stream *socket_ssl_; + +}; + + +#endif -- cgit v1.2.3