summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/server_ws.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/server_ws.hpp')
-rw-r--r--src/lib/util/server_ws.hpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/src/lib/util/server_ws.hpp b/src/lib/util/server_ws.hpp
index b9d4721e96b..41e8cda3287 100644
--- a/src/lib/util/server_ws.hpp
+++ b/src/lib/util/server_ws.hpp
@@ -53,7 +53,7 @@ namespace webpp {
public:
virtual ~SocketServerBase() {}
- class SendStream : public std::ostream {
+ class SendStream : public std::ostream, public std::enable_shared_from_this<SendStream> {
friend class SocketServerBase<socket_type>;
asio::streambuf streambuf;
@@ -65,7 +65,7 @@ namespace webpp {
};
- class Connection {
+ class Connection : public std::enable_shared_from_this<Connection> {
friend class SocketServerBase<socket_type>;
friend class SocketServer<socket_type>;
@@ -80,6 +80,10 @@ namespace webpp {
std::string remote_endpoint_address;
unsigned short remote_endpoint_port;
+
+ std::shared_ptr<Connection> ptr() {
+ return this->shared_from_this();
+ }
private:
explicit Connection(socket_type *socket): remote_endpoint_port(0), socket(socket), strand(socket->get_io_service()), closed(false) { }
@@ -143,7 +147,7 @@ namespace webpp {
}
};
- class Message : public std::istream {
+ class Message : public std::istream, public std::enable_shared_from_this<Message> {
friend class SocketServerBase<socket_type>;
public:
@@ -163,7 +167,7 @@ namespace webpp {
asio::streambuf streambuf;
};
- class Endpoint {
+ class Endpoint : public std::enable_shared_from_this<Endpoint> {
friend class SocketServerBase<socket_type>;
std::unordered_set<std::shared_ptr<Connection> > connections;
std::mutex connections_mutex;
@@ -215,8 +219,9 @@ namespace webpp {
}
};
public:
- /// Warning: do not add or remove endpoints after start() is called
- std::map<regex_orderable, Endpoint> endpoint;
+ /// Warning: do not access (red or write) m_endpoint without holding m_endpoint_mutex
+ std::map<regex_orderable, Endpoint> m_endpoint;
+ std::mutex m_endpoint_mutex;
virtual void start() {
if(!io_context)
@@ -247,7 +252,8 @@ namespace webpp {
acceptor->close();
io_context->stop();
- for(auto& p: endpoint)
+ std::lock_guard<std::mutex> lock(m_endpoint_mutex);
+ for(auto& p: m_endpoint)
p.second.connections.clear();
}
@@ -310,7 +316,8 @@ namespace webpp {
std::unordered_set<std::shared_ptr<Connection> > get_connections() {
std::unordered_set<std::shared_ptr<Connection> > all_connections;
- for(auto& e: endpoint) {
+ std::lock_guard<std::mutex> lock(m_endpoint_mutex);
+ for(auto& e: m_endpoint) {
std::lock_guard<std::mutex> lock(e.second.connections_mutex);
all_connections.insert(e.second.connections.begin(), e.second.connections.end());
}
@@ -425,10 +432,11 @@ namespace webpp {
}
}
}
-
+
void write_handshake(const std::shared_ptr<Connection> &connection, const std::shared_ptr<asio::streambuf> &read_buffer) {
//Find path- and method-match, and generate response
- for (auto &regex_endpoint : endpoint) {
+ std::lock_guard<std::mutex> lock(m_endpoint_mutex);
+ for (auto &regex_endpoint : m_endpoint) {
std::smatch path_match;
if(std::regex_match(connection->path, path_match, regex_endpoint.first)) {
auto write_buffer = std::make_shared<asio::streambuf>();