Add move constructor and assignment to SSLSocket

This commit is contained in:
rexy712 2021-06-25 11:42:36 -07:00
parent ddfbbe5584
commit c97280d190
3 changed files with 76 additions and 2 deletions

View File

@ -21,9 +21,9 @@ namespace fr
public: public:
explicit SSLSocket(std::shared_ptr<SSLContext> ssl_context) noexcept; explicit SSLSocket(std::shared_ptr<SSLContext> ssl_context) noexcept;
~SSLSocket() override; ~SSLSocket() override;
SSLSocket(SSLSocket &&) = delete; SSLSocket(SSLSocket &&);
SSLSocket(const SSLSocket &) = delete; SSLSocket(const SSLSocket &) = delete;
void operator=(SSLSocket &&)=delete; SSLSocket& operator=(SSLSocket &&);
void operator=(const SSLSocket &)=delete; void operator=(const SSLSocket &)=delete;
/*! /*!

52
include/frnetlib/Util.h Normal file
View File

@ -0,0 +1,52 @@
//
// Created by rexy712 on 06/25/2021.
//
#ifndef FRNETLIB_UTIL_H
#define FRNETLIB_UTIL_H
#include <mbedtls/ssl.h>
#include <cstdlib> //size_t
#include <cstring> //memcpy, memset
#include <utility> //move
namespace fr
{
template<class T>
constexpr void swap(T& a, T& b)
{
T tmp = std::move(a);
a = std::move(b);
b = std::move(tmp);
}
template<class T, size_t N>
constexpr void swap(T (&a)[N], T (&b)[N])
{
for(size_t i = 0;i < N;++i)
{
swap(a[i], b[i]);
}
}
constexpr mbedtls_ssl_config move_mbedtls_ssl_config(mbedtls_ssl_config &&src)
{
mbedtls_ssl_config retval = {};
swap(retval.ciphersuite_list, src.ciphersuite_list);
swap(retval.p_dbg, src.p_dbg);
swap(retval.f_get_cache, src.f_get_cache);
swap(retval.f_set_cache, src.f_set_cache);
swap(retval.p_cache, src.p_cache);
retval.max_major_ver = src.max_major_ver;
retval.max_minor_ver = src.max_minor_ver;
retval.min_major_ver = src.min_major_ver;
retval.min_minor_ver = src.min_minor_ver;
retval.endpoint = src.endpoint;
retval.transport = src.transport;
retval.authmode = src.authmode;
retval.allow_legacy_renegotiation = src.allow_legacy_renegotiation;
return retval;
}
}
#endif

View File

@ -3,6 +3,7 @@
// //
#include "frnetlib/SSLSocket.h" #include "frnetlib/SSLSocket.h"
#include "frnetlib/Util.h"
#include <memory> #include <memory>
#include <utility> #include <utility>
@ -58,6 +59,27 @@ namespace fr
mbedtls_ssl_config_free(&conf); mbedtls_ssl_config_free(&conf);
} }
SSLSocket::SSLSocket(SSLSocket &&s)
: ssl_context(std::move(s.ssl_context)),
ssl_socket_descriptor(std::move(s.ssl_socket_descriptor)),
ssl(std::move(s.ssl)),
conf(move_mbedtls_ssl_config(std::move(s.conf))),
should_verify(s.should_verify),
receive_timeout(s.receive_timeout),
is_blocking(s.is_blocking){}
SSLSocket& SSLSocket::operator=(SSLSocket &&s)
{
ssl_context = std::move(s.ssl_context);
ssl_socket_descriptor = std::move(s.ssl_socket_descriptor);
ssl = std::move(s.ssl);
conf = move_mbedtls_ssl_config(std::move(s.conf));
should_verify = s.should_verify;
receive_timeout = s.receive_timeout;
is_blocking = s.is_blocking;
return *this;
}
void SSLSocket::close_socket() void SSLSocket::close_socket()
{ {
ssl = nullptr; ssl = nullptr;