1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00
ULib/include/ulib/net/rpc/rpc.h
2015-01-23 17:24:36 +01:00

116 lines
3.2 KiB
C++

// ============================================================================
//
// = LIBRARY
// ULib - c++ library
//
// = FILENAME
// rpc.h - RPC utility
//
// = AUTHOR
// Stefano Casazza
//
// ============================================================================
#ifndef ULIB_RPC_H
#define ULIB_RPC_H 1
#include <ulib/utility/socket_ext.h>
#include <ulib/utility/string_ext.h>
// -----------------------------------------------------------------------------------------------------------------------------
// Very simple RPC-like layer
//
// Requests and responses are build of little packets each containing a U_TOKEN_NM-byte ascii token,
// an 8-byte hex value or length, and optionally data corresponding to the length.
// -----------------------------------------------------------------------------------------------------------------------------
class U_EXPORT URPC {
public:
// RPC representation
static UVector<UString>* rpc_info;
static void allocate()
{
U_TRACE(0, "URPC::allocate()")
U_INTERNAL_ASSERT_EQUALS(rpc_info,0)
rpc_info = U_NEW(UVector<UString>);
}
static void resetInfo()
{
U_TRACE(0, "URPC::resetInfo()")
U_INTERNAL_ASSERT_POINTER(rpc_info)
rpc_info->clear();
}
static bool readRequest(USocket* s);
// Read a token and value
static uint32_t readTokenInt(USocket* s, const char* token, UString& buffer, uint32_t& rstart);
// Read a token, and then the string data
static uint32_t readTokenString(USocket* s, const char* token, UString& buffer, uint32_t& rstart, UString& data);
// Read an vector of string from the network
static uint32_t readTokenVector(USocket* s, const char* token, UString& buffer, UVector<UString>& vec);
// Transmit token name (U_TOKEN_NM characters) and value (32-bit int, as 8 hex characters)
static bool sendTokenInt(USocket* s, const char* token, uint32_t value, UString& buffer)
{
U_TRACE(0, "URPC::sendTokenInt(%p,%S,%u,%.*S)", s, token, value, U_STRING_TO_TRACE(buffer))
UStringExt::buildTokenInt(token, value, buffer);
bool result = USocketExt::write(s, buffer, -1);
U_RETURN(result);
}
// Write a token, and then the string data
static bool sendTokenString(USocket* s, const char* token, const UString& data, UString& buffer)
{
U_TRACE(0, "URPC::sendTokenString(%p,%S,%.*S,%.*S)", s, token, U_STRING_TO_TRACE(data), U_STRING_TO_TRACE(buffer))
UStringExt::buildTokenString(token, data, buffer);
bool result = USocketExt::write(s, buffer, -1);
U_RETURN(result);
}
// Transmit an vector of string
static bool sendTokenVector(USocket* s, const char* token, UVector<UString>& vec, UString& buffer)
{
U_TRACE(0, "URPC::sendTokenVector(%p,%S,%p,%.*S)", s, token, &vec, U_STRING_TO_TRACE(buffer))
UStringExt::buildTokenVector(token, vec, buffer);
bool result = USocketExt::write(s, buffer, -1);
U_RETURN(result);
}
private:
#ifdef U_COMPILER_DELETE_MEMBERS
URPC(const URPC&) = delete;
URPC& operator=(const URPC&) = delete;
#else
URPC(const URPC&) {}
URPC& operator=(const URPC&) { return *this; }
#endif
};
#endif