From: Zofia Abramowska Date: Wed, 12 Nov 2014 15:36:52 +0000 (+0100) Subject: Remove dangerous reference X-Git-Tag: submit/R4/20141115.054144~26 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cb00a4e9ea320a3f74cb28b06e30bb7b5e43d613;p=platform%2Fcore%2Fsecurity%2Fcynara.git Remove dangerous reference RequestContext contained reference to an external BinaryQueue. One problem was, BBQ was held inside vector (so practically any operation on vector made this object out-of-date), second problem was, RequestContext was passed to other classes inside shared_ptr, so owner of this bbq looses control other its reference. Moreover, soon RequestContext will be held pending (e.g. when waiting for external Agent to return answer) inside cynara logic, so BBQ stored inside RequestContext needs to be alive as long as corresponding connection is opened. Not more, not less. Change-Id: I79c9eb9b5e74927bd7bb159da01fae23612ca83e --- diff --git a/src/client-async/sockets/SocketClientAsync.cpp b/src/client-async/sockets/SocketClientAsync.cpp index e3ffbd4..148b13a 100644 --- a/src/client-async/sockets/SocketClientAsync.cpp +++ b/src/client-async/sockets/SocketClientAsync.cpp @@ -30,6 +30,8 @@ namespace Cynara { SocketClientAsync::SocketClientAsync(const std::string &socketPath, ProtocolPtr protocol) : m_socket(socketPath, 0), m_protocol(protocol) { + m_readQueue = std::make_shared(); + m_writeQueue = std::make_shared(); } Socket::ConnectionStatus SocketClientAsync::connect(void) { @@ -54,15 +56,15 @@ void SocketClientAsync::appendRequest(RequestPtr request) { } bool SocketClientAsync::isDataToSend(void) { - return m_socket.isDataToSend() || !m_writeQueue.empty(); + return m_socket.isDataToSend() || !m_writeQueue->empty(); } Socket::SendStatus SocketClientAsync::sendToCynara(void) { - return m_socket.sendToServer(m_writeQueue); + return m_socket.sendToServer(*m_writeQueue); } bool SocketClientAsync::receiveFromCynara(void) { - return m_socket.receiveFromServer(m_readQueue); + return m_socket.receiveFromServer(*m_readQueue); } ResponsePtr SocketClientAsync::getResponse(void) { diff --git a/src/client-async/sockets/SocketClientAsync.h b/src/client-async/sockets/SocketClientAsync.h index 5fb0543..eaacf84 100644 --- a/src/client-async/sockets/SocketClientAsync.h +++ b/src/client-async/sockets/SocketClientAsync.h @@ -56,8 +56,8 @@ public: private: Socket m_socket; ProtocolPtr m_protocol; - BinaryQueue m_readQueue; - BinaryQueue m_writeQueue; + BinaryQueuePtr m_readQueue; + BinaryQueuePtr m_writeQueue; }; } // namespace Cynara diff --git a/src/common/containers/BinaryQueue.h b/src/common/containers/BinaryQueue.h index f69f786..12b2e8a 100644 --- a/src/common/containers/BinaryQueue.h +++ b/src/common/containers/BinaryQueue.h @@ -33,52 +33,20 @@ namespace Cynara { */ class BinaryQueue; typedef std::shared_ptr BinaryQueuePtr; +typedef std::weak_ptr BinaryQueueWeakPtr; /** * Binary stream implemented as constant size bucket list * * @todo Add optimized implementation for FlattenConsume */ -class BinaryQueue -{ - public: +class BinaryQueue { +public: typedef void (*BufferDeleter)(const void *buffer, size_t bufferSize, void *userParam); static void bufferDeleterFree(const void *buffer, size_t bufferSize, void *userParam); - - private: - struct Bucket - { - const void *buffer; - const void *ptr; - size_t size; - size_t left; - - BufferDeleter deleter; - void *param; - - Bucket(const void *buffer, - size_t bufferSize, - BufferDeleter deleter, - void *userParam); - ~Bucket(); - // make it noncopyable - Bucket(const Bucket &) = delete; - const Bucket &operator=(const Bucket &) = delete; - // make it nonmoveable - Bucket(Bucket &&) = delete; - Bucket &operator=(Bucket &&) = delete; - }; - - typedef std::list BucketList; - BucketList m_buckets; - size_t m_size; - - static void deleteBucket(Bucket *bucket); - - public: /** * Construct empty binary queue */ @@ -239,6 +207,35 @@ class BinaryQueue * is larger than available bytes in binary queue */ void flattenConsume(void *buffer, size_t bufferSize); + +private: + struct Bucket { + const void *buffer; + const void *ptr; + size_t size; + size_t left; + + BufferDeleter deleter; + void *param; + + Bucket(const void *buffer, + size_t bufferSize, + BufferDeleter deleter, + void *userParam); + ~Bucket(); + // make it noncopyable + Bucket(const Bucket &) = delete; + const Bucket &operator=(const Bucket &) = delete; + // make it nonmoveable + Bucket(Bucket &&) = delete; + Bucket &operator=(Bucket &&) = delete; + }; + + typedef std::list BucketList; + BucketList m_buckets; + size_t m_size; + + static void deleteBucket(Bucket *bucket); }; } // namespace Cynara diff --git a/src/common/exceptions/ContextErrorException.h b/src/common/exceptions/ContextErrorException.h new file mode 100644 index 0000000..771065a --- /dev/null +++ b/src/common/exceptions/ContextErrorException.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @file src/common/exceptions/ContextErrorException.h + * @author Zofia Abramowska + * @version 1.0 + * @brief Implementation of ContextErrorException.h + */ + +#ifndef SRC_COMMON_EXCEPTIONS_CONTEXTERROREXCEPTION_H_ +#define SRC_COMMON_EXCEPTIONS_CONTEXTERROREXCEPTION_H_ + +#include "Exception.h" + +namespace Cynara { + +class ContextErrorException : public Exception { +public: + ContextErrorException() = default; + virtual ~ContextErrorException() {}; + + virtual const std::string message(void) const { + return "ContextErrorException"; + } +}; + +} /* namespace Cynara */ + +#endif /* SRC_COMMON_EXCEPTIONS_CONTEXTERROREXCEPTION_H_ */ diff --git a/src/common/protocol/Protocol.h b/src/common/protocol/Protocol.h index b98f145..63ee24d 100644 --- a/src/common/protocol/Protocol.h +++ b/src/common/protocol/Protocol.h @@ -44,8 +44,8 @@ public: virtual ProtocolPtr clone(void) = 0; - virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue) = 0; - virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue) = 0; + virtual RequestPtr extractRequestFromBuffer(BinaryQueuePtr bufferQueue) = 0; + virtual ResponsePtr extractResponseFromBuffer(BinaryQueuePtr bufferQueue) = 0; ProtocolFrameHeader &frameHeader(void) { return m_frameHeader; diff --git a/src/common/protocol/ProtocolAdmin.cpp b/src/common/protocol/ProtocolAdmin.cpp index 2d4c879..8d792f9 100644 --- a/src/common/protocol/ProtocolAdmin.cpp +++ b/src/common/protocol/ProtocolAdmin.cpp @@ -149,7 +149,7 @@ RequestPtr ProtocolAdmin::deserializeSetPoliciesRequest(void) { m_frameHeader.sequenceNumber()); } -RequestPtr ProtocolAdmin::extractRequestFromBuffer(BinaryQueue &bufferQueue) { +RequestPtr ProtocolAdmin::extractRequestFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); if (m_frameHeader.isFrameComplete()) { @@ -201,7 +201,7 @@ ResponsePtr ProtocolAdmin::deserializeCodeResponse(void) { m_frameHeader.sequenceNumber()); } -ResponsePtr ProtocolAdmin::extractResponseFromBuffer(BinaryQueue &bufferQueue) { +ResponsePtr ProtocolAdmin::extractResponseFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); if (m_frameHeader.isFrameComplete()) { @@ -239,7 +239,7 @@ void ProtocolAdmin::execute(RequestContextPtr context, AdminCheckRequestPtr requ ProtocolSerialization::serialize(frame, request->startBucket()); ProtocolSerialization::serialize(frame, request->recursive()); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolAdmin::execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request) { @@ -255,7 +255,7 @@ void ProtocolAdmin::execute(RequestContextPtr context, InsertOrUpdateBucketReque ProtocolSerialization::serialize(frame, request->result().policyType()); ProtocolSerialization::serialize(frame, request->result().metadata()); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolAdmin::execute(RequestContextPtr context, RemoveBucketRequestPtr request) { @@ -267,7 +267,7 @@ void ProtocolAdmin::execute(RequestContextPtr context, RemoveBucketRequestPtr re ProtocolSerialization::serialize(frame, OpRemoveBucket); ProtocolSerialization::serialize(frame, request->bucketId()); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolAdmin::execute(RequestContextPtr context, SetPoliciesRequestPtr request) { @@ -311,7 +311,7 @@ void ProtocolAdmin::execute(RequestContextPtr context, SetPoliciesRequestPtr req } } - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolAdmin::execute(RequestContextPtr context, CheckResponsePtr response) { @@ -327,7 +327,7 @@ void ProtocolAdmin::execute(RequestContextPtr context, CheckResponsePtr response ProtocolSerialization::serialize(frame, response->m_resultRef.policyType()); ProtocolSerialization::serialize(frame, response->m_resultRef.metadata()); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolAdmin::execute(RequestContextPtr context, CodeResponsePtr response) { @@ -340,7 +340,7 @@ void ProtocolAdmin::execute(RequestContextPtr context, CodeResponsePtr response) ProtocolSerialization::serialize(frame, OpCodeResponse); ProtocolSerialization::serialize(frame, static_cast(response->m_code)); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } } // namespace Cynara diff --git a/src/common/protocol/ProtocolAdmin.h b/src/common/protocol/ProtocolAdmin.h index 3f7fcc9..9d2c6ac 100644 --- a/src/common/protocol/ProtocolAdmin.h +++ b/src/common/protocol/ProtocolAdmin.h @@ -35,8 +35,8 @@ public: virtual ProtocolPtr clone(void); - virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue); - virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue); + virtual RequestPtr extractRequestFromBuffer(BinaryQueuePtr bufferQueue); + virtual ResponsePtr extractResponseFromBuffer(BinaryQueuePtr bufferQueue); virtual void execute(RequestContextPtr context, AdminCheckRequestPtr request); virtual void execute(RequestContextPtr context, InsertOrUpdateBucketRequestPtr request); diff --git a/src/common/protocol/ProtocolClient.cpp b/src/common/protocol/ProtocolClient.cpp index c130809..3f13653 100644 --- a/src/common/protocol/ProtocolClient.cpp +++ b/src/common/protocol/ProtocolClient.cpp @@ -73,7 +73,7 @@ RequestPtr ProtocolClient::deserializeCheckRequest(void) { m_frameHeader.sequenceNumber()); } -RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueue &bufferQueue) { +RequestPtr ProtocolClient::extractRequestFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); if (m_frameHeader.isFrameComplete()) { @@ -117,7 +117,7 @@ ResponsePtr ProtocolClient::deserializeCheckResponse(void) { return std::make_shared(policyResult, m_frameHeader.sequenceNumber()); } -ResponsePtr ProtocolClient::extractResponseFromBuffer(BinaryQueue &bufferQueue) { +ResponsePtr ProtocolClient::extractResponseFromBuffer(BinaryQueuePtr bufferQueue) { ProtocolFrameSerializer::deserializeHeader(m_frameHeader, bufferQueue); if (m_frameHeader.isFrameComplete()) { @@ -147,7 +147,7 @@ void ProtocolClient::execute(RequestContextPtr context, CancelRequestPtr request ProtocolSerialization::serialize(frame, OpCancelRequest); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolClient::execute(RequestContextPtr context, CheckRequestPtr request) { @@ -162,7 +162,7 @@ void ProtocolClient::execute(RequestContextPtr context, CheckRequestPtr request) ProtocolSerialization::serialize(frame, request->key().user().value()); ProtocolSerialization::serialize(frame, request->key().privilege().value()); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolClient::execute(RequestContextPtr context, CancelResponsePtr response) { @@ -173,7 +173,7 @@ void ProtocolClient::execute(RequestContextPtr context, CancelResponsePtr respon ProtocolSerialization::serialize(frame, OpCancelResponse); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } void ProtocolClient::execute(RequestContextPtr context, CheckResponsePtr response) { @@ -188,7 +188,7 @@ void ProtocolClient::execute(RequestContextPtr context, CheckResponsePtr respons ProtocolSerialization::serialize(frame, response->m_resultRef.policyType()); ProtocolSerialization::serialize(frame, response->m_resultRef.metadata()); - ProtocolFrameSerializer::finishSerialization(frame, context->responseQueue()); + ProtocolFrameSerializer::finishSerialization(frame, *(context->responseQueue())); } } // namespace Cynara diff --git a/src/common/protocol/ProtocolClient.h b/src/common/protocol/ProtocolClient.h index dc72e7a..a1c5110 100644 --- a/src/common/protocol/ProtocolClient.h +++ b/src/common/protocol/ProtocolClient.h @@ -38,8 +38,8 @@ public: virtual ProtocolPtr clone(void); - virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue); - virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue); + virtual RequestPtr extractRequestFromBuffer(BinaryQueuePtr bufferQueue); + virtual ResponsePtr extractResponseFromBuffer(BinaryQueuePtr bufferQueue); virtual void execute(RequestContextPtr context, CancelRequestPtr request); virtual void execute(RequestContextPtr context, CheckRequestPtr request); diff --git a/src/common/protocol/ProtocolFrameSerializer.cpp b/src/common/protocol/ProtocolFrameSerializer.cpp index 0599f02..68c497c 100644 --- a/src/common/protocol/ProtocolFrameSerializer.cpp +++ b/src/common/protocol/ProtocolFrameSerializer.cpp @@ -31,15 +31,15 @@ namespace Cynara { void ProtocolFrameSerializer::deserializeHeader(ProtocolFrameHeader &frameHeader, - BinaryQueue &data) { + BinaryQueuePtr data) { if (!frameHeader.isHeaderComplete()) { - if ((data.size() < ProtocolFrameHeader::frameHeaderLength())) { + if ((data->size() < ProtocolFrameHeader::frameHeaderLength())) { return; } LOGD("Deserializing frameHeader"); - frameHeader.setHeaderContent(BinaryQueuePtr(&data, [=] (BinaryQueue *) {})); + frameHeader.setHeaderContent(data); ProtocolFrameSignature signature; ProtocolDeserialization::deserialize(frameHeader, frameHeader.m_signature.length(), @@ -60,7 +60,7 @@ void ProtocolFrameSerializer::deserializeHeader(ProtocolFrameHeader &frameHeader frameHeader.setHeaderComplete(); } - if (data.size() >= (frameHeader.frameLength() - ProtocolFrameHeader::frameHeaderLength())) { + if (data->size() >= (frameHeader.frameLength() - ProtocolFrameHeader::frameHeaderLength())) { frameHeader.setBodyComplete(); } } diff --git a/src/common/protocol/ProtocolFrameSerializer.h b/src/common/protocol/ProtocolFrameSerializer.h index f2e201e..d7d6c15 100644 --- a/src/common/protocol/ProtocolFrameSerializer.h +++ b/src/common/protocol/ProtocolFrameSerializer.h @@ -33,7 +33,7 @@ namespace Cynara { class ProtocolFrameSerializer { public: - static void deserializeHeader(ProtocolFrameHeader &frameHeader, BinaryQueue &data); + static void deserializeHeader(ProtocolFrameHeader &frameHeader, BinaryQueuePtr data); static ProtocolFrame startSerialization(ProtocolFrameSequenceNumber sequenceNumber); static void finishSerialization(ProtocolFrame &frame, BinaryQueue &data); }; diff --git a/src/common/protocol/ProtocolSignal.cpp b/src/common/protocol/ProtocolSignal.cpp index 963ee9a..d04dbbe 100644 --- a/src/common/protocol/ProtocolSignal.cpp +++ b/src/common/protocol/ProtocolSignal.cpp @@ -46,17 +46,17 @@ ProtocolPtr ProtocolSignal::clone(void) { return std::make_shared(); } -RequestPtr ProtocolSignal::extractRequestFromBuffer(BinaryQueue &bufferQueue) { - if (bufferQueue.size() >= sizeof(struct signalfd_siginfo)) { +RequestPtr ProtocolSignal::extractRequestFromBuffer(BinaryQueuePtr bufferQueue) { + if (bufferQueue->size() >= sizeof(struct signalfd_siginfo)) { struct signalfd_siginfo sigInfo; - bufferQueue.flattenConsume(&sigInfo, sizeof(sigInfo)); + bufferQueue->flattenConsume(&sigInfo, sizeof(sigInfo)); return std::make_shared(sigInfo); } return nullptr; } -ResponsePtr ProtocolSignal::extractResponseFromBuffer(BinaryQueue &bufferQueue UNUSED) { +ResponsePtr ProtocolSignal::extractResponseFromBuffer(BinaryQueuePtr bufferQueue UNUSED) { throw NotImplementedException(); } diff --git a/src/common/protocol/ProtocolSignal.h b/src/common/protocol/ProtocolSignal.h index e2142da..6c544c4 100644 --- a/src/common/protocol/ProtocolSignal.h +++ b/src/common/protocol/ProtocolSignal.h @@ -36,8 +36,8 @@ public: virtual ProtocolPtr clone(void); - virtual RequestPtr extractRequestFromBuffer(BinaryQueue &bufferQueue); - virtual ResponsePtr extractResponseFromBuffer(BinaryQueue &bufferQueue); + virtual RequestPtr extractRequestFromBuffer(BinaryQueuePtr bufferQueue); + virtual ResponsePtr extractResponseFromBuffer(BinaryQueuePtr bufferQueue); virtual void execute(RequestContextPtr context, SignalRequestPtr request); }; diff --git a/src/common/request/RequestContext.h b/src/common/request/RequestContext.h index 33b6ef4..9c98bef 100644 --- a/src/common/request/RequestContext.h +++ b/src/common/request/RequestContext.h @@ -26,6 +26,7 @@ #include #include +#include #include #include #include @@ -36,10 +37,10 @@ namespace Cynara { class RequestContext { private: ResponseTakerWeakPtr m_responseTaker; - BinaryQueue &m_responseQueue; + BinaryQueueWeakPtr m_responseQueue; public: - RequestContext(ResponseTakerPtr responseTaker, BinaryQueue &responseQueue) + RequestContext(ResponseTakerPtr responseTaker, BinaryQueuePtr responseQueue) : m_responseTaker(responseTaker), m_responseQueue(responseQueue) { } @@ -49,8 +50,11 @@ public: response->execute(response, taker, self); } - BinaryQueue &responseQueue(void) const { - return m_responseQueue; + BinaryQueuePtr responseQueue(void) const { + auto bbqPtr = m_responseQueue.lock(); + if (bbqPtr) + return bbqPtr; + throw ContextErrorException(); } }; diff --git a/src/common/sockets/SocketClient.cpp b/src/common/sockets/SocketClient.cpp index 6db91d6..b3ce8ad 100644 --- a/src/common/sockets/SocketClient.cpp +++ b/src/common/sockets/SocketClient.cpp @@ -37,6 +37,8 @@ namespace Cynara { SocketClient::SocketClient(const std::string &socketPath, ProtocolPtr protocol) : m_socket(socketPath), m_protocol(protocol) { + m_writeQueue = std::make_shared(); + m_readQueue = std::make_shared(); } bool SocketClient::connect(void) { @@ -64,14 +66,14 @@ ResponsePtr SocketClient::askCynaraServer(RequestPtr request) { request->execute(request, m_protocol, context); //send request to cynara - if (m_socket.sendToServer(m_writeQueue) == Socket::SendStatus::CONNECTION_LOST) { + if (m_socket.sendToServer(*m_writeQueue) == Socket::SendStatus::CONNECTION_LOST) { LOGW("Disconnected while sending request to Cynara."); return nullptr; } // receive response from cynara while (true) { - if (!m_socket.receiveFromServer(m_readQueue)) { + if (!m_socket.receiveFromServer(*m_readQueue)) { LOGW("Disconnected while receiving response from Cynara."); return nullptr; } diff --git a/src/common/sockets/SocketClient.h b/src/common/sockets/SocketClient.h index 9f9737f..c313b13 100644 --- a/src/common/sockets/SocketClient.h +++ b/src/common/sockets/SocketClient.h @@ -41,8 +41,8 @@ class SocketClient { private: Socket m_socket; ProtocolPtr m_protocol; - BinaryQueue m_readQueue; - BinaryQueue m_writeQueue; + BinaryQueuePtr m_readQueue; + BinaryQueuePtr m_writeQueue; public: SocketClient(const std::string &socketPath, ProtocolPtr protocol); diff --git a/src/service/sockets/Descriptor.cpp b/src/service/sockets/Descriptor.cpp index 0e55e4b..b2e9459 100644 --- a/src/service/sockets/Descriptor.cpp +++ b/src/service/sockets/Descriptor.cpp @@ -27,8 +27,17 @@ namespace Cynara { Descriptor::Descriptor() : m_listen(false), m_used(false), m_client(false), m_protocol(nullptr) { } +void Descriptor::checkQueues(void) { + if (!m_writeQueue) + m_writeQueue = std::make_shared(); + if (!m_readQueue) + m_readQueue = std::make_shared(); +} + bool Descriptor::hasDataToWrite(void) const { - return !(m_writeQueue.empty() && m_writeBuffer.empty()); + if (m_writeQueue) + return !(m_writeQueue->empty() && m_writeBuffer.empty()); + return false; } ResponseTakerPtr Descriptor::responseTaker(void) const { @@ -36,19 +45,22 @@ ResponseTakerPtr Descriptor::responseTaker(void) const { } void Descriptor::pushReadBuffer(const RawBuffer &readbuffer) { - m_readQueue.appendCopy(readbuffer.data(), readbuffer.size()); + checkQueues(); + m_readQueue->appendCopy(readbuffer.data(), readbuffer.size()); } RequestPtr Descriptor::extractRequest(void) { + checkQueues(); return m_protocol->extractRequestFromBuffer(m_readQueue); } RawBuffer &Descriptor::prepareWriteBuffer(void) { - size_t queuedDataSize = m_writeQueue.size(); + checkQueues(); + size_t queuedDataSize = m_writeQueue->size(); size_t bufferDataSize = m_writeBuffer.size(); m_writeBuffer.resize(queuedDataSize + bufferDataSize); - m_writeQueue.flattenConsume(m_writeBuffer.data() + bufferDataSize, queuedDataSize); + m_writeQueue->flattenConsume(m_writeBuffer.data() + bufferDataSize, queuedDataSize); return m_writeBuffer; } @@ -57,8 +69,8 @@ void Descriptor::clear(void) { m_listen = false; m_used = false; m_client = false; - m_readQueue.clear(); - m_writeQueue.clear(); + m_readQueue.reset(); + m_writeQueue.reset(); m_writeBuffer.clear(); m_protocol.reset(); } diff --git a/src/service/sockets/Descriptor.h b/src/service/sockets/Descriptor.h index 0f42a36..5d77028 100644 --- a/src/service/sockets/Descriptor.h +++ b/src/service/sockets/Descriptor.h @@ -34,17 +34,6 @@ namespace Cynara { class Descriptor { -private: - bool m_listen; - bool m_used; - bool m_client; - - BinaryQueue m_readQueue; - BinaryQueue m_writeQueue; - RawBuffer m_writeBuffer; - - ProtocolPtr m_protocol; - public: Descriptor(); @@ -68,7 +57,7 @@ public: ResponseTakerPtr responseTaker(void) const; - BinaryQueue &writeQueue(void) { + BinaryQueuePtr writeQueue(void) { return m_writeQueue; } @@ -94,6 +83,19 @@ public: RawBuffer &prepareWriteBuffer(void); void clear(void); + +private: + bool m_listen; + bool m_used; + bool m_client; + + BinaryQueuePtr m_readQueue; + BinaryQueuePtr m_writeQueue; + RawBuffer m_writeBuffer; + + ProtocolPtr m_protocol; + + void checkQueues(void); }; } // namespace Cynara