client: add onInit hook
This commit is contained in:
+19
-7
@@ -111,6 +111,7 @@ struct GPROp : public OperationBase
|
||||
{
|
||||
std::function<Value(Value&&)> builder;
|
||||
std::function<void(Result&&)> done;
|
||||
std::function<void (const Value&)> onInit;
|
||||
Value pvRequest;
|
||||
Value rpcarg;
|
||||
Result result;
|
||||
@@ -135,10 +136,11 @@ struct GPROp : public OperationBase
|
||||
_cancel(true);
|
||||
}
|
||||
|
||||
void setDone(decltype (done)&& cb)
|
||||
void setDone(decltype (done)&& donecb, decltype (onInit)&& initcb)
|
||||
{
|
||||
if(cb) {
|
||||
done = std::move(cb);
|
||||
onInit = std::move(initcb);
|
||||
if(donecb) {
|
||||
done = std::move(donecb);
|
||||
} else {
|
||||
auto waiter = this->waiter = std::make_shared<ResultWaiter>();
|
||||
done = [waiter](Result&& result) {
|
||||
@@ -166,10 +168,12 @@ struct GPROp : public OperationBase
|
||||
{
|
||||
auto context = chan->context;
|
||||
decltype (done) junk;
|
||||
decltype (onInit) junkI;
|
||||
bool ret;
|
||||
context->tcp_loop.call([this, &junk, &ret](){
|
||||
context->tcp_loop.call([this, &junk, &junkI, &ret](){
|
||||
ret = _cancel(false);
|
||||
junk = std::move(done);
|
||||
junkI = std::move(onInit);
|
||||
// leave opByIOID for GC
|
||||
});
|
||||
return ret;
|
||||
@@ -369,6 +373,14 @@ void Connection::handle_GPR(pva_app_msg_t cmd)
|
||||
|
||||
} else if(gpr->state==GPROp::Creating) {
|
||||
|
||||
try {
|
||||
if(gpr->onInit)
|
||||
gpr->onInit(data);
|
||||
} catch(std::exception& e) {
|
||||
gpr->result = Result(std::current_exception());
|
||||
gpr->state = GPROp::Done;
|
||||
}
|
||||
|
||||
if(cmd==CMD_PUT && gpr->getOput) {
|
||||
gpr->state = GPROp::GetOPut;
|
||||
|
||||
@@ -493,7 +505,7 @@ std::shared_ptr<Operation> GetBuilder::_exec_get()
|
||||
auto chan = Channel::build(ctx->shared_from_this(), _name);
|
||||
|
||||
auto op = std::make_shared<GPROp>(Operation::Get, chan);
|
||||
op->setDone(std::move(_result));
|
||||
op->setDone(std::move(_result), std::move(_onInit));
|
||||
op->pvRequest = _buildReq();
|
||||
|
||||
chan->pending.push_back(op);
|
||||
@@ -520,7 +532,7 @@ std::shared_ptr<Operation> PutBuilder::exec()
|
||||
auto chan = Channel::build(ctx->shared_from_this(), _name);
|
||||
|
||||
auto op = std::make_shared<GPROp>(Operation::Put, chan);
|
||||
op->setDone(std::move(_result));
|
||||
op->setDone(std::move(_result), std::move(_onInit));
|
||||
|
||||
if(_builder) {
|
||||
op->builder = std::move(_builder);
|
||||
@@ -561,7 +573,7 @@ std::shared_ptr<Operation> RPCBuilder::exec()
|
||||
auto chan = Channel::build(ctx->shared_from_this(), _name);
|
||||
|
||||
auto op = std::make_shared<GPROp>(Operation::RPC, chan);
|
||||
op->setDone(std::move(_result));
|
||||
op->setDone(std::move(_result), std::move(_onInit));
|
||||
if(_argument) {
|
||||
op->rpcarg = std::move(_argument);
|
||||
} else if(_args) {
|
||||
|
||||
Reference in New Issue
Block a user