Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 Go to previous messageGo to previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

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 Smile) 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
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Requirement Printing in a batch -> new Feature for PrinterJob
Next Topic: C2280
Goto Forum:
  


Current Time: Tue Apr 30 04:46:45 CEST 2024

Total time taken to generate the page: 0.89312 seconds