summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib
diff options
context:
space:
mode:
author Christian Brunschen <christian@brunschen.com>2017-07-05 17:27:34 +0100
committer Christian Brunschen <christian@brunschen.com>2017-07-05 17:27:34 +0100
commit0206314395c7dcf7b51311e635588778f6d9cca3 (patch)
treeab242d5e87d9c1f2578edda08c7f150b841831e6 /src/lib
parent2924e3c17470291a520492d68189dda16e99e2a4 (diff)
Add an external panel for the Ensoniq VFX family of keyboards, with a websocket interface and an HTML/Javascript implementation that can be served over HTTP.
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/util/server_http.hpp5
-rw-r--r--src/lib/util/server_ws.hpp28
2 files changed, 21 insertions, 12 deletions
diff --git a/src/lib/util/server_http.hpp b/src/lib/util/server_http.hpp
index d50ccc05bc7..b50161b1e3b 100644
--- a/src/lib/util/server_http.hpp
+++ b/src/lib/util/server_http.hpp
@@ -199,6 +199,7 @@ namespace webpp {
}
void clear()
{
+ std::lock_guard<std::mutex> lock(m_resource_mutex);
m_resource.clear();
}
@@ -206,12 +207,12 @@ namespace webpp {
std::function<void(std::shared_ptr<socket_type> socket, std::shared_ptr<typename ServerBase<socket_type>::Request>)> on_upgrade;
private:
- /// Warning: do not add or remove resources after start() is called
+ /// Warning: do not access (red or write) m_resources without holding m_resource_mutex
std::map<regex_orderable, std::map<std::string, std::tuple<path2regex::Keys, http_handler>>> m_resource;
+ std::mutex m_resource_mutex;
std::map<std::string, http_handler> m_default_resource;
- std::mutex m_resource_mutex;
public:
virtual void start() {
if(!m_io_context)
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>();