Warning, file /sdk/dferry/serialization/message_p.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002    Copyright (C) 2014 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef MESSAGE_P_H
0025 #define MESSAGE_P_H
0026 
0027 #include "message.h"
0028 
0029 #include "arguments.h"
0030 #include "error.h"
0031 #include "itransportlistener.h"
0032 
0033 #include <type_traits>
0034 
0035 class ICompletionListener;
0036 
0037 class VarHeaderStorage {
0038 public:
0039     VarHeaderStorage();
0040     VarHeaderStorage(const VarHeaderStorage &other);
0041     ~VarHeaderStorage();
0042     VarHeaderStorage &operator=(const VarHeaderStorage &other);
0043 
0044     bool hasHeader(Message::VariableHeader header) const;
0045 
0046     bool hasStringHeader(Message::VariableHeader header) const;
0047     std::string stringHeader(Message::VariableHeader header) const;
0048     cstring stringHeaderRaw(Message::VariableHeader header);
0049     void setStringHeader(Message::VariableHeader header, const std::string &value);
0050     void clearStringHeader(Message::VariableHeader header);
0051 
0052     bool hasIntHeader(Message::VariableHeader header) const;
0053     uint32 intHeader(Message::VariableHeader header) const;
0054     void setIntHeader(Message::VariableHeader header, uint32 value);
0055     void clearIntHeader(Message::VariableHeader header);
0056 
0057     // for use during header deserialization: returns false if a header occurs twice,
0058     // but does not check if the given header is of the right type (int / string).
0059     bool setIntHeader_deser(Message::VariableHeader header, uint32 value);
0060     bool setStringHeader_deser(Message::VariableHeader header, cstring value);
0061 
0062     const std::string *stringHeaders() const
0063     {
0064         return reinterpret_cast<const std::string *>(m_stringStorage);
0065     }
0066     std::string *stringHeaders()
0067     {
0068         return reinterpret_cast<std::string *>(m_stringStorage);
0069     }
0070 
0071     static const int s_stringHeaderCount = 7;
0072     static const int s_intHeaderCount = 2;
0073 
0074     // Uninitialized storage for strings, to avoid con/destructing strings we'd never touch otherwise.
0075     std::aligned_storage<sizeof(std::string)>::type m_stringStorage[VarHeaderStorage::s_stringHeaderCount];
0076     uint32 m_intHeaders[s_intHeaderCount];
0077     uint32 m_headerPresenceBitmap = 0;
0078 };
0079 
0080 class MessagePrivate : public ITransportListener
0081 {
0082 public:
0083     static MessagePrivate *get(Message *m) { return m->d; }
0084 
0085     MessagePrivate(Message *parent);
0086     MessagePrivate(const MessagePrivate &other, Message *parent);
0087     ~MessagePrivate() override;
0088 
0089     IO::Status handleTransportCanRead() override;
0090     IO::Status handleTransportCanWrite() override;
0091 
0092     // ITransport is non-public API, so these make no sense in the public interface
0093     void receive(ITransport *transport); // fills in this message from transport
0094     void send(ITransport *transport); // sends this message over transport
0095     // for receive or send completion (it should be clear which because receiving and sending can't
0096     // happen simultaneously)
0097     void setCompletionListener(ICompletionListener *listener);
0098 
0099     bool requiredHeadersPresent();
0100     Error checkRequiredHeaders() const;
0101     bool deserializeFixedHeaders();
0102     bool deserializeVariableHeaders();
0103     bool serialize();
0104     void serializeFixedHeaders();
0105     Arguments serializeVariableHeaders();
0106 
0107     void clearBuffer();
0108     void clear(bool onlyReleaseResources = false);
0109     void reserveBuffer(uint32 newSize);
0110 
0111     void notifyCompletionListener();
0112 
0113     std::vector<int> *argUnixFds();
0114 
0115     Message *m_message;
0116     chunk m_buffer;
0117     uint32 m_bufferPos;
0118 
0119     bool m_isByteSwapped;
0120     enum { // ### we don't have an error state, the need hasn't arisen yet. strange!
0121         Empty = 0,
0122         Serialized, // This means that marshalled and "native format" data is in sync, which is really
0123                     // the same whether the message was marshalled or demarshalled
0124         FirstIoState,
0125         Sending = FirstIoState,
0126         Receiving
0127     } m_state;
0128     Message::Type m_messageType;
0129     enum {
0130         NoReplyExpectedFlag = 0x1,
0131         NoAutoStartServiceFlag = 0x2,
0132         NoAllowInteractiveAuthorizationFlag = 0x4
0133     };
0134     byte m_flags;
0135     byte m_protocolVersion;
0136     bool m_dirty : 1;
0137     uint32 m_headerLength;
0138     uint32 m_headerPadding;
0139     uint32 m_bodyLength;
0140     uint32 m_serial;
0141 
0142     Error m_error;
0143 
0144     Arguments m_mainArguments;
0145 
0146     VarHeaderStorage m_varHeaders;
0147 
0148     ICompletionListener *m_completionListener;
0149 };
0150 
0151 #endif // MESSAGE_P_H