blob: 3f5e847b5fe4f3c35e0f8f5d5d15bcc2ec95407f [file] [log] [blame]
Kevin Rocardef8d7272014-04-23 20:34:14 +02001/*
David Wagnerb76c9d62014-02-05 18:30:24 +01002 * Copyright (c) 2011-2014, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Patrick Benavoli68a91282011-08-31 11:23:23 +020029 */
30#pragma once
31
32#include <stdint.h>
33#include <string>
34
Patrick Benavoli68a91282011-08-31 11:23:23 +020035class CSocket;
36
37class CMessage
38{
39public:
40 CMessage(uint8_t ucMsgId);
41 CMessage();
42 virtual ~CMessage();
43
Kevin Rocarde874c252014-04-25 15:11:12 +020044 enum Result {
45 success,
46 peerDisconnected,
47 error
48 };
49
Kevin Rocardef8d7272014-04-23 20:34:14 +020050 /** Write or read the message on pSocket.
51 *
52 * @param[in,out] pSocket is the socket on wich IO operation will be made.
53 * @param[in] bOut if true message should be read,
54 * if false it should be written.
55 * @param[out] strError on failure, a string explaining the error,
56 * on success, undefined.
57 *
Kevin Rocarde874c252014-04-25 15:11:12 +020058 * @return success if a correct message could be recv/send
59 * peerDisconnected if the peer disconnected before the first socket access.
60 * error if the message could not be read/write for any other reason
Kevin Rocardef8d7272014-04-23 20:34:14 +020061 */
Kevin Rocarde874c252014-04-25 15:11:12 +020062 Result serialize(CSocket* pSocket, bool bOut, std::string &strError);
Patrick Benavoli68a91282011-08-31 11:23:23 +020063
64protected:
65 // Msg Id
66 uint8_t getMsgId() const;
Patrick Benavoli911844b2014-07-23 01:27:00 +020067
68 /** Write raw data to the message
69 *
70 * @param[in] pvData pointer to the data array
71 * @param[in] uiSize array size in bytes
72 */
73 void writeData(const void* pvData, size_t uiSize);
74
75 /** Read raw data from the message
76 *
77 * @param[out] pvData pointer to the data array
78 * @param[in] uiSize array size in bytes
79 */
80 void readData(void* pvData, size_t uiSize);
81
82 /** Write string to the message
83 *
84 * @param[in] strData the string to write
85 */
Sebastien Gonzalved9526492014-02-20 22:28:03 +010086 void writeString(const std::string& strData);
Patrick Benavoli911844b2014-07-23 01:27:00 +020087
88 /** Write string to the message
89 *
90 * @param[out] strData the string to read to
91 */
Sebastien Gonzalved9526492014-02-20 22:28:03 +010092 void readString(std::string& strData);
Patrick Benavoli911844b2014-07-23 01:27:00 +020093
94 /** @return string length plus room to store its length
95 *
96 * @param[in] strData the string to get the size from
97 */
98 size_t getStringSize(const std::string& strData) const;
99
100 /** @return remaining data size to read or to write depending on the context
101 * (request: write, answer: read)
102 */
103 size_t getRemainingDataSize() const;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200104private:
JhinX Lee4ebc0982012-07-12 17:50:07 +0200105 CMessage(const CMessage&);
106 CMessage& operator=(const CMessage&);
Patrick Benavoli911844b2014-07-23 01:27:00 +0200107
108 /** Allocate room to store the message
109 *
110 * @param[int] uiDataSize the szie to allocate in bytes
111 */
112 void allocateData(size_t uiDataSize);
Patrick Benavoli68a91282011-08-31 11:23:23 +0200113 // Fill data to send
114 virtual void fillDataToSend() = 0;
115 // Collect received data
116 virtual void collectReceivedData() = 0;
Patrick Benavoli911844b2014-07-23 01:27:00 +0200117
118 /** @return size of the transaction data in bytes
119 */
120 virtual size_t getDataSize() const = 0;
121
Patrick Benavoli68a91282011-08-31 11:23:23 +0200122 // Checksum
123 uint8_t computeChecksum() const;
124
125 // MsgId
126 uint8_t _ucMsgId;
127 // Data
128 uint8_t* _pucData;
Patrick Benavoli911844b2014-07-23 01:27:00 +0200129 /** Size of the allocated memory to store the message */
130 size_t _uiDataSize;
131 /** Read/Write Index used to iterate across the message data */
132 size_t _uiIndex;
Patrick Benavoli68a91282011-08-31 11:23:23 +0200133};