| 1 |
/*=========================================================================== |
| 2 |
FILE: |
| 3 |
ProtocolBuffer.h |
| 4 |
|
| 5 |
DESCRIPTION: |
| 6 |
Generic protocol structures and affliated methods |
| 7 |
|
| 8 |
PUBLIC CLASSES AND METHODS: |
| 9 |
sProtocolBuffer |
| 10 |
Simple struct to represent a protocol buffer using a reference counted |
| 11 |
(shared) buffer, this allows us to use in in several places without |
| 12 |
copying it once in each place. A few base services are provided |
| 13 |
but the main purpose is to provide a class to inherit off of for |
| 14 |
specific protocols |
| 15 |
|
| 16 |
Copyright (c) 2012, Code Aurora Forum. All rights reserved. |
| 17 |
|
| 18 |
Redistribution and use in source and binary forms, with or without |
| 19 |
modification, are permitted provided that the following conditions are met: |
| 20 |
* Redistributions of source code must retain the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer. |
| 22 |
* Redistributions in binary form must reproduce the above copyright |
| 23 |
notice, this list of conditions and the following disclaimer in the |
| 24 |
documentation and/or other materials provided with the distribution. |
| 25 |
* Neither the name of Code Aurora Forum nor |
| 26 |
the names of its contributors may be used to endorse or promote |
| 27 |
products derived from this software without specific prior written |
| 28 |
permission. |
| 29 |
|
| 30 |
|
| 31 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 32 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 33 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 34 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 35 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 36 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 37 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 38 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 39 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 40 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 41 |
POSSIBILITY OF SUCH DAMAGE. |
| 42 |
===========================================================================*/ |
| 43 |
|
| 44 |
//--------------------------------------------------------------------------- |
| 45 |
// Pragmas |
| 46 |
//--------------------------------------------------------------------------- |
| 47 |
#pragma once |
| 48 |
|
| 49 |
//--------------------------------------------------------------------------- |
| 50 |
// Include Files |
| 51 |
//--------------------------------------------------------------------------- |
| 52 |
#include "SharedBuffer.h" |
| 53 |
#include "ProtocolEnum.h" |
| 54 |
|
| 55 |
static const tm EMPTY_TIME = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 56 |
|
| 57 |
/*=========================================================================*/ |
| 58 |
// Struct sProtocolBuffer |
| 59 |
/*=========================================================================*/ |
| 60 |
struct sProtocolBuffer |
| 61 |
{ |
| 62 |
public: |
| 63 |
// Constructor (default) |
| 64 |
sProtocolBuffer(); |
| 65 |
|
| 66 |
// Constructor (parameterized) |
| 67 |
sProtocolBuffer( sSharedBuffer * pBuffer ); |
| 68 |
|
| 69 |
// Copy constructor |
| 70 |
sProtocolBuffer( const sProtocolBuffer & copyThis ); |
| 71 |
|
| 72 |
// Assignment operator |
| 73 |
sProtocolBuffer & operator = ( const sProtocolBuffer & copyThis ); |
| 74 |
|
| 75 |
// Destructor |
| 76 |
virtual ~sProtocolBuffer(); |
| 77 |
|
| 78 |
// (Inline) Get buffer |
| 79 |
const BYTE * GetBuffer() const |
| 80 |
{ |
| 81 |
BYTE * pRet = 0; |
| 82 |
if (IsValid() == true) |
| 83 |
{ |
| 84 |
pRet = (BYTE *)mpData->GetBuffer(); |
| 85 |
} |
| 86 |
|
| 87 |
return (const BYTE *)pRet; |
| 88 |
}; |
| 89 |
|
| 90 |
// (Inline) Get buffer size |
| 91 |
ULONG GetSize() const |
| 92 |
{ |
| 93 |
ULONG size = 0; |
| 94 |
if (IsValid() == true) |
| 95 |
{ |
| 96 |
size = mpData->GetSize(); |
| 97 |
} |
| 98 |
|
| 99 |
return size; |
| 100 |
}; |
| 101 |
|
| 102 |
// (Inline) Return the protocol type |
| 103 |
eProtocolType GetType() const |
| 104 |
{ |
| 105 |
eProtocolType pt = ePROTOCOL_ENUM_BEGIN; |
| 106 |
if (IsValid() == true) |
| 107 |
{ |
| 108 |
pt = (eProtocolType)mpData->GetType(); |
| 109 |
} |
| 110 |
|
| 111 |
return pt; |
| 112 |
}; |
| 113 |
|
| 114 |
// (Inline) Return the shared buffer |
| 115 |
sSharedBuffer * GetSharedBuffer() const |
| 116 |
{ |
| 117 |
sSharedBuffer * pRet = 0; |
| 118 |
if (IsValid() == true) |
| 119 |
{ |
| 120 |
pRet = mpData; |
| 121 |
} |
| 122 |
|
| 123 |
return pRet; |
| 124 |
}; |
| 125 |
|
| 126 |
// (Inline) Return the timestamp |
| 127 |
tm GetTimestamp() const |
| 128 |
{ |
| 129 |
tm ft = EMPTY_TIME; |
| 130 |
|
| 131 |
if (IsValid() == true) |
| 132 |
{ |
| 133 |
ft = mTimestamp; |
| 134 |
} |
| 135 |
|
| 136 |
return ft; |
| 137 |
}; |
| 138 |
|
| 139 |
// (Inline) Is this buffer valid? |
| 140 |
virtual bool IsValid() const |
| 141 |
{ |
| 142 |
return mbValid; |
| 143 |
}; |
| 144 |
|
| 145 |
protected: |
| 146 |
// (Inline) Validate buffer |
| 147 |
virtual bool Validate() |
| 148 |
{ |
| 149 |
// Do we have a shared buffer and is it valid? |
| 150 |
mbValid = (mpData != 0 && mpData->IsValid()); |
| 151 |
return mbValid; |
| 152 |
}; |
| 153 |
|
| 154 |
/* Our data buffer */ |
| 155 |
sSharedBuffer * mpData; |
| 156 |
|
| 157 |
/* Time buffer was created */ |
| 158 |
tm mTimestamp; |
| 159 |
|
| 160 |
/* Has this buffer been validated? (NOTE: *NOT* set in base) */ |
| 161 |
bool mbValid; |
| 162 |
}; |