-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcallback_thread.cpp
More file actions
96 lines (77 loc) · 2.37 KB
/
callback_thread.cpp
File metadata and controls
96 lines (77 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <mage.h>
#include <unistd.h>
#include <future>
#include <iostream>
using namespace mage;
using namespace std;
//
// All the code (well, almost)
// is the same as simple.cpp
//
int main() {
mage::RPC client("game", "localhost:8080");
//
// However, we will store the result into
// a future object
//
Json::Value params;
//
// I put things in my Json::Value.
//
params["password"] = "test";
//
// We make the call
//
// The third argument is meant to define the nature of
// the future call:
//
// - false: Run at call time (when you call res.get())
// - true: Run asynchronously
//
std::thread::id t1 = client.Call("user.register", params, [](mage::MageError err, Json::Value res) {
std::cout << "1:Executed, we will now sleep for 1 second..." << std::endl;
usleep(1000000);
if (err.type() == MAGE_RPC_ERROR) {
cerr << "An RPC error has occured: " << err.what() << " (code " << err.code() << ")" << endl;
return;
}
if (err.type() == MAGE_ERROR_MESSAGE) {
cerr << "mymodule.mycommand responded with an error: " << err.code() << endl;
return;
}
cout << "user.register: " << res << endl;
});
std::thread::id t2 = client.Call("user.register", params, [](mage::MageError err, Json::Value res) {
std::cout << "2:Executed, we will now sleep for 1 second..." << std::endl;
usleep(1000000);
if (err.type() == MAGE_RPC_ERROR) {
cerr << "An RPC error has occured: " << err.what() << " (code " << err.code() << ")" << endl;
return;
}
if (err.type() == MAGE_ERROR_MESSAGE) {
cerr << "mymodule.mycommand responded with an error: " << err.code() << endl;
return;
}
cout << "user.register: " << res << endl;
});
std::thread::id t3 = client.Call("user.register", params, [](mage::MageError err, Json::Value res) {
std::cout << "3:Executed, we will now sleep for 1 second..." << std::endl;
usleep(1000000);
if (err.type() == MAGE_RPC_ERROR) {
cerr << "An RPC error has occured: " << err.what() << " (code " << err.code() << ")" << endl;
return;
}
if (err.type() == MAGE_ERROR_MESSAGE) {
cerr << "mymodule.mycommand responded with an error: " << err.code() << endl;
return;
}
cout << "user.register: " << res << endl;
});
cout << "Scheduled" << endl;
client.Cancel(t1);
cout << "t1 canceled" << endl;
client.Join(t2);
cout << "t2 joined" << endl;
client.Join(t3);
cout << "t3 joined" << endl;
}