Skip to content

Commit

Permalink
add user data in connection.
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 30, 2021
1 parent b835c03 commit b7936d8
Show file tree
Hide file tree
Showing 5 changed files with 743 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/client/basic_client.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>..\..\include;..\..\third\msgpack\include;$(IncludePath)</IncludePath>
<IncludePath>..\..\include;..\..\thirdparty\msgpack-c\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>..\..\include;..\..\third\msgpack\include;$(IncludePath)</IncludePath>
Expand Down
4 changes: 2 additions & 2 deletions examples/server/basic_server.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@
<IncludePath>..\..\include;..\..\third\msgpack\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<IncludePath>..\..\include;..\..\third\msgpack\include;D:\boost_1_73_0;$(IncludePath)</IncludePath>
<LibraryPath>D:\boost_1_73_0\lib64-msvc-14.2;$(LibraryPath)</LibraryPath>
<IncludePath>..\..\include;..\..\thirdparty\msgpack-c\include;$(IncludePath)</IncludePath>
<LibraryPath>$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<IncludePath>..\..\include;..\..\third\msgpack\include;$(IncludePath)</IncludePath>
Expand Down
7 changes: 7 additions & 0 deletions examples/server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ using namespace rpc_service;

struct dummy{
int add(rpc_conn conn, int a, int b) {
auto shared_conn = conn.lock();
if (shared_conn) {
shared_conn->set_user_data(std::string("aa"));
auto s = conn.lock()->get_user_data<std::string>();
std::cout << s << '\n'; //aa
}

return a + b;
}
};
Expand Down
12 changes: 12 additions & 0 deletions include/rest_rpc/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "const_vars.h"
#include "router.h"
#include "cplusplus_14.h"
#include "nonstd_any.hpp"

using boost::asio::ip::tcp;

Expand Down Expand Up @@ -73,6 +74,16 @@ namespace rest_rpc {

int64_t conn_id() const { return conn_id_; }

template<typename T>
void set_user_data(const T &data) {
user_data_ = data;
}

template<typename T>
T get_user_data() {
return nonstd::any_cast<T>(user_data_);
}

const std::vector<char>& body() const {
return body_;
}
Expand Down Expand Up @@ -364,6 +375,7 @@ namespace rest_rpc {
std::deque<message_type> write_queue_;
std::function<void(std::string, std::string, std::weak_ptr<connection>)> callback_;
router& router_;
nonstd::any user_data_;
};
} // namespace rpc_service
} // namespace rest_rpc
Expand Down
Loading

0 comments on commit b7936d8

Please sign in to comment.