diff options
Diffstat (limited to 'Swiften/Session/BasicSessionStream.cpp')
-rw-r--r-- | Swiften/Session/BasicSessionStream.cpp | 49 |
1 files changed, 23 insertions, 26 deletions
diff --git a/Swiften/Session/BasicSessionStream.cpp b/Swiften/Session/BasicSessionStream.cpp index 10c6ad0..c44961d 100644 --- a/Swiften/Session/BasicSessionStream.cpp +++ b/Swiften/Session/BasicSessionStream.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ @@ -34,11 +34,8 @@ BasicSessionStream::BasicSessionStream( connection(connection), tlsContextFactory(tlsContextFactory), timerFactory(timerFactory), - compressionLayer(nullptr), - tlsLayer(nullptr), - whitespacePingLayer(nullptr), tlsOptions_(tlsOptions) { - xmppLayer = new XMPPLayer(payloadParserFactories, payloadSerializers, xmlParserFactory, streamType); + auto xmppLayer = std::make_unique<XMPPLayer>(payloadParserFactories, payloadSerializers, xmlParserFactory, streamType); xmppLayer->onStreamStart.connect(boost::bind(&BasicSessionStream::handleStreamStartReceived, this, _1)); xmppLayer->onStreamEnd.connect(boost::bind(&BasicSessionStream::handleStreamEndReceived, this)); xmppLayer->onElement.connect(boost::bind(&BasicSessionStream::handleElementReceived, this, _1)); @@ -47,53 +44,50 @@ BasicSessionStream::BasicSessionStream( xmppLayer->onWriteData.connect(boost::bind(&BasicSessionStream::handleDataWritten, this, _1)); connection->onDisconnected.connect(boost::bind(&BasicSessionStream::handleConnectionFinished, this, _1)); - connectionLayer = new ConnectionLayer(connection); - - streamStack = new StreamStack(xmppLayer, connectionLayer); + streamStack = std::make_unique<StreamStack>(std::move(xmppLayer), std::unique_ptr<ConnectionLayer>(new ConnectionLayer(connection))); available = true; } BasicSessionStream::~BasicSessionStream() { - delete compressionLayer; - if (tlsLayer) { + if (auto tlsLayer = streamStack->getLayer<TLSLayer>()) { tlsLayer->onError.disconnect(boost::bind(&BasicSessionStream::handleTLSError, this, _1)); tlsLayer->onConnected.disconnect(boost::bind(&BasicSessionStream::handleTLSConnected, this)); - delete tlsLayer; } - delete whitespacePingLayer; - delete streamStack; connection->onDisconnected.disconnect(boost::bind(&BasicSessionStream::handleConnectionFinished, this, _1)); - delete connectionLayer; + auto xmppLayer = streamStack->getLayer<XMPPLayer>(); xmppLayer->onStreamStart.disconnect(boost::bind(&BasicSessionStream::handleStreamStartReceived, this, _1)); xmppLayer->onStreamEnd.disconnect(boost::bind(&BasicSessionStream::handleStreamEndReceived, this)); xmppLayer->onElement.disconnect(boost::bind(&BasicSessionStream::handleElementReceived, this, _1)); xmppLayer->onError.disconnect(boost::bind(&BasicSessionStream::handleXMPPError, this)); xmppLayer->onDataRead.disconnect(boost::bind(&BasicSessionStream::handleDataRead, this, _1)); xmppLayer->onWriteData.disconnect(boost::bind(&BasicSessionStream::handleDataWritten, this, _1)); - delete xmppLayer; } void BasicSessionStream::writeHeader(const ProtocolHeader& header) { assert(available); + auto* xmppLayer = streamStack->getLayer<XMPPLayer>(); xmppLayer->writeHeader(header); } void BasicSessionStream::writeElement(std::shared_ptr<ToplevelElement> element) { assert(available); + auto* xmppLayer = streamStack->getLayer<XMPPLayer>(); xmppLayer->writeElement(element); } void BasicSessionStream::writeFooter() { assert(available); + auto* xmppLayer = streamStack->getLayer<XMPPLayer>(); xmppLayer->writeFooter(); } void BasicSessionStream::writeData(const std::string& data) { assert(available); + auto* xmppLayer = streamStack->getLayer<XMPPLayer>(); xmppLayer->writeData(data); } @@ -111,12 +105,14 @@ bool BasicSessionStream::supportsTLSEncryption() { void BasicSessionStream::addTLSEncryption() { assert(available); - tlsLayer = new TLSLayer(tlsContextFactory, tlsOptions_); + auto tlsContext = tlsContextFactory->createTLSContext(tlsOptions_); + auto tlsLayer = std::make_unique<TLSLayer>(std::move(tlsContext)); if (hasTLSCertificate() && !tlsLayer->setClientCertificate(getTLSCertificate())) { onClosed(std::make_shared<SessionStreamError>(SessionStreamError::InvalidTLSCertificateError)); } else { - streamStack->addLayer(tlsLayer); + streamStack->addLayer(std::move(tlsLayer)); + auto tlsLayer = streamStack->getLayer<TLSLayer>(); tlsLayer->onError.connect(boost::bind(&BasicSessionStream::handleTLSError, this, _1)); tlsLayer->onConnected.connect(boost::bind(&BasicSessionStream::handleTLSConnected, this)); tlsLayer->connect(); @@ -124,23 +120,23 @@ void BasicSessionStream::addTLSEncryption() { } bool BasicSessionStream::isTLSEncrypted() { - return tlsLayer; + return streamStack->getLayer<TLSLayer>(); } Certificate::ref BasicSessionStream::getPeerCertificate() const { - return tlsLayer->getPeerCertificate(); + return streamStack->getLayer<TLSLayer>()->getPeerCertificate(); } std::vector<Certificate::ref> BasicSessionStream::getPeerCertificateChain() const { - return tlsLayer->getPeerCertificateChain(); + return streamStack->getLayer<TLSLayer>()->getPeerCertificateChain(); } std::shared_ptr<CertificateVerificationError> BasicSessionStream::getPeerCertificateVerificationError() const { - return tlsLayer->getPeerCertificateVerificationError(); + return streamStack->getLayer<TLSLayer>()->getPeerCertificateVerificationError(); } ByteArray BasicSessionStream::getTLSFinishMessage() const { - return tlsLayer->getContext()->getFinishMessage(); + return streamStack->getLayer<TLSLayer>()->getContext()->getFinishMessage(); } bool BasicSessionStream::supportsZLibCompression() { @@ -148,15 +144,15 @@ bool BasicSessionStream::supportsZLibCompression() { } void BasicSessionStream::addZLibCompression() { - compressionLayer = new CompressionLayer(); - streamStack->addLayer(compressionLayer); + streamStack->addLayer(std::make_unique<CompressionLayer>()); } void BasicSessionStream::setWhitespacePingEnabled(bool enabled) { + auto whitespacePingLayer = streamStack->getLayer<WhitespacePingLayer>(); if (enabled) { if (!whitespacePingLayer) { - whitespacePingLayer = new WhitespacePingLayer(timerFactory); - streamStack->addLayer(whitespacePingLayer); + streamStack->addLayer(std::make_unique<WhitespacePingLayer>(timerFactory)); + whitespacePingLayer = streamStack->getLayer<WhitespacePingLayer>(); } whitespacePingLayer->setActive(); } @@ -166,6 +162,7 @@ void BasicSessionStream::setWhitespacePingEnabled(bool enabled) { } void BasicSessionStream::resetXMPPParser() { + auto* xmppLayer = streamStack->getLayer<XMPPLayer>(); xmppLayer->resetParser(); } |