Repository navigation
Expand file tree
/
Copy pathsimple_sessions.cpp
More file actions
48 lines (43 loc) · 1.74 KB
/
Copy pathsimple_sessions.cpp
File metadata and controls
48 lines (43 loc) · 1.74 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
// Copyright 2013 (c) Google, Inc.
// Copyright 2013 (c) Dean Michael Berris <dberris@google.com>
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <http/server/simple_sessions.hpp>
#include <cassert>
namespace network {
namespace http {
session simple_sessions::lookup(boost::string_ref session_id) {
std::string real_session_id =
session_id.empty() ? std::to_string(next_session_id_.fetch_add(
1, std::memory_order::memory_order_seq_cst)) :
static_cast<std::string>(session_id);
assert(real_session_id != "");
std::lock_guard<std::mutex> lock_sessions(sessions_mutex_);
std::pair<session_map_type::iterator, bool> result =
sessions_.insert(make_pair(std::move(real_session_id), session()));
if (result.second) {
result.first->second.set("id", result.first->first);
assert(result.first->second["id"] != "");
}
return result.first->second;
}
void simple_sessions::update(boost::string_ref session_id, session&& session) {
std::string real_session_id =
session_id.empty() ? std::to_string(next_session_id_.fetch_add(
1, std::memory_order::memory_order_seq_cst)) :
static_cast<std::string>(session_id);
assert(real_session_id != "");
std::lock_guard<std::mutex> lock_sessions(sessions_mutex_);
auto it = sessions_.find(real_session_id);
if (it != sessions_.end()) {
it->second = session;
} else {
bool added = false;
std::tie(it, added) = sessions_.insert(make_pair(std::move(real_session_id),
session));
assert(added && " -- for some reason we cannot add here.");
}
}
} // namespace http
} // namespace network