File tree 7 files changed +52
-0
lines changed
Filter options
7 files changed +52
-0
lines changed
Original file line number Diff line number Diff line change @@ -270,6 +270,27 @@ int main() {
270
270
}
271
271
```
272
272
273
+ ### Storing per-session data
274
+
275
+ ``` cpp
276
+ #include < unordered_map>
277
+ #include " rpc/server.h"
278
+ #include " rpc/this_session.h"
279
+
280
+ int main () {
281
+ rpc::server srv(8080); // listen on TCP port 8080
282
+ std::unordered_map<rpc::session_id_t, std::string> data;
283
+
284
+ srv.bind("store_me_maybe", [](std::string const& value) {
285
+ auto id = rpc::this_session().id();
286
+ data[id] = value;
287
+ });
288
+
289
+ srv.run(); // blocking call
290
+ return 0;
291
+ }
292
+ ```
293
+
273
294
## Client examples
274
295
275
296
### Creating a client
Original file line number Diff line number Diff line change 11
11
namespace rpc
12
12
{
13
13
14
+ using session_id_t = std::intptr_t ;
15
+
14
16
// ! \brief Constants used in the library
15
17
struct constants RPCLIB_FINAL {
16
18
static RPCLIB_CONSTEXPR std::size_t DEFAULT_BUFFER_SIZE = 1024 << 10 ;
Original file line number Diff line number Diff line change 11
11
namespace rpc
12
12
{
13
13
14
+ using session_id_t = std ::intptr_t ;
15
+
14
16
//! \brief Constants used in the library
15
17
struct constants RPCLIB_FINAL {
16
18
static RPCLIB_CONSTEXPR std ::size_t DEFAULT_BUFFER_SIZE = @RPCLIB_DEFAULT_BUFFER_SIZE @;
Original file line number Diff line number Diff line change @@ -25,12 +25,21 @@ class this_session_t {
25
25
// ! handler.
26
26
void post_exit ();
27
27
28
+ // ! \brief Returns an ID that uniquely identifies a session.
29
+ // ! \note This is not an ID for the client. If the client disconnects
30
+ // ! and reconnects, this ID may change. That being said, you can
31
+ // ! use this ID to store client-specific information *for the duration
32
+ // ! of the session.
33
+ session_id_t id () const ;
34
+
28
35
friend class rpc ::detail::server_session;
29
36
30
37
private:
31
38
void clear ();
39
+ void set_id (session_id_t value);
32
40
33
41
std::atomic_bool exit_{false };
42
+ session_id_t id_{0 };
34
43
};
35
44
36
45
// ! \brief A thread-local object that can be used to control the currently
Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ void server_session::do_read() {
63
63
io_->post ([this , msg, z]() {
64
64
this_handler ().clear ();
65
65
this_session ().clear ();
66
+ this_session ().set_id (reinterpret_cast <session_id_t >(this ));
66
67
this_server ().cancel_stop ();
67
68
68
69
auto resp = disp_->dispatch (msg, suppress_exceptions_);
Original file line number Diff line number Diff line change @@ -16,5 +16,13 @@ void this_session_t::clear() {
16
16
exit_ = false ;
17
17
}
18
18
19
+ session_id_t this_session_t::id () const {
20
+ return id_;
21
+ }
22
+
23
+ void this_session_t::set_id (session_id_t value) {
24
+ id_ = value;
25
+ }
26
+
19
27
20
28
} /* rpc */
Original file line number Diff line number Diff line change 5
5
6
6
#include " rpc/client.h"
7
7
#include " rpc/server.h"
8
+ #include " rpc/this_session.h"
8
9
#include " rpc/rpc_error.h"
9
10
#include " rpc/detail/make_unique.h"
10
11
#include " testutils.h"
@@ -18,6 +19,7 @@ class server_session_test : public testing::Test {
18
19
c (" 127.0.0.1" , test_port) {
19
20
s.bind (" consume_big_param" , [](std::string const & str){ (void )str; });
20
21
s.bind (" func" , [](){ return 0 ; });
22
+ s.bind (" get_sid" , [](){ return rpc::this_session ().id (); });
21
23
s.async_run ();
22
24
}
23
25
@@ -44,6 +46,13 @@ TEST_F(server_session_test, connection_closed_properly) {
44
46
// no crash is enough
45
47
}
46
48
49
+ TEST_F (server_session_test, session_id_unique) {
50
+ rpc::client c2 (" localhost" , rpc::constants::DEFAULT_PORT);
51
+ auto sid1 = c.call (" get_sid" ).as <rpc::session_id_t >();
52
+ auto sid2 = c2.call (" get_sid" ).as <rpc::session_id_t >();
53
+ EXPECT_NE (sid1, sid2);
54
+ }
55
+
47
56
TEST (server_session_test_bug153, bug_153_crash_on_client_timeout) {
48
57
rpc::server s (" 127.0.0.1" , rpc::constants::DEFAULT_PORT);
49
58
s.bind (" bug_153" , []() {
You can’t perform that action at this time.
0 commit comments