30#ifndef PT_REMOTING_REMOTEPROCEDURE_H
31#define PT_REMOTING_REMOTEPROCEDURE_H
33#include <Pt/Remoting/Api.h>
34#include <Pt/Remoting/Client.h>
35#include <Pt/SerializationContext.h>
36#include <Pt/Composer.h>
37#include <Pt/Decomposer.h>
38#include <Pt/NonCopyable.h>
50class PT_REMOTING_API RemoteCall :
private Pt::NonCopyable
53 RemoteCall(Client& client,
const String& name);
55 RemoteCall(Client& client,
const std::string& name);
57 RemoteCall(Client& client,
const char* name);
59 virtual ~RemoteCall();
74 {
return _client->isFailed(); }
91 virtual void onReady() = 0;
95 virtual void onReset() = 0;
99 virtual void onClear() = 0;
119 _result =
new (_mem) R;
127 void init(RemoteCall* call)
136 return _call->isFailed();
160 _result =
new (_mem) R;
165 char _mem[
sizeof(R) ];
173class RemoteProcedureBase :
public RemoteCall
176 RemoteProcedureBase(Client& client,
const std::string& name)
177 : RemoteCall(client, name)
184 ~RemoteProcedureBase()
187 _r->~BasicComposer<R>();
207 {
return _finished; }
211 { _finished.send(_result); }
217 _r->~BasicComposer<R>();
221 _r =
new (_mem) BasicComposer<R>( &client().context() );
222 _r->
begin( result().value() );
226 virtual void onReset()
230 _r->~BasicComposer<R>();
235 virtual void onClear()
242 Signal< const Result<R>& > _finished;
244 char _mem[
sizeof(BasicComposer<R>) ];
245 BasicComposer<R>* _r;
250class RemoteArgument :
private Pt::NonCopyable
253 RemoteArgument(SerializationContext* )
260 _decomposer->~BasicDecomposer<A>();
263 void begin(
const A& a,
const char* name, SerializationContext& ctx)
267 _decomposer->~BasicDecomposer<A>();
271 _decomposer =
new (_mem) BasicDecomposer<A>(&ctx);
272 _decomposer->begin(a, name);
275 void clear(SerializationContext* )
279 _decomposer->~BasicDecomposer<A>();
285 BasicDecomposer<A>* decomposer()
286 {
return reinterpret_cast<BasicDecomposer<A>*
>(_mem); }
289 char _mem[
sizeof(BasicDecomposer<A>) ];
290 BasicDecomposer<A>* _decomposer;
294template <
typename... Ts>
295class RemoteArguments;
299class RemoteArguments<>
302 explicit RemoteArguments(SerializationContext* )
305 void begin(SerializationContext& )
308 void fill(std::size_t i, Decomposer** args)
313 void clear(SerializationContext* )
318template <
typename T,
typename... Ts>
319class RemoteArguments<T, Ts...>
322 explicit RemoteArguments(SerializationContext* ctx)
327 void begin(SerializationContext& ctx,
const T& a,
const Ts&... as)
329 _head.begin(a,
"", ctx);
330 _tail.begin(ctx, as...);
333 void fill(std::size_t i, Decomposer** args)
335 args[i] = _head.decomposer();
336 _tail.fill(i + 1, args);
339 void clear(SerializationContext* ctx)
346 RemoteArgument<T> _head;
347 RemoteArguments<Ts...> _tail;
372template <
typename R,
typename... As>
380 , _argv( &
client.context() )
382 _argv.fill(0, _args);
396 _argv.begin(this->client().context(), args...);
399 this->client().beginCall(r, *
this, _args,
sizeof...(As));
410 const R&
call(
const As&... args)
414 _argv.begin(this->client().context(), args...);
417 this->client().call(r, *
this, _args,
sizeof...(As));
426 return this->
result().value();
433 return this->
call(args...);
439 RemoteProcedureBase<R>::onReset();
440 _argv.clear( &this->client().context() );
444 RemoteArguments<As...> _argv;
Composes serializable types during serialization.
Definition Composer.h:327
void begin(T &type)
Begin composing a type.
Definition Composer.h:337
Manages the decomposition of types during serialization.
Definition Decomposer.h:121
Base class that disables copy construction and assignment.
Definition NonCopyable.h:59
A client for remote procedure calls.
Definition Client.h:48
void cancel()
Cancels the remote call.
Result< R > & result()
Returns the result of the call.
Definition RemoteProcedure.h:192
void begin(const As &... args)
Begins an asynchronous remote call.
Definition RemoteProcedure.h:392
const R & operator()(const As &... args)
Blocking remote call.
Definition RemoteProcedure.h:431
Signal< const Result< R > & > & finished()
Reports that the result has been received.
Definition RemoteProcedure.h:206
RemoteProcedure(Client &client, const std::string &name)
Construct with procedure name and client.
Definition RemoteProcedure.h:378
Client & client()
Returns the client used for communication.
Definition RemoteProcedure.h:63
const String & name() const
Returns the name of the procedure.
Definition RemoteProcedure.h:68
const R & call(const As &... args)
Blocking remote call.
Definition RemoteProcedure.h:410
bool isFailed() const
Returns true if the procedure failed.
Definition RemoteProcedure.h:73
virtual ~RemoteProcedure()
Destructor.
Definition RemoteProcedure.h:387
const Result< R > & result() const
Returns the result of the call.
Definition RemoteProcedure.h:199
Result of a remote procedure call.
Definition RemoteProcedure.h:111
const R & get() const
Ends a remote procedure call.
Definition RemoteProcedure.h:151
bool isFailed() const
Indicates if the procedure has failed.
Definition RemoteProcedure.h:134
R & value()
The return value.
Definition RemoteProcedure.h:141
Result(RemoteCall *call=0)
Constructor.
Definition RemoteProcedure.h:115
Multicast Signal to call multiple slots.
Definition Signal.h:190
Unicode capable basic_string.
Definition Api-String.h:67
Remote services and clients.
Definition HttpService.h:39
Core module.
Definition Allocator.h:33