| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Cosmin Tudorache |
|---|
| 31 |
|
|---|
| 32 |
#ifndef __NET_RPC_LIB_CODEC_RPC_TYPENAME_CODEC_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_CODEC_RPC_TYPENAME_CODEC_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string.h> |
|---|
| 36 |
#include <whisperlib/common/base/log.h> |
|---|
| 37 |
#include <whisperlib/common/io/num_streaming.h> |
|---|
| 38 |
|
|---|
| 39 |
// when this is defined we encode type along with every object. |
|---|
| 40 |
// Provides type check. |
|---|
| 41 |
// else: does not encode type. Faster. |
|---|
| 42 |
#undef __PERFORM_RPC_TYPENAME_ENCODING__ |
|---|
| 43 |
|
|---|
| 44 |
#if defined(__PERFORM_RPC_TYPENAME_ENCODING__) && defined(_DEBUG) |
|---|
| 45 |
# define RPC_TYPENAME_ENCODE(szTypename) \ |
|---|
| 46 |
do { \ |
|---|
| 47 |
const uint32 size = ::strlen(szTypename); \ |
|---|
| 48 |
CHECK_LT(size, 256); \ |
|---|
| 49 |
io::IONumStreamer::WriteByte(out_, 0x0A); \ |
|---|
| 50 |
io::IONumStreamer::WriteByte(out_, 0x0B); \ |
|---|
| 51 |
io::IONumStreamer::WriteByte(out_, 0x0C); \ |
|---|
| 52 |
io::IONumStreamer::WriteByte(out_, size); \ |
|---|
| 53 |
out_->Write(szTypename, size); \ |
|---|
| 54 |
} while ( false ) |
|---|
| 55 |
|
|---|
| 56 |
// TODO(cpopescu): Attention: this look buggy !!! |
|---|
| 57 |
# define RPC_TYPENAME_DECODE_AND_CHECK(szExpectedTypename) \ |
|---|
| 58 |
do { \ |
|---|
| 59 |
char szTypename[256] = { 0, }; \ |
|---|
| 60 |
uint32 available = in_.Readable(); \ |
|---|
| 61 |
if ( available < 4 ) { \ |
|---|
| 62 |
return 0; \ |
|---|
| 63 |
} \ |
|---|
| 64 |
if ( io::IONumStreamer::ReadByte(&in_) != 0x0A || \ |
|---|
| 65 |
io::IONumStreamer::ReadByte(&in_) != 0x0B || \ |
|---|
| 66 |
io::IONumStreamer::ReadByte(&in_) != 0x0C ) { \ |
|---|
| 67 |
LOG_ERROR << "Typename check failure. Wrong mark."; \ |
|---|
| 68 |
return -1; \ |
|---|
| 69 |
} \ |
|---|
| 70 |
const uint32 nSize = io::IONumStreamer::ReadByte(&in_); \ |
|---|
| 71 |
available -= 4; \ |
|---|
| 72 |
if ( available < nSize ) { \ |
|---|
| 73 |
return 0; \ |
|---|
| 74 |
} \ |
|---|
| 75 |
in_.Read(szTypename, nSize); \ |
|---|
| 76 |
if ( 0 != strcmp(szTypename, szExpectedTypename) ) { \ |
|---|
| 77 |
LOG_ERROR << "Typename check failure. Found: '" \ |
|---|
| 78 |
<< szTypename << "' expected: '" \ |
|---|
| 79 |
<< szExpectedTypename << "'"; \ |
|---|
| 80 |
return -1; \ |
|---|
| 81 |
} \ |
|---|
| 82 |
} |
|---|
| 83 |
#else |
|---|
| 84 |
# define RPC_TYPENAME_ENCODE(szTypename) |
|---|
| 85 |
# define RPC_TYPENAME_DECODE_AND_CHECK(szExpectedTypename) |
|---|
| 86 |
#endif // defined(__PERFORM_RPC_TYPENAME_ENCODING__) && defined(_DEBUG) |
|---|
| 87 |
|
|---|
| 88 |
#endif // __NET_RPC_LIB_CODEC_RPC_TYPENAME_CODEC_H__ |
|---|