270 lines
5.6 KiB
C++
270 lines
5.6 KiB
C++
|
#include <time.h>
|
||
|
|
||
|
#include <checks.h>
|
||
|
#include <isamrpc.h>
|
||
|
|
||
|
#define NO_MFC
|
||
|
#define CObject TObject
|
||
|
#define CString TString
|
||
|
#define CStringArray TString_array
|
||
|
#include <netsock.h>
|
||
|
|
||
|
static TSocketClient* _client = NULL;
|
||
|
|
||
|
static DWORD _connection = 0;
|
||
|
|
||
|
bool rpc_Start()
|
||
|
{
|
||
|
bool ok = TRUE;
|
||
|
if (_client == NULL)
|
||
|
{
|
||
|
_client = new TSocketClient;
|
||
|
if (!_client->IsOk())
|
||
|
{
|
||
|
delete _client;
|
||
|
_client = NULL;
|
||
|
ok = error_box("Errore di inizializzazione del client.");
|
||
|
}
|
||
|
}
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
bool rpc_Stop()
|
||
|
{
|
||
|
if (_client)
|
||
|
{
|
||
|
delete _client;
|
||
|
_client = NULL;
|
||
|
_connection = 0;
|
||
|
}
|
||
|
return TRUE;
|
||
|
}
|
||
|
|
||
|
bool rpc_Call(const char* cmd)
|
||
|
{
|
||
|
CHECK(_connection, "Server not connected");
|
||
|
bool ok = _client->Execute(_connection, cmd);
|
||
|
return ok;
|
||
|
}
|
||
|
|
||
|
char* rpc_Request(const char* cmd, DWORD& size, real& total)
|
||
|
{
|
||
|
CHECK(_connection, "Server not connected");
|
||
|
const clock_t start = clock();
|
||
|
_client->Request(_connection, cmd);
|
||
|
total = (clock() - start) / double(CLOCKS_PER_SEC);
|
||
|
char* buff = (char*)_client->GetBuffer(size);
|
||
|
return buff;
|
||
|
}
|
||
|
|
||
|
static TString256 _rpc_call;
|
||
|
static TString _rpc_string;
|
||
|
|
||
|
inline bool BoolCall()
|
||
|
{
|
||
|
CHECK(_connection, "Server not connected");
|
||
|
BOOL yes;
|
||
|
bool ok = _client->RequestBool(_connection, _rpc_call, yes);
|
||
|
if (!ok)
|
||
|
{
|
||
|
#ifndef DBG
|
||
|
yesnofatal_box("RPC call failed: %s", (const char*)_rpc_call);
|
||
|
#endif
|
||
|
yes = FALSE;
|
||
|
}
|
||
|
return yes ? TRUE : FALSE;
|
||
|
}
|
||
|
|
||
|
inline long IntCall()
|
||
|
{
|
||
|
CHECK(_connection, "Server not connected");
|
||
|
long n;
|
||
|
bool ok = _client->RequestInteger(_connection, _rpc_call, n);
|
||
|
if (!ok)
|
||
|
{
|
||
|
#ifndef DBG
|
||
|
yesnofatal_box("RPC call failed: %s", (const char*)_rpc_call);
|
||
|
#endif
|
||
|
n = 0;
|
||
|
}
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
inline TString& StrCall()
|
||
|
{
|
||
|
CHECK(_connection, "Server not connected");
|
||
|
bool ok = _client->RequestString(_connection, _rpc_call, _rpc_string);
|
||
|
if (!ok)
|
||
|
{
|
||
|
#ifndef DBG
|
||
|
yesnofatal_box("RPC call failed: %s", (const char*)_rpc_call);
|
||
|
#endif
|
||
|
_rpc_string.cut(0);
|
||
|
}
|
||
|
return _rpc_string;
|
||
|
}
|
||
|
|
||
|
inline bool BoolCallInt(const char* fn, long n)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld)", fn, n);
|
||
|
return BoolCall();
|
||
|
}
|
||
|
|
||
|
inline long IntCallInt(const char* fn, long n)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld)", fn, n);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
inline long IntCallIntInt(const char* fn, long n, long k)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,%ld)", fn, n, k);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
inline long IntCallIntIntInt(const char* fn, long n, long k, long f)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,%ld,%ld)", fn, n, k, f);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
|
||
|
inline long IntCallIntIntStr(const char* fn, long n, long k, const char* str)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,%ld,|%s|)", fn, n, k, str);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
inline long IntCallIntStr(const char* fn, long n, const char* str)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,|%s|)", fn, n, str);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
inline int IntCallIntStrInt(const char* fn, long n, const char* s, long f)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,|%s|,%ld)", fn, n, s, f);
|
||
|
return (int)IntCall();
|
||
|
}
|
||
|
|
||
|
inline long IntCallIntStrStr(const char* fn, long n, const char* str, const char* val)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,|%s|,|%s|)", fn, n, str, val);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
inline long IntCallStr(const char* fn, const char* str)
|
||
|
{
|
||
|
_rpc_call.format("%s(%s)", fn, str);
|
||
|
return IntCall();
|
||
|
}
|
||
|
|
||
|
|
||
|
inline TString& StrCallIntInt(const char* fn, long n, long k)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,%ld)", fn, n, k);
|
||
|
return StrCall();
|
||
|
}
|
||
|
|
||
|
inline TString& StrCallIntStr(const char* fn, long n, const char* str)
|
||
|
{
|
||
|
_rpc_call.format("%s(%ld,|%s|)", fn, n, str);
|
||
|
return StrCall();
|
||
|
}
|
||
|
|
||
|
bool rpc_DongleHasModule(word af)
|
||
|
{
|
||
|
return BoolCallInt("DongleHasModule", af);
|
||
|
}
|
||
|
|
||
|
bool rpc_DongleModules(word* int_tab0)
|
||
|
{
|
||
|
DWORD size;
|
||
|
real time;
|
||
|
char* buff = rpc_Request("DongleModules()", size, time);
|
||
|
if (buff)
|
||
|
memcpy(int_tab0, buff, (size_t)size);
|
||
|
return size > 0;
|
||
|
}
|
||
|
|
||
|
unsigned rpc_DongleNumber()
|
||
|
{
|
||
|
_rpc_call = "DongleNumber()";
|
||
|
return (unsigned)IntCall();
|
||
|
}
|
||
|
|
||
|
bool rpc_UserLogin(const char* server, const char* user,
|
||
|
const char* password, const char* application)
|
||
|
{
|
||
|
if (_client == NULL)
|
||
|
{
|
||
|
if (!rpc_Start())
|
||
|
return FALSE;
|
||
|
}
|
||
|
|
||
|
const bool local = server == NULL || *server == '\0';
|
||
|
TString name(40);
|
||
|
if (local)
|
||
|
name = "locale";
|
||
|
else
|
||
|
name = server;
|
||
|
const char* str = (const char*)name;
|
||
|
|
||
|
TString80 error;
|
||
|
|
||
|
if (_connection != 0)
|
||
|
_client->RemoveConnection(_connection);
|
||
|
|
||
|
_connection = _client->QueryConnection("1883", server);
|
||
|
|
||
|
if (_connection)
|
||
|
{
|
||
|
TString cmd(32);
|
||
|
cmd << "UserLogin(" << user << ")";
|
||
|
|
||
|
_rpc_call.format("UserLogin(%s,%s,%s)", user, password, application);
|
||
|
BOOL logged = FALSE;
|
||
|
bool connected = _client->RequestBool(_connection, _rpc_call, logged);
|
||
|
if (connected)
|
||
|
{
|
||
|
if (!logged)
|
||
|
{
|
||
|
connected = FALSE;
|
||
|
error.format("La connessione di %s e' stata rifiutata dal Server %s", (const char*)user, str);
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
error.format("Impossibile connettersi al Server %s", str);
|
||
|
}
|
||
|
|
||
|
if (!connected)
|
||
|
{
|
||
|
_client->RemoveConnection(_connection);
|
||
|
_connection = 0;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
error.format("Impossibile connettersi al Server %s", str);
|
||
|
}
|
||
|
|
||
|
if (error.not_empty())
|
||
|
error_box(error);
|
||
|
|
||
|
return _connection != 0;
|
||
|
}
|
||
|
|
||
|
bool rpc_UserLogout()
|
||
|
{
|
||
|
if (_connection)
|
||
|
{
|
||
|
rpc_Call("UserLogout()");
|
||
|
_client->RemoveConnection(_connection);
|
||
|
_connection = 0;
|
||
|
}
|
||
|
|
||
|
return TRUE;
|
||
|
}
|
||
|
|