| 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_TYPES_RPC_MESSAGE_H__ |
|---|
| 33 |
#define __NET_RPC_LIB_TYPES_RPC_MESSAGE_H__ |
|---|
| 34 |
|
|---|
| 35 |
#include <string> |
|---|
| 36 |
#include <whisperlib/common/base/types.h> |
|---|
| 37 |
#include <whisperlib/common/io/buffer/io_memory_stream.h> |
|---|
| 38 |
#include <whisperlib/net/rpc/lib/types/rpc_object.h> |
|---|
| 39 |
#include <whisperlib/net/rpc/lib/types/rpc_bool.h> |
|---|
| 40 |
#include <whisperlib/net/rpc/lib/types/rpc_object_simple.h> |
|---|
| 41 |
#include <whisperlib/net/rpc/lib/types/rpc_string.h> |
|---|
| 42 |
|
|---|
| 43 |
namespace rpc { |
|---|
| 44 |
|
|---|
| 45 |
enum MESSAGE_TYPE { |
|---|
| 46 |
RPC_CALL = 0, |
|---|
| 47 |
RPC_REPLY = 1, |
|---|
| 48 |
}; |
|---|
| 49 |
const char* MessageTypeName(int32 type); |
|---|
| 50 |
|
|---|
| 51 |
enum REPLY_STATUS { |
|---|
| 52 |
/////////////////////////////////////////////////////////////////// |
|---|
| 53 |
// |
|---|
| 54 |
// Server status codes |
|---|
| 55 |
// |
|---|
| 56 |
RPC_SUCCESS = 0, // RPC executed successfully. |
|---|
| 57 |
RPC_SERVICE_UNAVAIL = 1, // No such service. |
|---|
| 58 |
RPC_PROC_UNAVAIL = 2, // No such procedure in the given service. |
|---|
| 59 |
RPC_GARBAGE_ARGS = 3, // Illegal number of params |
|---|
| 60 |
// or wrong type for at least one param. |
|---|
| 61 |
RPC_SYSTEM_ERR = 4, // Unpredictable errors inside server, |
|---|
| 62 |
// like memory allocation failure. |
|---|
| 63 |
// Errors in remote method calls should be handled |
|---|
| 64 |
// and error codes should be returned as valid |
|---|
| 65 |
// rpc-types. (e.g. a method can return rpc::UInt32 |
|---|
| 66 |
// with value 0=success or (!0)=error code) |
|---|
| 67 |
RPC_SERVER_BUSY = 5, // The server has to many calls in execution. |
|---|
| 68 |
RPC_UNAUTHORIZED = 6, // The user is not authorized to perform the call |
|---|
| 69 |
/////////////////////////////////////////////////////////////////// |
|---|
| 70 |
// Client status codes |
|---|
| 71 |
RPC_QUERY_TIMEOUT = 101, // The transport layer did not receive |
|---|
| 72 |
// a response in due time. |
|---|
| 73 |
RPC_TOO_MANY_QUERIES = 102, // The transport layer has too many queries |
|---|
| 74 |
// pending for send and cannot accept |
|---|
| 75 |
// another one. |
|---|
| 76 |
RPC_CONN_CLOSED = 103, // The transport layer was closed while |
|---|
| 77 |
// waiting for reply. |
|---|
| 78 |
RPC_CONN_ERROR = 104, // The transport layer was unable to send |
|---|
| 79 |
// the query. |
|---|
| 80 |
}; |
|---|
| 81 |
const char* ReplyStatusName(int32 status); |
|---|
| 82 |
|
|---|
| 83 |
// TODO(cosmin): this (packet) mark has 4 bytes: "rpc\0", while the handshake |
|---|
| 84 |
// uses a 3 bytes mark: "rpc". Maybe unify convention? |
|---|
| 85 |
const char kMessageMark[4] = {'r', 'p', 'c', 0 }; |
|---|
| 86 |
|
|---|
| 87 |
struct Message : public Loggable { |
|---|
| 88 |
struct Header : public Loggable { |
|---|
| 89 |
// not needed here. The version is estabilished in initial handshake. |
|---|
| 90 |
// rpc::Int versionHi_; |
|---|
| 91 |
// rpc::Int versionLo_; |
|---|
| 92 |
|
|---|
| 93 |
rpc::Int xid_; // call ID. The response will have the same ID. |
|---|
| 94 |
rpc::Int msgType_; // CALL or REPLY |
|---|
| 95 |
bool operator==(const Header& h) const; |
|---|
| 96 |
string ToString() const; |
|---|
| 97 |
}; |
|---|
| 98 |
struct CallBody : public Loggable { |
|---|
| 99 |
rpc::String service_; |
|---|
| 100 |
rpc::String method_; |
|---|
| 101 |
io::MemoryStream params_; |
|---|
| 102 |
CallBody(); |
|---|
| 103 |
~CallBody(); |
|---|
| 104 |
bool operator==(const CallBody& c) const; |
|---|
| 105 |
string ToString() const; |
|---|
| 106 |
}; |
|---|
| 107 |
struct ReplyBody : public Loggable { |
|---|
| 108 |
rpc::Int replyStatus_; // call status |
|---|
| 109 |
io::MemoryStream result_; // return value |
|---|
| 110 |
ReplyBody(); |
|---|
| 111 |
~ReplyBody(); |
|---|
| 112 |
bool operator==(const ReplyBody& r) const; |
|---|
| 113 |
string ToString() const; |
|---|
| 114 |
}; |
|---|
| 115 |
|
|---|
| 116 |
Header header_; |
|---|
| 117 |
|
|---|
| 118 |
// These 2 below should be a union; only one of them must be used at a time. |
|---|
| 119 |
// But because every each of them contain attribs with constructors, |
|---|
| 120 |
// they cannot be part of an union. |
|---|
| 121 |
CallBody cbody_; |
|---|
| 122 |
ReplyBody rbody_; |
|---|
| 123 |
|
|---|
| 124 |
static const char* Name() { return "rpc_message"; } |
|---|
| 125 |
|
|---|
| 126 |
bool operator==(const Message& msg) const; |
|---|
| 127 |
|
|---|
| 128 |
string ToString() const; |
|---|
| 129 |
}; |
|---|
| 130 |
} |
|---|
| 131 |
|
|---|
| 132 |
#endif // __NET_RPC_LIB_TYPES_RPC_MESSAGE_H__ |
|---|