Commit b79beb48 authored by chiaming2000's avatar chiaming2000 Committed by GitHub
Browse files

Merge pull request #41 from plensing/ssl_initialization

Only initialize SSL once.
parents ad398d5a f353b2e3
Loading
Loading
Loading
Loading
+17 −9
Original line number Diff line number Diff line
@@ -32,18 +32,26 @@ namespace kinetic {

using std::string;

SocketWrapper::SocketWrapper(const std::string& host, int port, bool use_ssl, bool nonblocking)
        : ctx_(NULL), ssl_(NULL), host_(host), port_(port), nonblocking_(nonblocking), fd_(-1) {
    if(!use_ssl) return;

class OpenSSLInitializer{
public:
    OpenSSLInitializer() {
      SSL_library_init();
      OpenSSL_add_all_algorithms();
    }
};
static OpenSSLInitializer init;

SocketWrapper::SocketWrapper(const std::string& host, int port, bool use_ssl, bool nonblocking)
        : ctx_(NULL), ssl_(NULL), host_(host), port_(port), nonblocking_(nonblocking), fd_(-1) {
    if(use_ssl) {
        ctx_ = SSL_CTX_new(SSLv23_client_method());
        ssl_ = SSL_new(ctx_);
    if(!ssl_ || !ctx_)
        if(!ssl_ || !ctx_) {
            throw std::runtime_error("Failed Setting up SSL environment.");
        }
        SSL_set_mode(ssl_, SSL_MODE_AUTO_RETRY);
    }
}

SocketWrapper::~SocketWrapper() {
    if (fd_ == -1) {