| 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 |
#include "common/base/errno.h" |
|---|
| 33 |
#include "common/base/log.h" |
|---|
| 34 |
#include "net/rpc/lib/codec/rpc_decoder.h" |
|---|
| 35 |
#include "net/rpc/lib/codec/rpc_typename_codec.h" |
|---|
| 36 |
#include "net/rpc/lib/types/rpc_message.h" |
|---|
| 37 |
|
|---|
| 38 |
namespace rpc { |
|---|
| 39 |
|
|---|
| 40 |
DECODE_RESULT Decoder::DecodePacket(Message& out) { |
|---|
| 41 |
// Decode message header |
|---|
| 42 |
char mark[sizeof(kMessageMark)] = { 0, }; |
|---|
| 43 |
if ( in_.Read(mark, sizeof(mark)) < sizeof(mark) ) { |
|---|
| 44 |
return DECODE_RESULT_NOT_ENOUGH_DATA; |
|---|
| 45 |
} |
|---|
| 46 |
if ( memcmp(mark, kMessageMark, sizeof(mark)) ) { |
|---|
| 47 |
LOG_ERROR << "RPC bad header marker. Found: " |
|---|
| 48 |
<< strutil::PrintableDataBuffer((const uint8*)mark, sizeof(mark)); |
|---|
| 49 |
return DECODE_RESULT_ERROR; |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
Message::Header& header = out.header_; |
|---|
| 53 |
DECODE_RESULT result = Decode(header.xid_); |
|---|
| 54 |
if ( result != DECODE_RESULT_SUCCESS ) { |
|---|
| 55 |
return result; // error on Decode or not enough data |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
result = Decode(header.msgType_); |
|---|
| 59 |
if ( result != DECODE_RESULT_SUCCESS ) { |
|---|
| 60 |
return result; // error on Decode or not enough data |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
Int body_size; |
|---|
| 64 |
result = Decode(body_size); |
|---|
| 65 |
if ( result != DECODE_RESULT_SUCCESS ) { |
|---|
| 66 |
return result; // error on Decode or not enough data |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
if ( in_.Size() < body_size.Get() ) { |
|---|
| 70 |
return DECODE_RESULT_NOT_ENOUGH_DATA; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
// Decode RPC message body |
|---|
| 74 |
if ( header.msgType_ == RPC_CALL ) { |
|---|
| 75 |
const uint32 r1 = in_.Size(); // bytes at body begin |
|---|
| 76 |
Message::CallBody& call = out.cbody_; |
|---|
| 77 |
result = Decode(call.service_); |
|---|
| 78 |
if ( result != DECODE_RESULT_SUCCESS ) { |
|---|
| 79 |
return result; |
|---|
| 80 |
} |
|---|
| 81 |
result = Decode(call.method_); |
|---|
| 82 |
if ( result != DECODE_RESULT_SUCCESS ) { |
|---|
| 83 |
return result; |
|---|
| 84 |
} |
|---|
| 85 |
const uint32 r2 = in_.Size(); // bytes at params begin |
|---|
| 86 |
|
|---|
| 87 |
DCHECK_LT(r2, r1); |
|---|
| 88 |
// r1 - r2 = service + method length |
|---|
| 89 |
const uint32 params_size = body_size - (r1 - r2); |
|---|
| 90 |
DCHECK_LE(params_size, r2); |
|---|
| 91 |
|
|---|
| 92 |
call.params_.Clear(); |
|---|
| 93 |
call.params_.AppendStream(&in_, params_size); |
|---|
| 94 |
} else if ( header.msgType_ == RPC_REPLY ) { |
|---|
| 95 |
const uint32 r1 = in_.Size(); // bytes at body begin |
|---|
| 96 |
Message::ReplyBody & reply = out.rbody_; |
|---|
| 97 |
|
|---|
| 98 |
result = Decode(reply.replyStatus_); |
|---|
| 99 |
if ( result != DECODE_RESULT_SUCCESS ) { |
|---|
| 100 |
return result; |
|---|
| 101 |
} |
|---|
| 102 |
const uint32 r2 = in_.Size(); // bytes at result begin |
|---|
| 103 |
|
|---|
| 104 |
DCHECK_LT(r2, r1); |
|---|
| 105 |
// r1 - r2 = replyStatus length |
|---|
| 106 |
uint32 result_size = body_size - (r1 - r2); |
|---|
| 107 |
DCHECK_LE(result_size, r2); |
|---|
| 108 |
|
|---|
| 109 |
reply.result_.Clear(); |
|---|
| 110 |
reply.result_.AppendStream(&in_, result_size); |
|---|
| 111 |
} else { |
|---|
| 112 |
LOG_ERROR << "RPC Bad message type: " << header.msgType_; |
|---|
| 113 |
return DECODE_RESULT_ERROR; |
|---|
| 114 |
} |
|---|
| 115 |
return DECODE_RESULT_SUCCESS; |
|---|
| 116 |
} |
|---|
| 117 |
} |
|---|