Home » Developing U++ » U++ Developers corner » RPC_METHOD how to "define"
Re: RPC_METHOD how to "define" [message #47589 is a reply to message #47578] |
Thu, 02 February 2017 17:14 |
|
Well, the problem is that THISBACK can't be converted to function pointer as expected by Register. This can be worked around, but I had to made an one assumption: Only one instance of the class exists at a time. This is quite reasonable in RPC server, since otherwise you wouldn't know which instance to use when client calls one of the methods.
Here is a working (tested ) code:#include <Core/Core.h>
#include <Core/Rpc/Rpc.h>
using namespace Upp;
class MyRpcClass {
Time last;
public:
typedef MyRpcClass CLASSNAME;
void Status(RpcData& rpc) {
rpc << last;
}
void Ping(RpcData& rpc) {
last = GetSysTime();
rpc << last;
}
static void RegisterMethods() {
// we use lambda to get a singleton instance of the class and call its method
Register("Status", [](RpcData& rpc){Single<MyRpcClass>().Status(rpc);});
Register("Ping", [](RpcData& rpc){Single<MyRpcClass>().Ping(rpc);});
}
};
INITBLOCK {
// calls all the registrations before main executes
MyRpcClass::RegisterMethods();
}
CONSOLE_APP_MAIN
{
Cout() << "Server...\n";
LogRpcRequests();
RpcServerLoop(1234);
}
It could be made slightly prettier with macros, but I didn't want to obfuscate the logic too much...
Honza
|
|
|
Goto Forum:
Current Time: Tue Apr 30 04:46:45 CEST 2024
Total time taken to generate the page: 0.89312 seconds
|