Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d4cb58d

Browse filesBrowse files
committed
this_session: added unique id() function (#137)
1 parent 537f6c1 commit d4cb58d
Copy full SHA for d4cb58d

File tree

7 files changed

+52
-0
lines changed
Filter options

7 files changed

+52
-0
lines changed

‎doc/pages/cookbook.md

Copy file name to clipboardExpand all lines: doc/pages/cookbook.md
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,27 @@ int main() {
270270
}
271271
```
272272

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+
273294
## Client examples
274295

275296
### Creating a client

‎include/rpc/config.h

Copy file name to clipboardExpand all lines: include/rpc/config.h
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace rpc
1212
{
1313

14+
using session_id_t = std::intptr_t;
15+
1416
//! \brief Constants used in the library
1517
struct constants RPCLIB_FINAL {
1618
static RPCLIB_CONSTEXPR std::size_t DEFAULT_BUFFER_SIZE = 1024 << 10;

‎include/rpc/config.h.in

Copy file name to clipboardExpand all lines: include/rpc/config.h.in
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
namespace rpc
1212
{
1313

14+
using session_id_t = std::intptr_t;
15+
1416
//! \brief Constants used in the library
1517
struct constants RPCLIB_FINAL {
1618
static RPCLIB_CONSTEXPR std::size_t DEFAULT_BUFFER_SIZE = @RPCLIB_DEFAULT_BUFFER_SIZE@;

‎include/rpc/this_session.h

Copy file name to clipboardExpand all lines: include/rpc/this_session.h
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,21 @@ class this_session_t {
2525
//! handler.
2626
void post_exit();
2727

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+
2835
friend class rpc::detail::server_session;
2936

3037
private:
3138
void clear();
39+
void set_id(session_id_t value);
3240

3341
std::atomic_bool exit_{false};
42+
session_id_t id_{0};
3443
};
3544

3645
//! \brief A thread-local object that can be used to control the currently

‎lib/rpc/detail/server_session.cc

Copy file name to clipboardExpand all lines: lib/rpc/detail/server_session.cc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void server_session::do_read() {
6363
io_->post([this, msg, z]() {
6464
this_handler().clear();
6565
this_session().clear();
66+
this_session().set_id(reinterpret_cast<session_id_t>(this));
6667
this_server().cancel_stop();
6768

6869
auto resp = disp_->dispatch(msg, suppress_exceptions_);

‎lib/rpc/this_session.cc

Copy file name to clipboardExpand all lines: lib/rpc/this_session.cc
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,13 @@ void this_session_t::clear() {
1616
exit_ = false;
1717
}
1818

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+
1927

2028
} /* rpc */

‎tests/rpc/server_session_test.cc

Copy file name to clipboardExpand all lines: tests/rpc/server_session_test.cc
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "rpc/client.h"
77
#include "rpc/server.h"
8+
#include "rpc/this_session.h"
89
#include "rpc/rpc_error.h"
910
#include "rpc/detail/make_unique.h"
1011
#include "testutils.h"
@@ -18,6 +19,7 @@ class server_session_test : public testing::Test {
1819
c("127.0.0.1", test_port) {
1920
s.bind("consume_big_param", [](std::string const& str){ (void)str; });
2021
s.bind("func", [](){ return 0; });
22+
s.bind("get_sid", [](){ return rpc::this_session().id(); });
2123
s.async_run();
2224
}
2325

@@ -44,6 +46,13 @@ TEST_F(server_session_test, connection_closed_properly) {
4446
// no crash is enough
4547
}
4648

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+
4756
TEST(server_session_test_bug153, bug_153_crash_on_client_timeout) {
4857
rpc::server s("127.0.0.1", rpc::constants::DEFAULT_PORT);
4958
s.bind("bug_153", []() {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.